Insertion of binary search tree nodes (Java example code)

Table of contents

 

Insertion of Binary Search Tree Nodes

Java example code

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


 

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:

 

d3976baf770aee333af91838b6012acc.png

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

 

073c44a6f4adc17e5ebbd9076080701b.png

(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.

 

930606aa28b05164ad0f51e79f2ae5ac.png

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

Java example code

Source package download: Download https://www.runoob.com/wp-content/uploads/2020/09/runoob-algorithm-BinarySearchTreeInsert.zip

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, construct an empty binary search tree by default     public BinarySearchTreeInsert() {         root = null;




















        count = 0;
    }

    // returns the number of nodes in the binary search tree
    public int size() {         return count;     }     // returns whether the binary search tree is empty     public boolean isEmpty() {         return count == 0;     }     // to 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     // to In the binary search tree with node as the root, insert the node (key, value), use the recursive algorithm     // 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/2301_78835635/article/details/132296523