Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the root of the resulting AVL tree in one line.
Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88
答案如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct AVLNode *Position;
typedef Position AVLTree;
struct AVLNode{
int Data;
AVLTree Left;
AVLTree Right;
int height;
};
int max(int a,int b){
return a>b?a:b;
}
int GetHeight(AVLTree A){
if(!A) return 0;
else return A->height;
}
AVLTree NewNode(int V){
AVLTree t=(AVLTree)malloc(sizeof(struct AVLNode));
t->Data=V;
t->Left=t->Right=NULL;
t->height=0;
return t;
}
int Max ( int a, int b )
{
return a > b ? a : b;
}
AVLTree SingleLeftRotation ( AVLTree A )
{ /* 注意:A必須有一個左子結點B */
/* 將A與B做左單旋,更新A與B的高度,返回新的根結點B */
AVLTree B = A->Left;
A->Left = B->Right;
B->Right = A;
A->height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1;
B->height = Max( GetHeight(B->Left), A->height ) + 1;
return B;
}
AVLTree SingleRightRotation ( AVLTree A )
{ /* 注意:A必須有一個右子結點B */
/* 將A與B做右單旋,更新A與B的高度,返回新的根結點B */
AVLTree B = A->Right;
A->Right = B->Left;
B->Left = A;
A->height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1;
B->height = Max( GetHeight(B->Right), A->height ) + 1;
return B;
}
AVLTree DoubleLeftRightRotation ( AVLTree A )
{ /* 注意:A必須有一個左子結點B,且B必須有一個右子結點C */
/* 將A、B與C做兩次單旋,返回新的根結點C */
/* 將B與C做右單旋,C被返回 */
A->Left = SingleRightRotation(A->Left);
/* 將A與C做左單旋,C被返回 */
return SingleLeftRotation(A);
}
AVLTree DoubleRightLeftRotation ( AVLTree A )
{ /* 注意:A必須有一個右子結點B,且B必須有一個左子結點C */
/* 將A、B與C做兩次單旋,返回新的根結點C */
/* 將B與C做左單旋,C被返回 */
A->Right = SingleLeftRotation(A->Right);
/* 將A與C做右單旋,C被返回 */
return SingleRightRotation(A);
}
AVLTree Insert( AVLTree T, int X )
{ /* 將X插入AVL樹T中,並且返回撥整後的AVL樹 */
if ( !T ) { /* 若插入空樹,則新建包含一個結點的樹 */
T = (AVLTree)malloc(sizeof(struct AVLNode));
T->Data = X;
T->height = 0;
T->Left = T->Right = NULL;
} /* if (插入空樹) 結束 */
else if ( X < T->Data ) {
/* 插入T的左子樹 */
T->Left = Insert( T->Left, X);
/* 如果需要左旋 */
if ( GetHeight(T->Left)-GetHeight(T->Right) == 2 )
if ( X < T->Left->Data )
T = SingleLeftRotation(T); /* 左單旋 */
else
T = DoubleLeftRightRotation(T); /* 左-右雙旋 */
} /* else if (插入左子樹) 結束 */
else if ( X > T->Data ) {
/* 插入T的右子樹 */
T->Right = Insert( T->Right, X );
/* 如果需要右旋 */
if ( GetHeight(T->Left)-GetHeight(T->Right) == -2 )
if ( X > T->Right->Data )
T = SingleRightRotation(T); /* 右單旋 */
else
T = DoubleRightLeftRotation(T); /* 右-左雙旋 */
} /* else if (插入右子樹) 結束 */
/* else X == T->Data,無須插入 */
/* 別忘了更新樹高 */
T->height = Max( GetHeight(T->Left), GetHeight(T->Right) ) + 1;
return T;
}
AVLTree MakeTree(int n){
AVLTree t;
int i,V;
scanf("%d",&V);
t=NewNode(V);
for(i=1;i<n;i++){
scanf("%d",&V);
t=Insert(t,V);
}
return t;
}
void FreeTree ( AVLTree T ) /* 釋放T的空間 */
{
if (T->Left) FreeTree(T->Left);
if (T->Right) FreeTree(T->Right);
free(T);
}
int main(){
int N;
scanf("%d",&N);
AVLTree A;
A=MakeTree(N);
int data=A->Data;
printf("%d\n",data);
FreeTree(A);
return 0;
}
相關文章
- (非原創)PAT甲級1123 Is It a Complete AVL Tree (30分)|C++實現C++
- Java集合原始碼分析之基礎(五):平衡二叉樹(AVL Tree)Java原始碼二叉樹
- PG12-2 B-Tree 索引 level 0 root頁索引
- 看動畫學演算法之:平衡二叉搜尋樹AVL Tree動畫演算法
- [筆記]AVL樹筆記
- 題解 AVL 樹
- AVL樹旋轉
- 手撕AVL樹(C++)C++
- 資料結構 - AVL 樹資料結構
- 平衡二叉樹(AVL)二叉樹
- 圖解:什麼是AVL樹?圖解
- 資料結構之「AVL樹」資料結構
- tree
- Maximize the Root
- 手把手教,手寫AVL樹
- 漫話:什麼是平衡(AVL)樹?這應該是把AVL樹講的最好的文章了
- GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘root‘ WITH GRANT OPTION;報錯IDE
- 取消 root 級管理員的 root 許可權
- DSU on Tree
- Rebuild TreeRebuild
- 01 Tree
- Tree Compass
- A - Distance in Tree
- Decision Tree
- 2.3.1.2 Application RootAPP
- CSS E:rootCSS
- SCSS @at-rootCSS
- 【MySQL(1)| B-tree和B+tree】MySql
- jackson學習之四:WRAP_ROOT_VALUE(root物件)物件
- 多路查詢樹:B-tree/b+tree
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- 手寫AVL平衡二叉搜尋樹
- AVL樹(查詢、插入、刪除)——C語言C語言
- 資料結構與演算法:AVL樹資料結構演算法
- 平衡二叉樹(AVL樹),原來如此!!!二叉樹
- 十三、Mysql之平衡二叉樹(AVL樹)MySql二叉樹
- segment tree beats
- Circular Spanning Tree