Implement binary search tree

Binary search tree has the following characteristics:

  A binary search tree is a binary
  value of 2 left subtree binary search tree of each node is less than the value of the node, the value of each node in the right subtree of the node is greater than the value of
  3 of each of them any node subtree meets the definition of a binary search tree

We need to insert the elements of the tree based on the second point to achieve Comparable interface, implementation of the prototype the following binary search tree, this class has member variables and methods substantially binary search tree, and then after the implementation of the operating element.

public class BST<E extends Comparable<E>> {

    private class Node{
        public E e;
        public Node left, right;

        public Node (e) {
             this .and = e;
            left = null;
            right = null;
        }
    }

    private Node root;
    private int size;

    public BST(){
        root = null;
        size = 0;
    }

    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }
}

 

Add a binary search tree

  Conditions To add an element to the binary search tree using recursion properly achieved, recursion is terminating the current node is null

// Add a new element e to the binary search tree 
    public  void the Add (E e) {
        root = add(root, e);
    }

    // Insert element e in the root node of the binary search tree, recursive algorithm
     // returns the new node is inserted into the root after the binary search tree 
    Private the Node the Add (the Node node, E e) {

        if(node == null){
            size ++;
            return new Node(e);
        }

        if(e.compareTo(node.e) < 0)
            node.left = add(node.left, e);
        else if(e.compareTo(node.e) > 0)
            node.right = add(node.right, e);

        return node;
    }

 

Queries binary search tree

  Method queries whether to include an element tree and insertion of the idea is the same, with a recursive implementation, on behalf of recursion when the current node is null in the end is still not found, returns false, otherwise it will end soon find recursive return true

// see if the binary search tree contains elements E 
    public  Boolean the contains (E E) {
         return the contains (the root, E);
    }

    // look to the root node of the binary search tree contains the elements e, a recursive algorithm 
    Private  Boolean the contains (the Node node, E E) {

        if(node == null)
            return false;

        if(e.compareTo(node.e) == 0)
            return true;
        else if(e.compareTo(node.e) < 0)
            return contains(node.left, e);
        else // e.compareTo(node.e) > 0
            return contains(node.right, e);
    }

Next is the traversal of a binary search tree

  Binary search tree traversal into a preorder traversal, in order traversal and postorder traversals, where the front access sequence refers to the root node, then the root node traversal preamble to traverse the left and right subtrees, the first traversal order is sub-tree root node and then to the left and then the right subtree, after the order is about to traverse the subtree, then the root node traversal

  Former first realization preorder traversal, recursive very easy to implement, the first to traverse the tree to the root node and then recursively sub

