Balanced binary trees (AVL tree)

Source: Internet
Author: User

In the process of learning algorithms, the binary balance tree is bound to encounter, this blog post as concise as possible to introduce the relevant concepts of the two-fork tree, and then focus on what the balance of binary tree.

( because the drawing time ignores the arrow problem, the normal tree does not have the arrow, although does not affect the description of the process, but still need to pay attention to, so also ask the reader to ignore some of the diagram of the arrow )

One or two fork (find) tree

Binary search tree is a two-fork tree whose tree node (internal nodes of the trees) stores key values and satisfies the following characteristics, as shown in a:

    • Suppose you, V, R are three nodes of the tree (nodes), R is the root node of the tree, U is the left subtree of the root, and V is the right subtree of the root node;
    • Key value size relationship: Key(U) < key (R) < key (v), that is, at the root node (or the parent node) all nodes of the left subtree are smaller than the root or parent nodes, while the nodes in the right sub-tree are greater than the root or parent node;
    • The external nodes of the tree do not store any information.

Figure A two fork find tree

Two or two fork find tree operations

2.1 Find (search)

To find an element K in a binary tree, we look for the corresponding node from the root node toward the tree structure, and the direction of the node we are looking for depends on the comparison of the current node with the value of the node you are looking for. Based on figure A, assuming that the node we are looking for is 7, then from the root node we can know 7 < 8, then go down toward the subtree of the node value 6, and then we find 6 < 7 so at this point we are looking for the right subtree of node 6, when we find 7 = 7, that is, to reach the node we are looking for, B is the process of finding the node:

Figure B Two cross-tree lookup process

Algorithm pseudo-code:

Bstreesearch (k, v): if T.isexternal (v):    return Velif K < key (v):    return Bstreesearch (k, T.left (v)) elif k = = key (v):    return velse k > key (v):    return Bstreesearch (k, T.right (v))

2.2 Insertion (insertion)

If you want to perform an insert operation, we first need to find the tree, locate the leaf node, and then perform the insert operation. Below to insert the 5-bit example to execute, if you want to insert 5, perform the 2.1 section of the two-tree search operation, we can find 5 < 8,5 < 6,5 > 4, we can find that the final 5 is greater than 4, then we need to 4 of the right leaf node inserted 5, and extends the leaf nodes of the newly inserted node (5). The specific operation is shown in C:

Figure C Two fork tree insert operation

2.3 Delete (deletion)

The corresponding delete operation, we need to find the corresponding node, and then remove the node from the two-fork tree (remove), the actual code implementation needs to determine whether the key value of the node to be deleted exists in the binary tree, in addition, if the node is accompanied by a leaf node, Then you need to remove the node and the leaf node together. There is, of course, another case where the left and right subtrees of the removed node are internal nodes (internal nodes), and this time the operation will be slightly more complicated, here are examples of deleting 4 and 6 to illustrate the two cases, the specific operation D (based on Figure C operation) is shown:

Figure D Delete operation

The deletion of Node 4 is relatively simple, only need to remove the node and the corresponding leaf nodes, but instead of deleting 6, we need to ensure that all parent nodes of the left subtree is smaller than the node, and the right subtree is greater than the parent node, so when we delete 6, We need to move the 5 to the appropriate position and fill in the corresponding leaf nodes with new leaves.

2.4 Algorithm Performance (performance)

Suppose the height of a binary tree is H, the worst operation is O (n), and the best execution is O (log (n)).

Three or two fork balance tree (AVL tree)

3.1 Definition of the balanced binary tree

Binary balance tree refers to either it is an empty tree itself, or it is a Saozi right subtree of the depth of the difference of the absolute value is not greater than 1, and to ensure that the left and the sub-tree are balanced tree, figure E is a balanced binary tree. As we can see, the height of a node 1 indicates the height of the leaf node to the parent node, and the height of the whole tree depends on the distance from the deepest leaf node to the root node.

Figure E Balanced binary tree

3.2 Operation of the Balanced binary tree

The search operation of the AVL tree is basically the same as that of the normal two-fork tree, but the insert and delete operations are different, because insertions and deletions reduce the tree's nodes and change the structure of the tree, and this time we need to refactor the tree so that it is always balanced in order to keep the tree in balance. Generally this operation is called rotation operation (rotation), rotation is divided into left and right rotation, and so on, the following to see the insert and delete operations and how to use rotation to make the two-fork balance tree after inserting and deleting a node remains balanced.

3.2.1 Rotation operation

In this section of the main introduction to the left and right rotation, rotation operation is not limited to these two, but the rationale is the same, the ultimate goal is to let the two-fork balance tree after being manipulated to achieve balance again.

Figure F Left rotation

In the said left rotation operation, we assume that x < Y < Z, because the tree is unbalanced, we perform the left rotation, the X and its left subtree to rotate left, and the original Y left subtree into the right subtree of x, here need to note two points, ① is we need to find to three points, The size of these three points is ordered, such as this paragraph at the beginning of the XYZ relationship, the middle value as the new center node, and then the rotation operation, ② is to ensure that all the left and right subtree follow the definition of binary tree requirements, both the left subtree must always be smaller than its parent node, The right subtree is always greater than the parent node.

Figure G Right rotation

The relationship of the three nodes described in Figure G is Z < x < Y, so according to the left rotation we can know that X should be the center node that is the parent node, and then it needs to be rotated two times to make the two fork tree finally balance, first of all z to the left rotation, z into the left subtree of x, Then the y is rotated right, and in this process, the left subtree of x becomes the right subtree of z, and the right subtree becomes the left subtree of y. With the basic two operations, we then take a look at the actual example to perform the insert and delete operations on the balanced binary tree and combine the rotation to achieve a balanced state.

3.2.2 Insert Operation

The insert operation of the balanced binary tree is the same as the normal binary search tree, where the newly inserted nodes occur at the leaf node, the only difference being that the newly inserted node causes the structure of the tree to change, resulting in an imbalance, which needs to be rotated to achieve balance. A new node is inserted on the basis of Figure e, and the value of the node is 40, as shown in the newly obtained figure H:

Figure h Inserting a new node key (40)

At this time we will find that the two fork tree is not balanced, then we need to find the tree inside causes the tree imbalance of the three points, the corresponding operation, the concrete has the following two steps: ① node 39 with node 42 for the parent node to the left rotation, at this point 40 becomes 39 of the right node, and 33, 39,40 together becomes the left subtree of the closing point 42. ② the node 53 to the right, turning it into the right sub-tree of nodes 42, and node 55 is still the right subtree of knot 53. This completes the reconstruction of the whole tree and keeps the new tree in balance. The reconstructed tree looks like I:

Figure I refactor the resulting tree

3.2.3 Delete operation

Assuming that node 22 is deleted on the basis of figure I, then at this point we can conveniently find the first junction that causes the unbalanced node to be 25 with the maximum height value, and then to the right subtree to find the second node with a maximum height value, which is 42 (the node with the red border).

Diagram J Delete after the refactoring flowchart

3.2. Algorithm representation of 42-fork Balance Tree

The algorithm performance of binary balance tree is mainly embodied in the following aspects:

    • If the run time required for the individual refactoring is O (1)
    • O (log (n)) if a node in the binary balance tree is found
    • The insert operation is O (log (n)) and if refactoring is required, the time required to maintain the balance is O (log (n))
    • The delete operation is O (log (n)) and if refactoring is required, the time required to maintain the balance is O (log (n))

The above is about the binary tree and two fork balance tree related knowledge points, if there is any wrong, please also point out the reader, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.