Article directory
0 Search in tree table
The previous blog recorded binary search, but binary search requires ordering to be completed. Sometimes non-ordered ones are not convenient, so you can use dynamic lookup tables, which are tree tables.
1 Binary sorting tree
Binary Sort Tree (Binary Sort Tree) is also called binary search tree and binary search tree.
Definition:
A binary sorting tree is either an empty tree or a binary tree that satisfies the following properties:
- If its left subtree is not empty, the values of all nodes on the left subtree are less than the value of the root node
- If its right subtree is not empty, then the values of all nodes on the right subtree are greater than or equal to the value of the root node.
- The left and right subtrees themselves are each a binary sorting tree.
The left is small and the right is large, left < root < right
The first picture is a binary sorting tree. The values of the nodes on the left subtree are all less than the root node 45, and the nodes on the right subtree are all greater than the root node 45. In the second picture, there is no left subtree, and the first letters of all words in the right subtree are greater than or equal to the root node, so it is also a binary sorting tree. In the third and fourth examples, because the following node is larger than the root node, the condition is not met.
The sequence of data elements obtained by in-order traversal of a non-empty binary sorted tree is an ascending ordered sequence arranged by keyword .
1.1 Operation of binary sorting tree
If the object you need to find is smaller than the current root node, you only need to search the left subtree. If it is larger than the root node, go to the right subtree to find it. If it is equal to the root node, you will directly return to the root node.
1.1.1 Storage of binary sorting tree
typedef struct
{
KeyType key; //关键字项
InfoType otherinfo; //其他数据域
}ElemType;
typedef struct BSTNode
{
ElemType data; //数据域
struct BSTNode *lchild, *rchild; //左孩子右孩子指针
}BSTNode, *BSTree;
BSTree T;//定义二叉排序树
1.1.2 Recursive search of binary sorting tree
The left and right subtrees call their own algorithms to search.
BSTree SearchBST(BSTree T, KeyType key)
{
if((!T)||key == T->data.key)
{
return T;
}
else if(key < T->data.key)
{
return SearchBST(T->lchild, key);//在左子树中继续查找
}
else
{
return SearchBST(T->rchild, key);//在右子树中继续查找
}
}
1.1.3 Insertion into binary sorting tree
The node to be inserted is smaller than the root node, so it is placed in the left child, and then compared with the root node in the left child. If it is larger than the root node, put the right child and continue the comparison.
Thought
The inserted node must be on a leaf node.
1.1.4 Generation of binary sorting tree
An unordered sequence can be turned into an ordered sequence by constructing a binary sorting tree. The process of constructing the tree is the process of sorting the unordered sequence (in-order traversal).
The inserted nodes are all leaf nodes, so there is no need to move other nodes. Equivalent to inserting records in an ordered sequence without moving other records.
However:
the input order of keywords is different, and different binary sorting trees are established.
If the form is different, the search efficiency will be different.
1.1.5 Deletion of binary sorting tree
The most important ones are the last two.
If you delete a leaf node, delete it directly.
Delete 20 and 88 directly, and modify the left and right children of the parents.
Change the value of the corresponding pointer field in its parent node to "null" .
The deleted node has only the left subtree or only the right subtree. Replace it with its left subtree or right subtree (node replacement) and directly modify the
parent pointer so that the parents point directly to the left and right children of the deleted node.
The deleted node has both a left subtree and a right subtree. In
this case, we still need to keep the in-order traversal in order. What should we do?
One way is to replace it with the predecessor node of the in-order traversal . This still maintains the in-order traversal, so we find its left subtree and replace it with the closest node. Here, 40 is used instead of 50.
Of course, you can also replace it with its successor and then delete the successor node. The successor is the smallest node in the right subtree . Here, 80 is replaced with 50. After 80 becomes the root node, a line directly points to 90.
Some examples.
1.2 Search performance analysis of binary sorting tree
The number of times each node needs to be searched depends on the level of the binary sorting tree where the node is located. The process of finding a node on a binary sorting tree where a certain keyword is equal to a given value is actually taking a path from the root to the node.
To sum up, it can be concluded that the number of keyword comparisons = the number of levels where this node is located .
As for the number of layers, it is best not to exceed log 2 n + 1 log_2n+1log2n+Level 1 , the worst case scenario isnnLayer n is the situation in the picture on the right.
When the number of layers is the largest, ASL is the same as when searching sequentially.
2 Balanced binary tree (AVL tree)
Question: How to improve the search efficiency of binary sorting trees with imbalanced shapes?
Solution: Do "balancing" processing, that is, try to make the shape of the binary tree as balanced as possible! This is where balanced binary trees come from.
For convenience, a number is appended to each node, giving the height difference between the left subtree and the right subtree of the node . This number is called the node's balance factor (BF) .
Balance factor = height of the left subtree of the node - height of the right subtree of the node
According to the definition of a balanced binary tree, the balance factors of all nodes on a balanced binary tree can only be -1,0,1.
The absolute value is less than or equal to 1 and not more than 1.
For a tree with nnThe height of an AVL tree with n nodes remains atO (log 2 n) O(log_2n)O(log2n ) order of magnitude, ASL also remains atO (log 2 n) O(log_2n)O(log2n ) magnitude.
2.1 Analysis and adjustment of unbalanced binary sorting tree
If an imbalance is caused by inserting a new node into an AVL tree, the tree structure must be readjusted to restore balance. (Node 53 in the picture above is unbalanced)
If it is found that after inserting a node, more than one unbalanced node is found, the root node of the minimum unbalanced subtree is found as the unbalanced node.
Adjustment principles:
- lower the altitude
- Maintain the binary sorting tree properties (the left subtree is smaller than the root node, and the right subtree is larger than the root node), so the smallest one is placed on the far left.
2.1.1 LL type adjustment
The LL type is to insert a new node. The insertion position of this new node is above the left subtree of the left subtree of the unbalanced node.
Adjustment:
Lower node A and raise node B.
Or use the balanced rotation method, just turn it down.
2.1.2 RR type adjustment
The RR type is to insert a new node. The insertion position of this new node is above the right subtree of the right subtree of the unbalanced node.
The idea is to lower the height.
Example:
Answer:
2.1.3 LR type adjustment
The LR type is to insert a new node. The insertion position of this new node is above the right subtree of the left subtree of the unbalanced node.
Adjustments should still be made in accordance with the principles.
The third step is placed like this to maintain the characteristics of the AVL tree.
2.1.4 RL type adjustment
The RL type is to insert a new node. The insertion position of this new node is above the left subtree of the right subtree of the unbalanced node.
Similar to LR tree.
3 Construct an AVL tree based on the above types
When 7 is inserted, it is found that it is of LR type, so it is adjusted according to the LR type, that is, the 7 node goes up, and then 3 and 16 are used as its left subtree and right subtree respectively.
At this time, the LL type appears again. The middle node 11 is directly raised, and then one is used as its left child and the other is used as its right child.
After inserting node 26, it was found that there was an RR imbalance.
Then there was another RL imbalance.
RL and LR are to lift the middle value, and then the other two are used as the left subtree and the right subtree.
If 3 vertices are out of balance, adjust the smallest subtree. , that is, marked in red, this is an LR type imbalance.
At this point, the AVL tree is constructed.