04-樹5 Root of AVL Tree (25 分)
04-樹5 Root of AVL Tree (25 分)
雖然借鑑了別人的思路,但是第一次自己寫出程式碼,值得紀念,繼續加油。
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 <iostream> #include <string> #include <vector> #include <stdio.h> #include <stdlib.h> using namespace std; typedef struct TNode *PtrToTNode; typedef PtrToTNode Tree; struct TNode{ int data; Tree left; Tree right; }; Tree LeftSingleRotation(Tree T1); //四種旋轉 Tree RightSingleRotation(Tree T1); Tree LeftRightRotation(Tree T1); Tree RightLeftRotation(Tree T1); int GetHeight(Tree T1); Tree Insert(int n,Tree T1) //後續輸入節點依次插入並做平衡因子判斷 { if(!T1) { T1 = (Tree)malloc(sizeof(struct TNode)); T1->left = nullptr; T1->right = nullptr; T1->data = n; T1->height = 1; } else if(T1->data>n) { T1->left = Insert(n, T1->left); if(GetHeight(T1->left)-GetHeight(T1->right)==2) //左子樹麻煩節點出現 { if(n<T1->left->data)//左左 { T1 = LeftSingleRotation(T1); } else if(n>T1->left->data)//左右 { T1 = LeftRightRotation(T1); } } } else if(T1->data<n) { T1->right = Insert(n, T1->right); if(GetHeight(T1->right)-GetHeight(T1->left)==2) //右子樹麻煩節點出現 { if(n>T1->right->data)//右右 { T1 = RightSingleRotation(T1); } else if(n< T1->right->data)//右左 { T1 = RightLeftRotation(T1); } } } return T1; } Tree BuildTree(int n) //第一個資料用於建樹 { Tree T1; T1 = (Tree)malloc(sizeof(struct TNode)); T1->data = n; T1->left = nullptr; T1->right = nullptr; return T1; } int GetHeight(Tree T1) //遞迴求樹高用作判斷平衡依據 { int hl, hr,h; if(!T1) return 0; else { hl = GetHeight(T1->left); hr = GetHeight(T1->right); h = max(hl, hr); return h + 1; } } Tree LeftSingleRotation(Tree A) { Tree B; B = A->left; A->left = B->right; B->right = A; //根結點和根節點的左子樹高度發生變化,其餘不變 return B;//B成為原A樹的根結點 } Tree RightSingleRotation(Tree A) { Tree B; B = A->right; A->right = B->left; B->left = A; return B; } Tree LeftRightRotation(Tree A) { Tree B, C; B = A->left; C = B->right; A->left = C->right; B->right = C->left; C->right = A; C->left = B; return C; } Tree RightLeftRotation(Tree A) { Tree B, C; B = A->right; C = B->left; A->right = C->left; B->left = C->right; C->left = A; C->right = B; return C; } int max(int a,int b) { return a > b ? a : b; } int main() { int n,da; Tree T; //cin >> n; scanf("%d", &n); //cin >> da; scanf("%d", &da); T = BuildTree(da); for (int i = 1; i < n;i++) { scanf("%d", &da); T = Insert(da, T); } printf("%d", T->data); system("pause"); return 0; }