1. Definition
Binary sort tree, also known as a two-fork lookup (search) tree . It is defined as: a two-fork sort tree or an empty tree, or a two-fork tree that satisfies the following properties:
① if its left subtree is not empty, the values of all nodes on the left subtree are smaller than the value of the root node;
② if its right subtree is not empty, the values of all nodes on the right subtree are greater than the values of the root nodes;
③ the left and right sub-tree itself is a binary sorting tree.
These properties are called binary sort Tree Properties (BST property), so the binary sort tree is actually a two-fork tree satisfying the BST nature.
Attention:
When a linear table is used as the form of a table, there can be three methods of finding it. The two-point search is the most efficient. However, because the two-point lookup requires that the nodes in the table be ordered by the keyword, and the chain list cannot be used as the storage structure, when the table is inserted or deleted frequently, in order to maintain the order of the table, it is bound to move many nodes in the table. This extra time overhead caused by moving nodes offsets the advantage of binary lookups. In other words, a binary lookup is only available for static lookup tables. To efficiently find a dynamic lookup table, you can use the next two tree or tree as the organization of the table. You might want to refer to them collectively as a tree table.
2. Features
The properties of BST are:
(1) Any node in the binary sorting tree X, the key of any node Y (if present) in its left (right) subtree must be small (large) to the keyword of x.
(2) In binary sorting tree, the key words of each node are unique.
Note:
In practice, it is not guaranteed that the key words of each element in the data set being searched are different, so the "less" in the BST Nature (1) in the two-fork sort tree definition is changed to "greater than or equal", or the "greater" in the BST Nature (2) is changed to "less than equals" or even both.
(3) The middle order sequence which is obtained by traversing the tree in the middle order is an ascending order sequence.
The two trees shown in the example are two-fork sort trees, each of which has an ordered sequence: 2,3,4,5,7,8.
3. Storage structure
binary tree structure definition;
typedef struct bitnode{ int data; Node data struct Bitnode *lchild,*rchild;} Bitnode,*bitree;
Or as defined below:
typedef int KEYTYPE;//Assuming the keyword type is an integer typedef struct Node { KeyType key; //Key entry InfoType Otherinfo;// Other data fields , InfoType depending on the application, the following does not handle it struct node *lchild,*rchild; //Child pointer} bstnode;typedef Bstnode *bstree; Bstree is a two-fork sort tree Type
4, binary sorting tree operation(1) insertion and generation of binary sorting tree
① the process of inserting a binary sort tree into a new node
Insert a new node in the binary sort tree to ensure that the BST property is still satisfied after insertion. The insertion process is:
A, if the binary sort tree T is empty, then the keyword to be inserted key to apply for a new node, and make it the root;
b, if the binary sort tree T is not empty, then the key and the root of the keyword comparison:
(i) If the two are equal, the keyword key is already in the tree and does not need to be inserted.
(ii) If key<t→key, insert the key into the left subtree of the root.
(iii) If key>t→key, insert it into the right sub-tree of the root.
The insert procedure in the subtree is the same as the insert procedure in the preceding tree. This goes on until you insert key as a new leaf node in the two-fork sort tree, or until the keyword is already present in the discovery tree.
Recursive algorithm for inserting new nodes of ② binary sort tree
Non-recursive algorithm for inserting new nodes of ③ binary sort tree
void Insertbst (Bstree *tptr,keytype key) {//Jou fork Sort Tree *tptr no keyword is key, then insert, otherwise directly return Bstnode *f,*p=*tptr; The initial value of P points to the root node while (p) {//finds the insertion position if (P->key==key) the key is already present in the return;//tree, without inserting f=p;//f Save the node currently found p= (key <p->key)?p->lchild:p->rchild; //If key<p->key, look in the left subtree, otherwise look in the right subtree }//endwhile p= ( Bstnode *) malloc (sizeof (Bstnode)); p->key=key; p->lchild=p->rchild=null;//Generate new node if (*tptr== NULL)//The original tree is an empty *tptr=p;//The newly inserted node is the new root else// The original tree is non-empty when the new node off p as the left child or right child of off F is inserted if (Key<f->key) F >lchild=p; else f->rchild=p; }//insertbst
Generation of ④ binary sorting tree
The binary sort tree is generated from an empty two-fork sort tree, and each input node data is called once to insert the insertion algorithm into the currently generated two-fork sort tree. The algorithm for generating a two-fork sort tree is as follows:
Bstree createbst (void) {//Enter a sequence of nodes, establish a binary sort tree, return the root node pointer to bstree t=null;//initial T is empty tree KeyType key; scanf ( "%d", &key);//Read people a keyword while (key) {//Assume key=0 is the end of the Terminator sign insertbst (&t,key);//Insert key into the binary sort tree T scanf (" %d ", &key);//Read the next keyword } return T;//Returns the root pointer of the established two-fork sort tree }//bstree
Attention:
The input sequence determines the shape of the two-fork sort tree .
The middle order sequence of binary sort tree is an ordered sequence. So for an arbitrary keyword sequence to construct a binary sorting tree, its essence is to order this keyword sequence, so that it into an ordered sequence. This is also the name of the sort tree. This sort of sorting is often referred to as the tree sort, which proves that the average execution time for this sort is also O (NLGN).
For the same input instance, the execution time of the tree sort is approximately 2 to 3 times times the heap sort. So in general, the purpose of constructing a two-fork sort tree is not to sort, but to use it to speed up lookups, because finding on an ordered set is usually faster than finding it on an unordered collection. Therefore, people often call the two-fork sort tree A binary lookup tree.
(2) The deletion of binary sorting tree
Delete a node from the two-fork sorting tree, and you cannot remove the subtree with the root of the node, and also ensure that the resulting two-fork tree still satisfies the BST property.
① General steps for deleting operations
A, to find
When looking, p points to the node that is currently accessed, and the parent points to its parent (whose initial value is null). If the deleted node is not found in the tree, it will be returned, otherwise the deletion point is *p.
b, by deleting *p.
When deleting *p, the *p subtree (if any) should still be connected to the tree and the properties of BST remain unchanged. The number of children according to *p is dealt with in three different cases.
② three scenarios for deleting *P nodes
A, *p is a leaf (that is, it has a child number of 0)
You do not need to connect *p subtree, simply place *p's parent *parent in the pointer field that points to *p.
B, *p only one child *child
By simply connecting the parents of *child and *p, you can delete the *p.
Note:
*p can be either the left child of *parent or the right child, and *child may be the left child or right child of *p, so there are 4 states.
C, *p has two children
Shilling q=p, save the address of the deleted node in Q, and then find the middle order successor *p of *q, and still use the parent to remember *p's parent position in the search process. The middle order successor of the *q *p must be the leftmost node in the right subtree of the *q, which has no left sub-tree. Thus, it is possible to convert the deleted *q operation to the deleted *p operation, i.e. to copy the data to *q before releasing the node *p, which is equivalent to deleting the *q.
③ binary sorting tree deletion algorithm
Analysis:
All of the three cases can be unified to the situation (2), the algorithm only for the situation (2) processing can be.
Note the boundary condition: If the parent is empty, the deleted node *p is the root, so after deleting *p, the child should be placed as the root.
Algorithm:
void Delbstnode (Bstree *tptr,keytype key) {//In the binary sort tree *tptr, delete the node Bstnode *parent=null,*p=*tptr,*q,*child of the keyword key; while ( p) {//from the root to find the key keyword for the delete node if (P->key==key) break;//has been found, jump out of the lookup loop parent=p;//parent point to *p's parents p= (key<p->key)? p-& gt;lchild:p->rchild;//In the left or right subtree of the off P, continue to find if (!p) return,//cannot find the deleted node is returned q=p;//q remember the deleted node *p if (q->lchild&&q-> , Rchild)//*q two children are not empty, so find the *q of the middle order successor *p for (parent=q,p=q->rchild; p->lchild; parent=p,p=p=->lchild);//Current situation (3) has been converted to a condition (2), while the condition (1) corresponds to the condition (2) of Child=null in child= (p->lchild)? p->lchild:p->rchild;//if the situation (2), then child non-null Otherwise, the child is null if (!parent)//*p The parents are empty, the *p is the root, delete the *p should modify the root pointer *tptr=child;//If the case (1), delete the *p, the tree is empty; otherwise the child becomes the root else{//*p is not the root, the *p's child and *p's parents are connected, *p is removed from the tree if (p==parent->lchild)//*p is the left child of the parents parent->lchild=child;//*child as the left child of *parent else parent->rchild=child;//*child as the right child of the parent if (P!=Q)//Is the case (3), you need to copy the *p data to *q q->key=p->key;// If there are other data fields also need to copy}//endif free (p);/release *p accounted forThe space used}//delbstnode (3) Find on binary sort tree
① Lookup Recursive algorithm
Finding on a binary sort tree, similar to a binary lookup, is also a process of narrowing down the search scope.
Recursive lookup algorithm:
/* Find the node where the keyword is key on the binary sort tree T, return the node position on success, or return Null*/bstnode *searchbst (bstree t,keytype key) { if (t==null| | Key==t->key) //recursive end condition return T, //t empty, lookup failed, otherwise successful, return found node position if (key<t->key) return Searchbst (T->lchild,key), else return Searchbst (t->rchild,key); //Continue to find in the right subtree}
② Algorithm Analysis
When searching on a binary sort tree, if the lookup succeeds, a path from the root to the unknown Origin node is set off from the root node. If the lookup is unsuccessful, a path from the root to a leaf is set off from the root node.
A, Two fork sort tree find the average lookup length for success
Under the equal probability hypothesis, the average lookup length of the binary sort tree lookup in the following (a) figure is
Under the equal probability hypothesis, (b) The average lookup length of the tree shown in the figure when finding success is:
aslb= (1+2+3+4+5+6+7+8+9+10)/10=5.5
Attention:
Similar to binary lookup, the number of times the keyword is compared does not exceed the depth of the tree.
B, The average lookup length when searching on a binary sort tree is related to the morphology of the two-fork tree.
The binary lookup method finds an ordered table of length n, and its decision tree is unique. A two-fork sort tree with n nodes is inflexible. For tables with the same set of nodes, the shape and depth of the two-fork sort tree may vary due to the order in which the nodes are inserted.
The tree shown in "Example" (a) is formed in the following order of insertion:
45,24,55,12,37,53,60,28,40,70
(b) The tree shown is formed in the following order of insertion:
12,24,28,37,40,45,53,55,60,70
The average lookup length when searching on a binary sort tree is related to the morphology of the two-fork tree:
① in the worst case, the binary sort tree is generated by inserting an ordered table of N nodes sequentially, at which point the resulting two-fork sort tree is degenerated into a single tree with a depth of N, and its average lookup length is the same as the order lookup on a single-linked list, and is (n+1)/2.
② in the best case, the binary sort tree in the process of generation, the tree shape is more symmetrical, the final result is a shape and a binary search decision tree similar to the two-fork sorting tree, at this time its average lookup length is about LGN.
③ Insert, Delete, and find algorithms have a time complexity of O (LGN).
5. Comparison of binary sorting tree and binary search
In terms of average time performance, lookups on binary sort trees are similar to binary lookups.
In terms of maintaining the order of the table, the binary sort tree does not need to move the nodes, but only modifies the pointer to complete the INSERT and delete operations, and its average execution time is O (LGN), so it is more effective. The ordered table involved in the binary lookup is a vector, and if there is an operation to insert and delete nodes, the cost of maintaining the order of the table is O (n). When the ordered table is a static lookup table, it is advisable to use the vector as its storage structure, and the binary search to realize its lookup operation; if the order table dynamically finds tables, the binary sort tree should be chosen as its storage structure.
6. Balanced binary Tree
In order to ensure that the height of the binary sorting tree is LGN, the average time for basic operations such as INSERT, delete, and find that is implemented on the binary sort tree is O (LGN), and when inserting or deleting nodes into a tree, adjust the shape of the tree to preserve the "balance" of the tree. Make sure that both the BST nature and the height of the tree are O (LGN) in all cases, ensuring that the basic operations on the tree are at the worst-case time O (LGN).
Attention:
① Balanced binary trees (Balanced binary tree) refers to the same height of the left and right subtrees of any node in the tree.
② the height of the left and right subtrees of either node is the same (e.g. full two-tree), the two-fork tree is completely balanced. Usually, as long as the height of the two-fork tree is O (1gn), it can be considered as balanced.
The ③ balanced two-fork sort tree refers to a balanced binary tree satisfying the BST nature.
The absolute value of the difference in the height of the left and right subtrees of any node in the ④avl tree does not exceed 1. In the worst case scenario, the height of the AVL tree for n nodes is approximately 1.44lgn. And a perfectly balanced two-fork tree is about the Lgn,avl tree that is nearly optimal.
Binary sorting trees (binary sort tree)