[] What data structure is a binary search tree (BST)

What is a binary search tree (BST)

1. What is BST

For each node in the binary tree X, the value of its left subtree are less than all of the item X, its right subtree is greater than the value of all the items X. Such a binary tree is a binary search tree.

The above is a binary search tree, which is characterized by:

(1) if its left subtree is not empty, then the left subtree of the node is less than the value of all the values of its root;
(2) if its right subtree is not empty, then the right subtree values of all the nodes are greater than the value of the root node;
(3) the other of left and right subtrees are also binary search tree;
(4) binary search tree is a dynamic lookup table, seen looking in the process of adding and deleting the the corresponding element, these operations need to remain in the binary search tree of the above properties.

2. Definition of a binary search tree

According to the requirements of a binary search tree, we let BinaryNode implement the Comparable interface.


// 树节点
class BinaryNode implements Comparable {

    Integer element;

    BinaryNode left;

    BinaryNode right;

    public BinaryNode{

    }

    public BinaryNode(Integer element, BinaryNode left, BinaryNode right) {
        this.element = element;
        this.left = left;
        this.right = right;
    }

    @Override
    public int compareTo(@NonNull Object o) {
        return this.element - (Integer) o;
    }
}

Let's complete the definition of BinarySearchTree of (only defines the key profile, follow specific interfaces for analysis).


public class BinarySearchTree {

    //定义树的根节点
    BinaryNode root;

    public BinarySearchTree() {
        this.root = null;
    }

    public void makeEmpty() {
        this.root = null;
    }

    public boolean isEmpty() {
        return this.root == null;
    }

    // 判断是否包含某个元素
    public boolean contains(Integer x) {
        //TODO:后续讲解
        return false;
    }

    // 查找最小值
    public BinaryNode findMin(){
        //TODO:后续讲解
        return null;
    }

    // 查找最大值
    public BinaryNode findMax(){
        //TODO:后续讲解
        return null;
    }

    // 按照顺序插入值
    public void insert(Integer x){
        //TODO:后续讲解
    }

    // 删除某个值
    public void remove(Integer x){
        //TODO:后续讲解
    }
    
    // 打印树
    public void printTree(){
        //TODO:后续讲解
    }
}

3. contaions operation

If a node in the tree contains the item X T, then the operation returns true, otherwise false.

Tree makes this operation very simple, if T is an empty set, direct return false; otherwise we recursive search on the left or right subtree subtree of T until you find the X.

Code is implemented as follows:


/**
     * 是否包含某个元素
     * @param x 待查找对象
     * @return 查找结果
     */
    public boolean contains(Integer x) {
        // 首次查找从根节点开始
        return contains(x,root);
    }

    private boolean contains(Integer x, BinaryNode node) {
        // 根节点为空的情况,不需要再查找
        if (node == null) {
            return false;
        }
        
        // 与当前节点进行比较
        int compareResult = x.compareTo(node.element);

        // 小于当前节点的值,就递归遍历左子树
        if (compareResult < 0) {
            return contains(x, node.left);
        } 
        // 大于当前节点的值,就递归遍历右子树
        else if (compareResult > 0) {
            return contains(x, node.right);
        } 
        // 等于当前节点值,直接返回
        else {
            return true;
        }
    }


4. Find the lowest node

And as long as there is a left sub-tree root on the left from the start, the end point is the smallest element node.


// 查找最小值
    public BinaryNode findMin() {
        return findMin(root);
    }

    private BinaryNode findMin(BinaryNode node) {
        // 当前节点为null,直接返回null
        if (node == null) {
            return null;
        }

        // 不存在左子树,返回当前节点
        if (node.left == null) {
            return node;
        }

        // 递归遍历左子树
        return findMin(node.left);

    }

5. Find the maximum node

And as long as you have the right subtree root right from the start, the end point is the greatest element node. In contrast with the findMin, findMax we abandoned the usual method of recursion, use a while loop to find common.


 // 查找最大值
    public BinaryNode findMax() {
        return findMax(root);
    }

    private BinaryNode findMax(BinaryNode node) {
        if (node != null) {
            while (node.right != null) {
                node = node.right;
            }
        }
        return node;
    }

6. insert operation

Insert operation is conceptually simple, in order to insert the X-tree T, we like the same look along the tree contains.
If you find X, then we can do nothing, you can also do some updates.
Otherwise, it will be inserted into the last node X on the traversal path. (This practice is open to question)

Code is implemented as follows:


public void insert(Integer x) {
        root = insert(x, root);
    }

    // 返回的插入节点的根节点
    private BinaryNode insert(Integer x, BinaryNode node) {
        // 如果当前节点为null,新建节点返回
        if (node == null) {
            return new BinaryNode(x, null, null);
        }
        // 与当前节点比较
        int compareResult = x.compareTo(node.element);

        // 小于当前节点值,递归插入左子树,并将返回值设置为当前节点的left
        if (compareResult < 0) {
            node.left = insert(x, node.left);
        }


         // 大于当前节点值,递归插入右子树,并将返回值设置为当前节点的right
        if (compareResult > 0) {
            node.right = insert(x, node.right);
        }

        // 等于当前的值,不做任何处理
        if (compareResult == 0) {
            // do some update or do noting
        }
        
        return node;

    }

7. deletion

Just as many data structures as the most difficult operations are remove, once we discovered the element to be deleted, it is necessary to consider several possible scenarios:

When a node is a leaf node to be deleted, delete it.
When the node to be deleted has only one son node, the son node instead of the position of the node, and then delete the node.
When a node to be deleted node has two sons, a general strategy is to delete the data instead of the node with minimal data in its right subtree and recursively delete the node (now it is empty); because the minimum right subtree node can not have left his son, so the second Delete easier.


    // 删除某个值
    public void remove(Integer x) {
        remove(x, root);
    }

    private BinaryNode remove(Integer x, BinaryNode node) {
        if (node == null) {
            return null;
        }

        int compareResult = x.compareTo(node.element);

        if (compareResult < 0) {
            node.left = remove(x, node.left);
        }

        if (compareResult > 0) {
            node.right = remove(x, node.right);
        }

        if (compareResult == 0) {

            if (node.left != null && node.right != null) {

                node.element = findMin(node.right).element;

                node.right = remove(node.element, node.right);

            } else {

                node = (node.left != null) ? node.left : node.right;

            }

        }


        return node;

    }


8. Limitations binary search tree

The same data may correspond to a different binary search tree, as follows:

Binary search tree may degenerate into a linked list, and correspondingly, the lookup operation binary search tree and the tree is highly relevant at a time when The tree height is the number of nodes The tree of n, while binary search tree corresponding algorithm degenerates into all O (n) level.

Obviously, he says binary search tree to find, insert, delete these three operations are O (lgn) level, only an approximate estimate, to specific shapes and binary search tree related. But overall time complexity in o (logn)
between the o (n).

9. A brief summary

Node binary search tree query structure and delete performance, associated with the height of the tree, if the binary search tree can be more "balanced" Some avoid the tilt tree structure to a linear structure, it is possible to significantly reduce the time complexity. Storage binary search tree, only a linear structure with respect to the element values ​​stored in the tree nodes need parent-child relationship between the nodes to save additional space, so the storage consumption than a linear structure.

Guess you like

Origin www.cnblogs.com/54chensongxia/p/11567886.html