04-Tree 4. Root of AVL Tree (25) time limit
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Chen, Yue
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
#include <stdio.h>struct node {int val;int height;struct node *left;struct node *right;}; int max (int a, int b) {//returns the larger of both return a > B? a:b;} int height (struct node* root) {//In order to be compatible with the empty tree, the tree height cannot be returned directly to the root node's Height property if (root = NULL) {return-1;} else {return root->height;}} struct node* rrrotation (struct node* k1) {//right-right rotation struct node* K2 = k1->right;//k2 for root node k1 right son k1->right = k2->left;/ /Connect the left son of K2 to the right child node of K1 k2->left = k1;//Connect K1 to K2 's left child node K1->height = max (height (k1->left), height (k1->right)) + 1 ;//Update node height, only k1,k2 node height change k2->height = max (height (k2->left), height (k2->right)) + 1;return K2;} struct node* llrotation (struct node* k1) {//Zo rotation struct node* k2 = k1->left;k1->left = K2->right;k2->right = K1 ; k1->height = max (height (k1->left), height (k1->right)) + 1;k2->height = max (height (k2->left), Height ( k2->right)) + 1;return K2;} struct node* rlrotation (struct node* k1) {//right-to-left rotation//two-step: first to the right subtree of the root node to do left-to-left rotation, and then to the root do right-right rotation k1->right = llrotation (k1->right); RetUrn rrrotation (k1);} struct node* lrrotation (struct node* k1) {//rotate around k1->left = rrrotation (k1->left); return llrotation (K1);} struct node* insertavltree (struct node* Node, struct node* root) {if (root = NULL) {root = Node;return root;} if (Node->val > Root->val) {root->right = insertavltree (node, root->right);//Insert Right subtree if (height (root-> right)-height (root->left) = = 2) {if (Node->val > Root->right->val) {///if the right subtree of the right subtree is inserted, rotate the root = Rrrotatio n (root);} else if (Node->val < Root->right->val) {//Right left rotation root = rlrotation (root);}}} else if (Node->val < Root->val) {//Insert left subtree case similar to above root->left = insertavltree (node, root->left); if (height ( Root->left)-height (root->right) = = 2) {if (Node->val < root->left->val) {root = Llrotation (root);} else if (Node->val > root->left->val) {root = Lrrotation (root);}}} Recursively updates the height of all nodes in the insertion node to the root node path root->height = max (height (root->left), height (root->right)) + 1;return root;} INT Main () {freopen ("test.txt", "R", stdin), int n;scanf ("%d", &n), struct node nodes[20];struct node *root = null;for (i NT i = 0; I < n; ++i) {//Initializes a node and inserts scanf ("%d", &nodes[i].val) into the AVL tree, nodes[i].height = 0;//The isolated node height is 0nodes[i].left = null;nodes[i]. right = Null;root = Insertavltree (&nodes[i], root);} printf ("%d", root->val); return 0;}
Title Link: http://www.patest.cn/contests/mooc-ds/04-%E6%A0%914
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
04-Tree 4. Root of AVL Tree (25)