1. Concepts of binary sorting tree:
Binary sorting tree is a dynamic tree table.
Binary Decision tree is defined as a binary decision tree or an empty decision tree,
Or a binary tree with the following properties:
(1) if its left subtree is not empty, the value of all nodes on the left subtree is smaller than the value of the root node;
(2) if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of the root node;
(3) The left and right sub-trees are a binary sorting tree. The nature of the binary sorting tree: traverses the binary sorting tree in the middle order, and the obtained Central order traversal sequence is an incremental order sequence.
2. Insert the binary sorting tree:
To insert a new node into the binary tree, make sure that the inserted binary tree still complies with the binary tree definition.
Insert Process: If the binary sorting tree is empty, the node to be inserted * s is inserted as the root node into the empty tree;
If it is not empty, compare the keywords-> key and the keywordt-> key of the node to be inserted,
If S-> key = T-> key, you do not need to insert it. If S-> key <t-> key, it is inserted into the left subtree of the root,
If S-> key> T-> key, insert it to the right subtree of the root. The Insert Process in the subtree is the same as that in the tree,
Continue until the node * s is inserted into the binary sorting tree as a new leaf, or until the same keyword node is found in the tree.
3. Binary sorting tree generation:
Starting from an empty binary sorting tree, a binary sorting tree is generated after a series of search and insert operations.
Note:
① Each inserted new node is a new leaf node in the binary sorting tree.
② The keyword sequence in different order will produce different binary sorting trees.
③ Construct a binary sorting tree for a random keyword sequence, and sort the keyword in essence.
4. Implement the binary sorting tree search program:
# Include <malloc. h>
# Include <iostream. h>
# Include <stdio. h>
Typedef struct bitnode {
Int data;
Int flag;
Struct bitnode * lchild, * rchild;
} Btnode, btree;
// Non-recursive search algorithm for the binary sorting tree
// Search for the key element in the binary sorting tree T. If the key is found, true is returned,
// Otherwise, false is returned.
Bool searchbst (btree * t, int key ){
Btree * P = T;
While (p ){
If (p-> DATA = key)
Return true;
P = (Key <p-> data )? P-> lchild: p-> rchild;
}
Return false;
}
// Recursive search algorithm for Binary sorting tree
// Search for the key element in the binary sorting tree T. If the key is found, true is returned,
// Otherwise, false is returned.
Bool searchbst2 (btree * t, int key ){
Btree * P = T;
If (! P)
Return false;
Else
If (p-> DATA = key)
Return true;
Else
If (Key> P-> data)
Return searchbst2 (p-> rchild, key );
Else
Return searchbst2 (p-> lchild, key );
}
// Create a binary sorting tree
// When no key element exists in the binary sorting tree T, the key is inserted and the root of the tree is returned,
// Otherwise, no insert is performed and the root of the tree is returned.
Btree * insertbst (btree * t, int key ){
Btree * f = T, * P = T;
While (p ){
If (p-> DATA = Key) return T;
F = P; // use F to write down the last vertex in the search path
P = (Key <p-> data )? P-> lchild: p-> rchild;
}
P = (btnode *) malloc (sizeof (btnode ));
P-> DATA = key;
P-> lchild = p-> rchild = NULL;
If (t = NULL)
T = P;
Else
If (Key <F-> data)
F-> lchild = P;
Else
F-> rchild = P;
Return T;
}
// Recursive sequential Traversal
Void inorderdisplay (btree * t ){
If (t ){
Inorderdisplay (t-> lchild );
Cout <t-> data;
Inorderdisplay (t-> rchild );
}
}
// Test:
Int main (){
Int I;
Int data;
Btree * tree = NULL;
For (I = 0; I <4; I ++ ){
Cout <"input data" <Endl;
Cin> data;
Tree = insertbst (tree, data );
}
Inorderdisplay (tree );
Bool find = searchbst2 (tree, 24 );
Cout <find <Endl;
Return 0;
}
5. Delete the binary sorting tree:
If the deleted node is * P and its parent is * F, but not general, Set * P to the left child of * F. The following three cases are discussed:
(1) If node * P is a leaf node, you only need to change the pointer of its parent node * F.
(2) If * P has only the left subtree PL or only the right subtree PR, you only need to make PL or PR the left subtree of its parent node.
(3) If the left and right subtree of node * P are not empty, first, locate the forward node of * P (note that * s is the rightmost node in the left subtree of * P, and its right link field is empty ), there are two methods:
① Link the left subtree of * P directly to the left trace of * P's parent node * F, the right subtree of * P is linked to the right link of * P's central forward node * s.
② Replace * P with the forward node of * P (that is, copy * s data to * P ), link the left subtree of * s to the left (or right) link of * s's parent node * q. (This method is used in the following demo algorithm)
6. demo of deleting an algorithm:
// Delete a node in the binary sorting tree
// Delete the key node in the binary sorting tree T
Void delbst (btree * t, int key ){
Btree * P = T, * F, * q, * s, * root = T;
While (p ){
If (p-> DATA = Key) break; // locate the node where keyword is key
F = P; // write down the parent node of the keywordkey Node
P = (Key <p-> data )? P-> lchild: p-> rchild; // search in the left and right subtree of * P respectively.
}
If (! P) return; // No key node in the binary sorting tree
If (p-> lchild = NULL & P-> rchild = NULL) {// P No left and right subtree
If (P = T) t = NULL; // The root node is deleted.
Else
If (P = f-> lchild)
F-> lchild = NULL;
Else
F-> rchild = NULL;
Free (P );
}
Else if (p-> lchild = NULL & P-> rchild! = NULL) // p no left subtree has right subtree
{
If (F-> lchild = P)
F-> lchild = p-> rchild; // link the right subtree of P to the left link of its parent node.
Else
F-> rchild = p-> rchild; // link the right subtree of P to the right link of its parent node
Free (P );
}
Else if (p-> rchild = NULL & P-> lchild! = NULL) // P has left subtree and right subtree
{
If (F-> lchild = P)
F-> lchild = p-> lchild; // link the left subtree of P to the left subtree of its parent node.
Else
F-> rchild = p-> lchild; // link the left subtree of P to the right link of its parent node.
Free (P );
}
Else if (p-> lchild! = NULL & P-> rchild! = NULL) // P has both the left subtree and the right subtree
{
Q = P;
S = p-> lchild; // turn left
While (S-> rchild) {// then right to the end
Q = s;
S = s-> rchild; // s points to the forward of the deleted vertices (forward in the middle order)
}
P-> DATA = s-> data; // replace P with P in the forward node s of P (that is, copy s data to P)
If (Q! = P) q-> rchild = s-> lchild; // re-connect the right subtree of Q
Else Q-> lchild = s-> lchild; // re-connect the left subtree of Q.
Free (s );
}
}
7. Search for the binary sorting tree:
The process of searching in the binary sorting tree is similar to that in binary search. It is also a process of gradually narrowing the search scope. If the search is successful, a path is taken from the root node to the node to be queried. If the search fails, the path from a root node to a leaf node is taken. Therefore, the number of times in the search process and the keyword ratio does not exceed the depth of the tree.
Because the binary sorting Tree Containing N nodes is not unique, the form and depth may be different. Therefore, the average length of the binary sorting Tree Containing N nodes is related to the tree structure.
The best case is that the Binary Decision Tree and the binary decision tree form the same.
The worst case is that the binary sorting tree is a single decision tree, and the average length of the query is the same as that of the sequential query.
Worst case demo
In terms of average performance, the search on the binary sorting tree is not much different from the binary search, and the insertion and deletion nodes on the binary sorting tree are very convenient, so there is no need to move a large number of nodes.