Insertion of Binary Search Tree Nodes

First define a binary search tree, the Java code is expressed as follows:

public class BST<Key extends Comparable<Key>, Value> {     // The nodes in the tree are private classes, the outside world does not need to understand the specific implementation of binary search tree nodes     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;         }     }     // the root node     private Node root;     // the number of nodes in the tree     private int count;     // constructor, constructs an empty binary search tree by default     public BST() {         root = null;         count = 0;     }     // returns 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;     } }






Node represents the node, and count represents the number of nodes.

The following example inserts element 61 into the following binary search tree:

(1) The element 61 to be inserted is greater than 42, compare the root node of the right subtree of 42.

(2) 61 is larger than 59, so 61 needs to be moved to the corresponding position of the right subtree of 59, but it is empty at this time, and directly inserted as the right child node of 59.

The insertion operation is also a recursive process, divided into three cases, equal to, greater than, and less than.

Java example code

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

package runoob.binary;

/**
 * Binary search tree inserts new elements
 */

public class BinarySearchTreeInsert<Key extends Comparable<Key>, Value> {     // The nodes in the tree are private classes, and the outside world does not need to understand the binary search tree The specific implementation of the node     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;         }     }     private Node root; // Root node     private int count; // The number of nodes in the tree type     // Constructor, default to construct an empty binary search tree     public BinarySearchTreeInsert() {



















        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);     }     //Core code --- start     // 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;
    }
    //核心代码---结束
}

Guess you like

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