-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL_Balance.cpp
40 lines (29 loc) · 873 Bytes
/
AVL_Balance.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
#include <fstream>
using namespace std;
struct AVL_tree {
int id;
int left;
int right;
int balance;
};
int Count_Height(AVL_tree tree[], int id) {
if (id == 0) return 0;
int left_height = Count_Height(tree, tree[id].left) + 1;
int right_height = Count_Height(tree, tree[id].right) + 1;
tree[id].balance = right_height - left_height;
return tree[id].balance > 0 ? right_height : left_height;
}
int main() {
ifstream foe("balance.in");
ofstream ally("balance.out");
int number;
foe >> number;
number++;
AVL_tree *tree = new AVL_tree[number * sizeof(AVL_tree)];
for (int i = 1; i < number; i++) foe >> tree[i].id >> tree[i].left >> tree[i].right;
Count_Height(tree, 1);
for (int i = 1; i < number; i++) ally << tree[i].balance << "\n";
foe.close();
ally.close();
return 0;
}