Home >Common Problem >The relationship between balanced binary trees and binary sorted trees
##1. Binary sorting treeBinary Sort Tree, also known asThere is no direct relationship between balanced binary trees and binary sorting trees, but the search efficiency of binary sorting trees is related to the shape of the binary tree. So when we want the shape of the binary sorting tree to be uniform, like this Binary trees are called balanced binary trees.
Binary Search Tree (Binary Search Tree), also known as Binary Search Tree.
Perform in-order traversal on the binary sorting tree to get a sorted by keyword Sequence, for example, an in-order traversal of the above figure can obtain an ordered sequence: 10, 42, 45, 55, 58, 63, 67, 70, 83, 90, 98
depth of the tree:
The depth of the left subtree of a binary tree node minus the depth of its right subtree is called balance factor BF. Then it is only possible to balance the balance factors of all nodes on the binary tree. They are -1, 0 and 1. The one on the left in the figure below is a balanced binary tree, and the one on the right is an unbalanced binary tree.
Because the difference between the depths of the left and right subtrees of any node on a balanced binary tree will not exceed 1, it can be proved that its depth is the same as the depth of a complete binary tree with n nodes 1 is of the same order of magnitude. Therefore, its average number of searches is also . The minimum balanced subtree refers to the subtree AVL tree which is the node closest to the inserted node and whose absolute value of the balance factor is greater than 1 as the root node.
Note LL Type, rotate with the middle node as the axis. Why here I is the left child of BL cannot use B-BL-I as the LL type, because the A node is the closest balance to the I node For subtrees with factor absolute value > 1, the absolute values of balance factors of other nodes do not exceed 1; similarly, when I is the right child of BL, B-BL-I cannot be regarded as LR type .
2.
One-way left rotation (RR type): Insert the right subtree whose position is the right subtree, and the right subtree is the axis, and perform a single Rotate to the left
3.
Bidirectional rotation first left and then right (LR type): insert the right subtree whose position is the left subtree, and perform two rotations, first to the left and then to the right.
cannot use B-C-I as a subtree to define it as RL type. The principle is the same as the explanation in RR type. For LR type, the R end or L is close to the inserted node. The end is used as the axis of rotation (as shown in the figure below, it is equivalent to first rotating the subtree with B as the root to become an LL shape, and then rotating the subtree with A as the root).
Insert the node to the right Child:
4.
Two-way rotation first right and then left (RL type): Insert the left subtree whose position is the right subtree, make two adjustments, first rotate right and then rotate left; The processing situation is similar to LR.
Insert the node as the right child:
It is necessary to use the node closest to the inserted node and the absolute value of the balance factor > 1 as the subtree of the root node to determine which type it is .
After inserting 8 and 6 in sequence, the absolute value of the balance factor of node 5 is >1, becoming an RL type, so first take 5 as the root node and right-rotate its subtree 8-6 (becoming RR type), and then rotate the entire tree with 5 as the root node left-rotated.
After continuing to insert node 9, the balance factor of node 4 is > 1, becoming the RR type, so 4 is the root node , turn the whole thing to the left.
The above is the detailed content of The relationship between balanced binary trees and binary sorted trees. For more information, please follow other related articles on the PHP Chinese website!