// preorder traversal binary search tree 
    public  void preOrder () {
        preOrder(root);
    }

    // preorder traversal to the root node of the binary search tree, recursive algorithm 
    Private  void preOrder (the Node node) {

        if(node == null)
            return;

        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

In order and post-order traversal only need to change the position of the statement can be realized

 // inorder traversal of the binary search tree 
    public  void inOrder () {
        inOrder(root);
    }

    // traversal sequence to the root node of the binary search tree, recursive algorithm 
    Private  void inOrder (the Node node) {

        if(node == null)
            return;

        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    // postorder traversal of the binary search tree 
    public  void postorder () {
        postOrder(root);
    }

    // postorder to the root node of the binary search tree, recursive algorithm 
    Private  void postorder (the Node node) {

        if(node == null)
            return;

        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

Sequence binary search tree traversal

  Preorder layer, layer by layer by definition is traversed from the root node, and then the second and third layers and so on, there is no need recursion, can be realized using a queue

  The elements of each enqueue, dequeue and then the time the child nodes of the node teams

// sequence binary search tree traversal 
    public  void levelOrder () {

        Queue<Node> q = new LinkedList<>();
        q.add(root);
        while(!q.isEmpty()){
            Node cur = q.remove();
            System.out.println (cur.e);

            if(cur.left != null)
                q.add(cur.left);
            if(cur.right != null)
                q.add(cur.right);
        }
    }

Find the maximum and minimum node

  First of all you need to know that, according to the left subtree must be younger than the root, right subtree certainly larger than the root of this principle can know right child node of the tree must be maximum is null, while the smallest node in the left subtree are bound is null, know methods found these two points of maximum and minimum node after it is very easy to implement, but also the use of recursion

// Find the smallest binary search tree element 
    public E Minimum () {
         IF (size == 0 )
             the throw  new new an IllegalArgumentException ( "empty the BST IS!" );

        return minimum(root).e;
    }

    // node returns to the root node of the binary search tree where the minimum 
    Private the Node Minimum (the Node node) {
         IF (node.left == null )
             return node;
         return Minimum (node.left);
    }

    // Find the maximum binary search tree element 
    public E maximum () {
         IF (size == 0 )
             the throw  new new an IllegalArgumentException ( "empty the BST IS" );

        return maximum(root).e;
    }

    // returns the root node of the binary search tree nodes where the maximum value of 
    Private the Node maximum (the Node node) {
         IF (node.right == null )
             return node;

        return maximum(node.right);
    }

 

To delete the maximum and minimum node

  Mentioned above, the right subtree largest nodes must be as null, while the left subtree smallest nodes are bound to null, so delete the maximum node when the direct use of its left subtree replaced by point abridged, delete the lowest node when the direct use of its instead of the right subtree can achieve recursive

// delete the node where the minimum value from the binary search tree, returns the minimum value 
    public E removeMin () {
        E ret = minimum ();
        root = removeMin(root);
        return ret;
    }

    // delete the lowest node to the root node of the binary search tree
     // New after returning the deletion node binary search tree root 
    Private the Node removeMin (the Node node) {

        if(node.left == null){
            Node rightNode = node.right;
            node.right = null;
            size --;
            return rightNode;
        }

        node.left = removeMin(node.left);
        return node;
    }

    // delete the node from where the maximum value of the binary search tree 
    public E removeMax () {
        Entitled E = Maximum ();
        root = removeMax(root);
        return ret;
    }

    // delete the node to the root of the binary search tree, the largest node
     // After returning to delete the new node binary search tree roots 
    Private the Node removeMax (the Node node) {

        if(node.right == null){
            Node leftNode = node.left;
            node.left = null;
            size --;
            return leftNode;
        }

        node.right = removeMax(node.right);
        return node;
    }

 

Delete any node

  Delete any node of the situation is more complicated when there is about to be sub-tree abridged point is what elements of alternative locations truncated point of it? Here we use is truncated successor node point, that ratio is the lowest node point abridged right subtree, we first use the above minimum () method right subtree successor node to find out, then () method to remove a node and successor removeMin Right after the sub-tree node as a successor to delete assigned to the right child node of a tree successor, then the left subtree is left intact abridged point is assigned to the successor node as the left child node of a tree successor, and returns to the tree , truncated point blank about sub-tree remember, let jvm recovery.

 // remove elements from the binary search tree is a node e 
    public  void Remove (E e) {
        root = remove(root, e);
    }

    // delete the node to the root node of the binary search tree is e, the recursive algorithm
     // root node after returning to delete the new binary search tree 
    Private the Node Remove (the Node node, E e) {

        if( node == null )
            return null;

        if( e.compareTo(node.e) < 0 ){
            node.left = remove(node.left , e);
            return node;
        }
        else if(e.compareTo(node.e) > 0 ){
            node.right = remove(node.right, e);
            return node;
        }
        else {    // e.compareTo (node.e) == 0

            // case to be deleted node in the left subtree is empty 
            IF (node.left == null ) {
                Node rightNode = node.right;
                node.right = null;
                size --;
                return rightNode;
            }

            // where the right subtree node to be deleted is empty 
            IF (node.right == null ) {
                Node leftNode = node.left;
                node.left = null;
                size --;
                return leftNode;
            }

            // left and right sub-tree node to be deleted is not empty case

            // find to be deleted is larger than the lowest node, the lowest node i.e. node to be deleted right subtree
             // use this node to replace the node to be deleted position 
            the Node by successor = Minimum (node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;

            node.left = node.right = null;

            return successor;
        }
    }

Guess you like

Origin www.cnblogs.com/skychmz/p/12026317.html