Binary Search Tree
Binary Search Tree (BST) is also known as binary search tree and binary sorting tree.
1. Features
The binary search tree is a binary tree first;
For any node, if the left subtree is not empty, the value of any node on the left subtree is not greater than the value of its root node;
If the right subtree is not empty, the value of any node on the right subtree is not greater than the value of its root node;
The left and right subtree of any node is also a binary search tree.
2. Sequential Traversal
Traverse the binary search tree in the middle order and the result is an ordered data column.
// Traverse the binary search tree in the middle order. The result is the sorted data public void midorder (treenode node) {If (node = NULL) {return;} midorder (node. getleft (); system. out. print (node. getkey () + ""); midorder (node. getright ());}
3. Add nodes
New nodes added to the Binary Search Tree always become leaf nodes.
// Add a node to construct the public treenode addnode (treenode node, int key) of the Binary Search Tree {// if no node exists, create a node. Otherwise, if (node = NULL) is returned) {node = new treenode (key);} If (Key <node. getkey () {node. setleft (addnode (node. getleft (), key);} else if (Key> node. getkey () {node. setright (addnode (node. getright (), key);} else {return node ;}
4. Search nodes
The binary search tree needs to be traversed. the time complexity depends on the depth of the traversal.
// Find a node. the time complexity is the number of layers that have elapsed. The worst is O (h) Public treenode searchnode (treenode node, int key) {// if no node is found, null is returned, if (node = NULL | node. getkey () = Key) {return node;} else if (Key <node. getkey () {return searchnode (node. getleft (), key);} else {return searchnode (node. getright (), key );}}
5. delete a node
Data Structure _ tree _ Binary Search Tree