-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_2.cpp
98 lines (94 loc) · 2.29 KB
/
04_2.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <cstdio>
using namespace std;
typedef struct TreeNode* Tree;
struct TreeNode {
int val, height;
Tree right, left;
};
Tree DoubleRightLeftRotation(Tree A);
Tree DoubleLeftRightRotation(Tree A);
Tree SingleRightRotation(Tree A);
Tree SingleLeftRotation(Tree A);
Tree Insert(Tree root, int v);
int getHeight(Tree r);
int Max(int a, int b);
int main()
{
int n, temp;
cin >> n;
Tree root = NULL;
while (n--) {
scanf("%d", &temp);
root = Insert(root, temp);
}
if (root) cout << root->val;
cout << endl;
return 0;
}
int Max(int a, int b)
{
return a > b ? a : b;
}
int getHeight(Tree t)
{
if (t) return t->height;
else return 0;
}
Tree SingleRightRotation(Tree A)
{
Tree B = A->right;
A->right = B->left;
B->left = A;
B->height = Max(getHeight(B->right), getHeight(B->left)) + 1;
A->height = Max(getHeight(A->right), getHeight(A->left)) + 1;
return B;
}
Tree SingleLeftRotation(Tree A)
{
Tree B = A->left;
A->left = B->right;
B->right = A;
B->height = Max(getHeight(B->right), getHeight(B->left)) + 1;
A->height = Max(getHeight(A->right), getHeight(A->left)) + 1;
return B;
}
Tree DoubleLeftRightRotation(Tree A)
{
A->left = SingleRightRotation(A->left);
return SingleLeftRotation(A);
}
Tree DoubleRightLeftRotation(Tree A)
{
A->right = SingleLeftRotation(A->right);
return SingleRightRotation(A);
}
Tree Insert(Tree root, int v)
{
if (!root) {
root = new TreeNode;
root->height = 0;
root->val = v;
root->left = root->right = NULL;
}
else if (v < root->val) {
root->left = Insert(root->left, v);
if ( getHeight(root->left) - getHeight(root->right) == 2) {
if ( v < root->left->val )
root = SingleLeftRotation(root);
else
root = DoubleLeftRightRotation(root);
}
}
else if (v > root->val) {
root->right = Insert(root->right, v);
if ( getHeight(root->right) - getHeight(root->left) == 2) {
if ( v > root->right->val )
root = SingleRightRotation(root);
else
root = DoubleRightLeftRotation(root);
}
}
root->height = Max(getHeight(root->left), getHeight(root->right)) + 1;
return root;
}