For a normal two-fork search tree, after multiple insertions or deletions, it is easy to get the tree out of balance, resulting in a tree whose depth is not O (Logn), but close to O (N), which greatly reduces the search efficiency of the tree. One solution is to have an additional structural condition called Balance: the depth of any node must not be too deep. There is one of the oldest balanced lookup trees, the AVL tree.
The AVL tree is a two-fork lookup tree with equilibrium conditions. The equilibrium condition is that the height of the Saozi right subtree of each node is up to 1 of the two-fork lookup tree (the height of the empty tree is defined as-1). Compared to the normal two-fork tree, the AVL tree node needs to add a variable to save the node height. The node declaration for the AVL tree is as follows:
struct TreeNode *struct TreeNode *Position; struct treenode{ int Data; Avltree left; Avltree right; int Height; // Save node height };
A tree with only one node is obviously an AVL tree, after which we insert a node. However, the nature of the AVL tree may be broken during the insertion process, so we need to make a simple correction to the tree, namely the rotation of the AVL tree.
Setting a node loses its balance after inserting the next node, which can occur in four situations:
1. Insert the left sub-tree of A's left son. (left-left)
2. Insert the right sub-tree of the left son of a. (left-right)
3. Insert the left sub-tree of the right son of a. (Right-left)
4. Insert the right sub-tree of the right son of a. (Right-right)
Cases 1 and 4, cases 2 and 3 are about the mirror symmetry of a node respectively, so in theory there are two cases, and there are four kinds of programming implementation.
Single rotation-Cases 1 and 4:
Double rotation-Cases 2 and 3:
Cases 2 and 3 are the insertion of a node into the subtree Y, which, by the Zodan or right paddle, cannot alter the height of the subtree y. The solution is to decompose the subtree y into the root node and the corresponding Saozi right subtree, and then rotate the corresponding nodes accordingly, such as:
The following topic examines the rotation of the AVL tree: Source: http://www.patest.cn/contests/mooc-ds/04-%E6%A0%914
An AVL tree is a self-balancing binary search tree. In a AVL tree, the heights of the subtrees of any node differ by at the most one; If at any time they differ by more than one, the rebalancing is the done to restore this property. Figures 1-4 illustrate the rotation rules.
Now given a sequence of insertions, you is supposed to the root of the resulting AVL tree.
Input Specification:
Each input file contains the one test case. For each case, the first line contains a positive integer N (<=20) which are the total number of keys to being inserted. Then N distinct integer keys is given in the next line. All the numbers in a line is separated by a space.
Output Specification:
For each test case, print ythe root of the resulting AVL tree on one line.
Sample Input 1:
588 70 61) 96 120
Sample Output 1:
70
Sample Input 2:
788 70 61 96 120 90 65
Sample Output 2:
88
The main idea is to enter an integer n, then enter the value of n nodes, in order to establish the AVL tree, and finally output the value of the root node of the AVL tree.
The code is as follows:
#include <cstdio>#include<cstdlib>typedefstructTreeNode *Avltree;typedefstructTreeNode *Position;structtreenode{intData; Avltree left; Avltree right; intHeight;}; Avltree Insert (intX, Avltree T);//Insert new node, adjust if necessaryPosition Singlerotatewithleft (Position a);//ZodanPosition Singlerotatewithright (Position b);//Right PaddlePosition Doublerotatewithleft (Position a);//left and right rotationPosition Doublerotatewithright (Position b);//right-left rotationintMax (intX1,intx2);//returns the larger of a two intintHeight (Position P);//returns the height of a nodeintMain () {intN, x; Avltree T=NULL; scanf ("%d", &N); for(inti =0; I < n; i++) {scanf ("%d", &x); T=Insert (x, T); } printf ("%d\n", T->data);//Print the value of the root node return 0;} Avltree Insert (intx, Avltree T) { if(T = =NULL) {T= (Avltree)malloc(sizeof(structTreeNode)); T->data =x; T->left = T->right =NULL; T->height =0; } Else if(x < T->data)//inserting to Zuozi{T->left = Insert (x, t->Left ); if(Height (t->left)-height (t->right) = =2)//need to adjust { if(X < t->left->Data) T=Singlerotatewithleft (T); ElseT=Doublerotatewithleft (T); } } Else if(x > T->data)//Right child tree insert{T->right = Insert (x, t->Right ); if(Height (t->right)-height (t->left) = =2)//need to adjust { if(X > T->right->Data) T=singlerotatewithright (T); ElseT=doublerotatewithright (T); } } /*the node with the else value x already exists in the tree without inserting*/ /*Update node Height*/T->height = Max (height (t->left), height (t->right)) +1; returnT;} Position Singlerotatewithleft (Position a) {Position b= a->Left ; A->left = b->Right ; b->right =A; //update A, b node heightA->height = Max (height (a->left), height (a->right)) +1; b->height = Max (height (b->left), height (b->right)) +1; returnb/*the new root node*/}position singlerotatewithright (Position b) {Position a= b->Right ; b->right = a->Left ; A->left =b; //Update A/b node heightA->height = Max (height (a->left), height (a->right)) +1; b->height = Max (height (b->left), height (b->right)) +1; returnA/*the new root node*/}position Doublerotatewithleft (Position a) {a->left = Singlerotatewithright (a->Left ); returnSinglerotatewithleft (a);} Position Doublerotatewithright (Position b) {b->right = Singlerotatewithleft (b->Right ); returnSinglerotatewithright (b);}intMax (intX1,intx2) { return(X1 > x2)?x1:x2;}intHeight (Position P) {if(P = = NULL)//NULL node height is-1 return-1; returnP->Height;}
The detail to note is that we need to quickly get the height of a node (including an empty node), so we need a function to handle the empty node (null pointer) instead of the simple position->height.
04-Tree 4. Root of AVL tree-the implementation of the balanced lookup tree AVL tree