1. 程式人生 > >PAT 1066. Root of AVL Tree (25)

PAT 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 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

AVL樹的旋轉。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 101000;
struct Node {
  int val;
  int son[2];
  int height;
}s[maxn];
int root, sz;
int n;

int add(int x) {
  s[sz].val = x;
  s[sz].son[0] = s[sz].son[1] = -1;
  s[sz].height = 0;
  sz ++;
  return sz - 1;
}

int Height(int id) {
  if(id == -1) return -1;
  return s[id].height;
}

int R(int k2) {
  int k1 = s[k2].son[0];
  s[k2].son[0] = s[k1].son[1];
  s[k1].son[1] = k2;
  s[k2].height = max(Height(s[k2].son[0]), Height(s[k2].son[1])) + 1;
  s[k1].height = max(Height(s[k1].son[0]), Height(s[k1].son[1])) + 1;
  return k1;
}

int L(int k2) {
  int k1 = s[k2].son[1];
  s[k2].son[1] = s[k1].son[0];
  s[k1].son[0] = k2;
  s[k2].height = max(Height(s[k2].son[0]), Height(s[k2].son[1])) + 1;
  s[k1].height = max(Height(s[k1].son[0]), Height(s[k1].son[1])) + 1;
  return k1;
}

int RL(int k3) {
  int k1 = s[k3].son[1];
  s[k3].son[1] = R(k1);
  return L(k3);
}

int LR(int k3) {
  int k1 = s[k3].son[0];
  s[k3].son[0] = L(k1);
  return R(k3);
}

int Insert(int id, int val) {
  if(id == -1) {
    id = add(val);
  } else if(val < s[id].val) {
    s[id].son[0] = Insert(s[id].son[0], val);
    if(Height(s[id].son[0]) - Height(s[id].son[1]) == 2) { // 需要調整
      if(val < s[s[id].son[0]].val) id = R(id);
      else id = LR(id);
    }
  } else {
    s[id].son[1] = Insert(s[id].son[1], val);
    if(Height(s[id].son[1]) - Height(s[id].son[0]) == 2) { // 需要調整
      if(val > s[s[id].son[1]].val) id = L(id);
      else id = RL(id);
    }
  }
  s[id].height = max(Height(s[id].son[0]), Height(s[id].son[1])) + 1;
  return id;
}

int main() {
  scanf("%d", &n);
  root = -1;
  for(int i = 1; i <= n; i ++) {
    int x;
    scanf("%d", &x);
    root = Insert(root, x);
   // cout << root << endl;
  }
  cout << s[root].val << endl;
  return 0;
}

相關推薦

PAT 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 ti

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

1066. Root of AVL Tree (25)

組合 png 必須 數組 eight pac image spa span 距離PAT考試還有 11天最重要的是做透每一題 (1)思路 就是考察基本的AVL樹 這裏主要寫的是單旋轉左旋和右旋 雙旋轉可以用其組合得到 這裏要註意的是,insert

1066 Root of AVL Tree25 分)AVL

題目 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

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 代碼

PTA (Advanced Level)1066 Root of AVL Tree

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 o

PAT.A1066 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 on

PAT-ADVANCED1066——Root of AVL Tree

題目描述: 題目翻譯: AVL樹是一棵自平衡的二分搜尋樹。在AVL樹中,左右子樹的高度差不會超過1;任何時刻如果左右子樹的高度差超過了1,自平衡操作會使得其維持左右子樹高度差不超過1這個性質。下圖展示了旋轉規則。 現在給你一串插入序列,你需要輸出由該插入

PAT1066--Root of AVL Tree (25)

http://www.patest.cn/contests/pat-a-practise/1066 原題的描述就不再複述了。 這道題我從昨晚開始,做了七個小時。哭 從這道題的各種波折來看,我存在如下幾個程式設計上的問題: 1,原本我以為自己在樹的結構方面是很清晰的,可是事實並不是

04-樹5 Root of AVL Tree25 分)

size figure ive images lang rop first double alt An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the

Root of AVL Tree25 分)---AVL樹的證明式分析

(第一篇)記PAT習題,AVL樹的原理的見解 04-樹5 Root of AVL Tree (25 分)       題目意思就是給出一個N,接著插入N個值,以AVL樹的方式插入,然後求AVL樹的根節點   首先是樹的節點結構體,網上有人實現是節點本身儲存了

04-樹5 Root of AVL Tree25 分)

  #include<cstdio> #include<stdlib.h> typedef struct TNode* Tree; struct TNode{ int data; Tree Left, Right; }; int Height(Tr

AVL_insert_1066 Root of AVL Tree25 分)

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

Root of AVL Tree

mage ted 技術分享 tinc pre math input span pos An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two chi

PAT1066 Root of AVL Tree-平衡二叉樹構建

題目連結: PAT1066 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

04-樹5 Root of AVL Tree

oot eof sin 要求 avltree std %d tty pre   這道題目要求找出AVL樹的根節點,重點考查了AVL樹的旋轉(右單旋、左單旋、右-左雙旋和左-右雙旋)與插入操作。 1 #include <stdio.h> 2 #inclu

PTA || 04-樹5 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

7-6 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 a

《資料結構》04-樹5 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

平衡二叉樹實現 A1066 root of avl tree

AVL平衡二叉樹 實現 查詢時間複雜度 O(logn) LL 左旋 root 2 root->lchild=1 LR 左旋後右旋 root 2 root->lchild=-1 RR 右旋 root=-2 root->rchild=-1 RL