1. 程式人生 > >04-樹4. Root of AVL Tree (25)

04-樹4. Root of AVL Tree (25)

print fine -c mono case tle scan stdin amp

04-樹4. Root of AVL Tree (25)

時間限制 100 ms
內存限制 65536 kB
代碼長度限制 8000 B
判題程序 Standard 作者 CHEN, Yue

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 ythe 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>
struct Node {
	int val;
	int height;
	struct Node *left;
	struct Node *right;
};
int max(int a, int b) {				//返回兩者較大者
	return a > b ?

a : b; } int height(struct Node* root) { //為了兼容空樹,樹高度不能直接返回根節點的height屬性 if (root == NULL) { return -1; } else { return root->height; } } struct Node* RRrotation(struct Node* k1) { //右右旋轉 struct Node* k2 = k1->right; //k2為根節點k1的右兒子 k1->right = k2->left; //將k2的左兒子連接到k1的右子節點 k2->left = k1; //將k1連接到k2的左子節點 k1->height = max(height(k1->left), height(k1->right)) + 1; //更新節點高度,僅僅有k1,k2節點高度變化 k2->height = max(height(k2->left), height(k2->right)) + 1; return k2; } struct Node* LLrotation(struct Node* k1) { //左左旋轉 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) { //右左旋轉 //分兩步:先對根節點的右子樹做左左旋轉。再對根做右右旋轉 k1->right = LLrotation(k1->right); return RRrotation(k1); } struct Node* LRrotation(struct Node* k1) { //左右旋轉 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); //插入右子樹 if (height(root->right) - height(root->left) == 2) { if (node->val > root->right->val) { //假設插入右子樹的右子樹,進行右右旋轉 root = RRrotation(root); } else if (node->val < root->right->val) { //進行右左旋轉 root = RLrotation(root); } } } else if (node->val < root->val) { //插入左子樹情況與上面相似 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); } } } //遞歸中不斷更新插入節點到根節點路徑上全部節點的高度 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 (int i = 0; i < n; ++i) { //初始化一個節點。並插入AVL樹中 scanf("%d", &nodes[i].val); nodes[i].height = 0; //孤立的節點高度為0 nodes[i].left = NULL; nodes[i].right = NULL; root = insertAvlTree(&nodes[i], root); } printf("%d", root->val); return 0; }



題目鏈接:http://www.patest.cn/contests/mooc-ds/04-%E6%A0%914

04-樹4. Root of AVL Tree (25)