1066 Root of AVL Tree 25 ☆☆☆

github 地址:https://github.com/iofu728/PAT-A-by-iofu728 难度:☆☆☆ 关键词:二叉平衡树,AVL

题目

1066.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 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

大意

给出二叉平衡树AVL的插入顺序,输出最后树的根节点值。

思路

  1. 很久没做二叉树的题目了,特意来回顾一下。
  2. 二叉树建树很套路的分为newNodeinsertcreate三个函数。
  3. 如果是二叉查找树BST,需要对insert分类一下,总体和普通树一致。
  4. 特殊的是二叉平衡树AVL
  • 一要更新树高,增加getheightgeibanlanceupdate三个函数。
  • 二是要对不平衡情况的修正。
  1. 若getbanlance==2,左子树getbanlance==1,R;左子树==-1,LR。若getbanlance==-2,右子树getbanlance==-1,L;右子树==1,RL。
  2. LR分别为向左、右转。

code

#include <algorithm>
#include <iostream>
using namespace std;
struct node {
  int data, height;
  node *left, *right;
};
node *newNode(int num) {
  node *root = new node;
  root->data = num;
  root->height = 1;
  root->left = root->right = NULL;
  return root;
}
int getheight(node *root) {
  if (root == NULL) return 0;
  return root->height;
}
int getbalance(node *root) {
  return getheight(root->left) - getheight(root->right);
}
int update(node *&root) {
  root->height = max(getheight(root->left), getheight(root->right)) + 1;
}
void L(node *&root) {
  node *temp = root->right;
  root->right = temp->left;
  temp->left = root;
  update(root);
  update(temp);
  root = temp;
}
void R(node *&root) {
  node *temp = root->left;
  root->left = temp->right;
  temp->right = root;
  update(root);
  update(temp);
  root = temp;
}
node insert(node *&root, int num) {
  if (root == NULL) {
    root = newNode(num);
  } else if (root->data > num) {
    insert(root->left, num);
    update(root);
    if (getbalance(root) == 2) {
      if (getbalance(root->left) == 1) {
        R(root);
      } else if (getbalance(root->left) == -1) {
        L(root->left);
        R(root);
      }
    }
  } else {
    insert(root->right, num);
    update(root);
    if (getbalance(root) == -2) {
      if (getbalance(root->right) == -1) {
        L(root);
      } else if (getbalance(root->right) == 1) {
        R(root->right);
        L(root);
      }
    }
  }
}
node *create(int n) {
  int num;
  node *root = NULL;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &num);
    insert(root, num);
  }
  return root;
}
int main() {
  int n;
  scanf("%d", &n);
  node *root = create(n);
  printf("%d\n", root->data);
  return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
You can use this BibTex to reference this blog if you find it useful and want to quote it.