-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice7_5.c
191 lines (159 loc) · 3.95 KB
/
practice7_5.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define MAX(a,b) ((a>b) ? a : b)
typedef int element;
typedef struct treeNode {
element key;
struct treeNode *left;
struct treeNode *right;
} treeNode;
treeNode *LL_rotate(treeNode *parent) {
treeNode *child = parent->left;
parent->left = child->right;
child->right = parent;
return child;
}
treeNode *RR_rotate(treeNode *parent) {
treeNode *child = parent->right;
parent->right = child->left;
child->left = parent;
return child;
}
treeNode *LR_rotate(treeNode *parent) {
treeNode *child = parent->left;
parent->left = RR_rotate(child);
return LL_rotate(parent);
}
treeNode *RL_rotate(treeNode *parent) {
treeNode *child = parent->right;
parent->right = LL_rotate(child);
return RR_rotate(parent);
}
int getHeight(treeNode *p) {
int height = 0;
if (p != NULL) height = MAX(getHeight(p->left), getHeight(p->right)) + 1;
return height;
}
int getBF(treeNode *p) {
if (p == NULL) return 0;
return getHeight(p->left) - getHeight(p->right);
}
treeNode *rebalance(treeNode **p) {
int BF = getBF(*p);
if (BF > 1) {
if (getBF((*p)->left) > 0) {
*p = LL_rotate(*p);
}
else {
*p = LR_rotate(*p);
}
}
else if (BF < -1) {
if (getBF((*p)->right) < 0) {
*p = RR_rotate(*p);
}
else {
*p = RL_rotate(*p);
}
}
return *p;
}
treeNode *insert_AVL_Node(treeNode **root, element x) {
if (*root == NULL) {
*root = (treeNode *)malloc(sizeof(treeNode));
(*root)->key = x;
(*root)->left = NULL;
(*root)->right = NULL;
}
else if (x < (*root)->key) {
(*root)->left = insert_AVL_Node(&((*root)->left), x);
*root = rebalance(root);
}
else if (x > (*root)->key) {
(*root)->right = insert_AVL_Node(&((*root)->right), x);
*root = rebalance(root);
}
else {
printf("\n 이미 같은 키가 있습니다! \n");
exit(1);
}
return *root;
}
treeNode *insert_BST_Node(treeNode *p, element x) {
treeNode *newNode;
if (p == NULL) {
newNode = (treeNode *)malloc(sizeof(treeNode));
newNode->key = x;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
else if (x < p->key) p->left = insert_BST_Node(p->left, x);
else if (x > p->key) p->right = insert_BST_Node(p->right, x);
else printf("\n 이미 같은 키가 있습니다! \n");
return p;
}
treeNode *searchTree(treeNode *root, element x) {
treeNode *p;
int count = 0;
p = root;
while (p != NULL) {
count++;
if (x < p->key)p = p->left;
else if (x == p->key) {
printf("%3d번째에 탐색 성공", count);
return p;
}
else p = p->right;
}
count++;
printf("%3d번째에 탐색 실패! 찾는 키가 없습니다!", count);
return p; // 이거 초기화 안됨
}
void displayInorder(treeNode *root) {
if (root) {
displayInorder(root->left);
printf("_%d ", root->key);
displayInorder(root->right);
}
}
int main() {
treeNode *root_AVL = NULL;
treeNode *root_BST = NULL;
root_AVL = insert_AVL_Node(&root_AVL, 50);
insert_AVL_Node(&root_AVL, 60);
insert_AVL_Node(&root_AVL, 70);
insert_AVL_Node(&root_AVL, 90);
insert_AVL_Node(&root_AVL, 80);
insert_AVL_Node(&root_AVL, 75);
insert_AVL_Node(&root_AVL, 73);
insert_AVL_Node(&root_AVL, 72);
insert_AVL_Node(&root_AVL, 78);
printf("\n ******* AVL 트리 출력 **************** \n\n");
displayInorder(root_AVL);
printf("\n\n AVL 트리에서 80 탐색 : ");
searchTree(root_AVL, 80);
printf("\n\n AVL 트리에서 90 탐색 : ");
searchTree(root_AVL, 90);
printf("\n\n AVL 트리에서 76 탐색 : ");
searchTree(root_AVL, 76);
root_AVL = insert_AVL_Node(&root_BST, 50);
insert_BST_Node(root_BST, 60);
insert_BST_Node(root_BST, 70);
insert_BST_Node(root_BST, 90);
insert_BST_Node(root_BST, 80);
insert_BST_Node(root_BST, 75);
insert_BST_Node(root_BST, 73);
insert_BST_Node(root_BST, 72);
insert_BST_Node(root_BST, 78);
printf("\n ******* BST 출력 **************** \n\n");
displayInorder(root_BST);
printf("\n\n BST에서 80 탐색 : ");
searchTree(root_BST, 80);
printf("\n\n BST에서 90 탐색 : ");
searchTree(root_BST, 90);
printf("\n\n BST에서 76 탐색 : ");
searchTree(root_BST, 76);
return 0;
}