AVL Balance Tree

Source: Internet
Author: User

AVL is a balanced binary tree that rotates the binary search tree by rotating the nodes in the two-fork search tree to achieve a balance. AVL has the highest balance in all balanced binary search trees.

definition

A balanced binary tree or an empty tree or a two-fork search tree that satisfies the following properties:

  1. The difference in the height of the left and right subtrees is no more than 1
  2. The left and right sub-trees are still balanced binary trees

Defines the Zuozi height of the balance factor BF (x) = x-the height of the right subtree of x. The balance factor for each node of the balanced binary tree can only be 1, 0, 1.

maintaining the balance of thought

If the binary tree is currently in equilibrium, inserting/Deleting a new node at this time, it is possible to cause the two fork tree to not meet the equilibrium condition, it is necessary to rotate the nodes so that the binary tree reaches a balanced state. From the inserted/deleted node, start looking up, find the first one not satisfied | Bf| <= 1 's ancestor node p, at which point the child nodes of P and P (or child nodes of P's child nodes) are rotated (possibly divided into the following four cases).

Rotate

(1) ll-Type balanced rotation method
Because the node f is inserted on the left child B of a, the balance factor of a increases from 1 to 2 and loses balance. Therefore, a clockwise rotation operation is required. The left child B of a will rotate to the right to replace a as the root node, and a to the right down to the root node of the right subtree of B. and the right sub-tree of B turns into a's left subtree.

(2) RR type Balanced rotation method
Because the node f is inserted in right child C of a, the balance factor of a is reduced from 1 to 2. Therefore, a counterclockwise rotation operation is required. The right child c of a will rotate to the left up instead of a as the root node, and a to the left is the root node of the left subtree of c. The original C's left subtree becomes the right subtree of a.

(3) LR-Type balanced rotation method
Due to the insertion of node F on the right number of the left child B of a, the balance factor of a increases from 1 to 2 and loses balance. It is necessary to perform two rotation operations (first counterclockwise, then clockwise). That is, first, the right subtree of the left child B of the A node , the root node D to the left up to the position of the B node, and then the D node to the right up to the point of rotation to the position of a node. That is, first make it a ll type, and then the LL-type treatment.

, that is, the circle is first adjusted to the balance tree, and then to the root node to the left of a subtree, at this time become the ll type, and then by the LL-type treatment into a balanced type.

(4) RL type Balanced Rotation method
Because the node f is inserted on the left subtree of right child c of a, the balance factor of a is reduced from 1 to 2. It is necessary to perform two rotations (clockwise, counterclockwise), i.e. the root node d of the right child C of the a node is lifted to the right up to the C node position, and then the D node is rotated to the left up to the position of a node. The first is to make it a RR type, and then the RR type processing.

, that is, the circle is first adjusted to the balance tree, and then to the root node to the left of a subtree, at this time become RR type, and then processed into a balanced type RR.

Implementation (c + +)
#include <iostream>using namespace std; #define MAX (A, b) a > B? A:bstruct treenode{int data; treenode* child[2];int size;int height;int count; TreeNode (int val) {data = Val;size = Count = height = 1;child[0] = child[1] = NULL;} void Update () {size = Count;height = 1;if (child[0]) {size + = Child[0]->size;height = MAX (height, 1 + child[0]->height );} if (Child[1]) {size + = Child[1]->size;height = MAX (height, 1 + child[1]->height);}}; struct avl{treenode* root; AVL (): root (NULL) {};int getheight (treenode* node) {if (!node) return 0;return Node->height;} int GetSize (treenode* node) {if (!node) return 0;return node->size;} Note that the reference to the pointer using void Rotate (treenode*& node, int dir) {treenode* ch = node->child[dir];node->child[dir] = ch-> Child[!dir];ch->child[!dir] = node;node->update (); node = ch;} void maintain (treenode*& node) {if (!node) {return;} int BF = GetHeight (node->child[0])-getheight (node->child[1]); if (BF >=-1 && BF <= 1) {return;} if (BF = = 2) {int bf2 = getheight (node->child[0]->child[0])-getheight (node->child[0]->child[1]); if (bf2 = = 1) {// Left-left rotation rotate (node, 0);} else if (BF2 = =-1) {///rotate rotate (node->child[0], 1); Rotate (node, 0);}} else if (bf = =-2) {int bf2 = getheight (node->child[1]->child[0])-getheight (node->child[1]->child[1]); if ( BF2 = = 1) {//right-left rotation rotate (node, 1);} else if (BF2 = =-1) {//right-right rotation rotate (node->child[1], 0); Rotate (node, 1);}} void Insert (treenode*& node, int val) {if (!node) {node = new TreeNode (val);} else if (Node->data = = val) {node->count++;} Else{int dir = node->data < Val;insert (Node->child[dir], Val);} Maintain tree balance maintain (node);//Update node node->update ();} Note The argument is a reference to the pointer void Delete (treenode*& node, int val) {if (!node) {return;} else if (Node->data = = val) {if (node->child[0] && node->child[1]) {int dir = getheight (Node->child[0] ) < GetHeight (node->child[1]);//Turn the taller tree in the subtree, rotate the Rotate (node, dir), or recursively call delete until the leaf node does a real delete delete (node-> child[!DIR], Val);} else{treenode* Tmp_node = null;if (Node->child[0]) {tmp_node = node->child[0];} else if (node->child[1]) {tmp_node = node->child[1];} Delete Node;node = Tmp_node; Use reference}}else{int dir = Node->data < Val;delete (Node->child[dir], Val);} Maintain (node); Maintain balance node->update ();//Update node}int getkth (treenode* node, int k) {while (node) {if (! node->child[0]) {if (k <= node- >count) {return node->data;} Else{k-= Node->count;node = Node->child[1];}} Else{if (Node->child[0]->size < K && Node->child[0]->size + node->count >= k) {return node-& Gt;data;} else if (Node->child[0]->size > K) {node = node->child[0];} Else{k-= (node->child[0]->size + node->count); node = node->child[1];}} return-1;}};

Reference:
Balance Tree Balanced binary trees (AVL tree)
AVL Tree Templates

AVL Balance Tree

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.