If its left subtree is not empty, the value of all nodes in the left subtree is less than the value of its root structure.
If its right subtree is not empty, the value of all nodes in the right subtree is greater than the value of its root structure.
Its left and right subtrees are also binary sort trees (recursion).
Note: elements in the tree are repeatable, that means, they can be changed to >= or <=.
Why is it called a binary sort tree?
Because the sequence in BST is an ordered sequence, the construction of a binary sort tree for an arbitrary keyword sequence is to sort it and make it into an ordered sequence
source code
publicclassBst{staticclassBSTNode{// this is a internal classprivateint key;private BSTNode lc = null;private BSTNode rc = null;}publicstaticvoidmain(String[] args){int[] a =newint[]{2,3,54,1,5,0,5,3,2,1};
BSTNode b =CreateBST(a);DispBST(b);InsertBST(b,7);
System.out.println();DispBST(b);
System.out.println();InOrder(b);//InOrder traversal}publicstatic BSTNode CreateBST(int[] a){
BSTNode bst = null;for(int i =0; i < a.length; i++){
bst =InsertBST(bst, a[i]);}return bst;}publicstatic BSTNode InsertBST(BSTNode b,int k){// Insert a data in bstif(b == null){
b =newBSTNode();
b.key = k;
b.lc = null;
b.rc = null;return b;}if(b.key > k) b.lc =InsertBST(b.lc, k);else b.rc =InsertBST(b.rc, k);return b;}publicstaticvoidDispBST(BSTNode bstNode){//parenthesized notationif(bstNode != null){
System.out.print(bstNode.key);if(bstNode.rc != null || bstNode.lc != null){
System.out.print("(");DispBST(bstNode.lc);if(bstNode.rc != null) System.out.print(",");DispBST(bstNode.rc);
System.out.print(")");}}}publicstaticvoidInOrder(BSTNode bstNode){//inorder traversalif(bstNode != null){InOrder(bstNode.lc);
System.out.print(bstNode.key +" ");InOrder(bstNode.rc);}}
BST is more efficient in terms of maintaining the orderliness of the table, because you don’t have to move the elements, just modify the pointer to complete the insertion and deletion of nodes The average execution time is
O
(
l
o
g
2
n
)
O(log_2n)
O(log2n)
Why is it called a binary sort tree?- Because the sequence in BST is an ordered sequence, the construction of a binary sort tree for an arbitrary keyword sequence is to sort it and make it into an ordered sequence