Binary search tree node lookup

The binary search tree has no subscript, so for the search operation of the binary search tree, a contain method is defined here to determine whether the binary search tree contains an element and return a Boolean variable. This search operation is also a recursive process, specifically The code is implemented as follows:

...
// Check if the binary search tree rooted at node contains a node whose key value is key, using a recursive algorithm
private boolean contain(Node node, Key key){     if( node == null )         return false;     if( key.compareTo(node.key) == 0 )         return true;     else if( key.compareTo(node.key) < 0 )         return contain( node.left , key );     else // key > node->key         return contain ( node.right , key ); } ...











The following example finds 43 elements in a binary search tree

(1)  The element 43 is larger than the root node 42, and the comparison needs to be continued at the right child node.

(2)  Element 43 is smaller than 59, and the comparison needs to be continued at the left child node.

(3)  Element 43 is smaller than 51, and the comparison needs to be continued at the left child node.

(4)  Find the left child node 43 of 51, exactly equal to the sum, and end.

If you need to find the value corresponding to the key, the code is as follows:

...
// Find the value corresponding to the key in the binary search tree rooted at node, recursive algorithm
// If the value does not exist, return NULL
private Value search(Node node, Key key){     if( node == null )         return null;     if( key.compareTo(node.key) == 0 )         return node.value;     else if( key.compareTo(node.key) < 0 )         return search( node.left , key );     else / /key > node->key         return search( node.right, key ); } ...











Java example code

src/runoob/binary/BinarySearchTreeSearch.java file code:

package runoob.binary;

/**
 * binary search tree search
 */
public class BinarySearchTreeSearch<Key extends Comparable<Key>, Value> {     // The nodes in the tree are private classes, and the outside world does not need to know the details of the binary search tree nodes Implement     private class Node {         private Key key;         private Value value;         private Node left, right;         public Node(Key key, Value value) {             this.key = key;             this.value = value;             left = right = null;         }     }     / / Root node     private Node root;     // The number of nodes in the tree     private int count;     // Constructor, construct an empty binary search tree by default     public BinarySearchTreeSearch() {



















        root = null;
        count = 0;
    }

    // Return the number of nodes in the binary search tree
    public int size() {         return count;     }     // Return whether the binary search tree is empty     public boolean isEmpty() {         return count == 0;     }     // Insert a new (key, value) data pair into the binary search tree     public void insert(Key key, Value value){         root = insert(root, key, value);     }     // Check whether the binary search tree is There is a key key     public boolean contain(Key key){         return contain(root, key);     }     // Search the value corresponding to the key key in the binary search tree. If the value does not exist, return null     public Value search(Key key){         return search( root , key );





















    }


    //********************
    //* Auxiliary function for binary search tree
    //**************** ****

    // Insert a node (key, value) into the binary search tree rooted at node, and use a recursive algorithm
    // to return the root of the binary search tree after inserting the new node
    private Node insert(Node node, Key key , Value value){         if( node == null ){             count ++;             return new Node(key, value);         }         if( key.compareTo(node.key) == 0 )             node.value = value;         else if( key.compareTo(node.key) < 0 )             node.left = insert( node.left , key, value);         else // key > node->key             node.right = insert( node.right, key, value);         return node;














    }

    // Check whether the binary search tree rooted at node contains a node whose key value is key, using a recursive algorithm
    private boolean contain(Node node, Key key){         if( node == null )             return false;         if( key. compareTo(node.key) == 0 )             return true;         else if( key.compareTo(node.key) < 0 )             return contain( node.left , key );         else // key > node->key             return contain( node .right , key );     }     // Find the value corresponding to the key in the binary search tree rooted at node, recursive algorithm     // If the value does not exist, return NULL     private Value search(Node node, Key key){         if ( node == null )             return null;         if( key. compareTo(node. key) == 0 )




















            return node.value;
        else if( key.compareTo(node.key) < 0 )
            return search( node.left , key );
        else // key > node->key
            return search( node.right, key );
    }
}

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/131320464