-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
183 lines (151 loc) · 3.87 KB
/
tree.cpp
File metadata and controls
183 lines (151 loc) · 3.87 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct n {
int data;
struct n *left;
struct n *right;
};
typedef struct n node;
node *ekle(node *agac, int x) {
if (agac == NULL) {
node *root = (node *)malloc(sizeof(node));
root->left = NULL;
root->right = NULL;
root->data = x;
return root;
}
if (x < agac->data) {
agac->left = ekle(agac->left, x);
} else if (x > agac->data) {
agac->right = ekle(agac->right, x);
}
return agac;
}
int getHeight(node* root) {
if (root == NULL) return 0;
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
void printLevel(node* root, int level, int space, int gap) {
if (root == NULL) {
for (int i = 0; i < space + gap; i++) printf(" ");
return;
}
if (level == 1) {
for (int i = 0; i < space; i++) printf(" ");
printf("%d", root->data);
for (int i = 0; i < gap; i++) printf(" ");
} else if (level > 1) {
printLevel(root->left, level - 1, space, gap);
printLevel(root->right, level - 1, space, gap);
}
}
void printTree(node* root) {
int height = getHeight(root);
int maxWidth = pow(2, height) - 1;
int space = maxWidth * 2;
for (int i = 1; i <= height; i++) {
int gap = space / pow(2, i);
printLevel(root, i, gap / 2, gap);
printf("\n");
if (i < height) {
for (int j = 0; j < gap / 2 - 1; j++) printf(" ");
for (int k = 0; k < pow(2, i - 1); k++) {
printf("/");
for (int j = 0; j < gap - 1; j++) printf(" ");
printf("\\");
for (int j = 0; j < gap - 1; j++) printf(" ");
}
printf("\n");
}
}
}
int max(node* agac){
while(agac->right != NULL){
agac = agac->right;
}
return agac->data;
}
int min(node* agac){
while(agac->left != NULL){
agac = agac->left;
}
return agac->data;
}
node* find(node* root, int key) {
if (root == NULL)
return NULL;
if (root->data == key)
return root;
if (key < root->data)
return find(root->left, key);
else
return find(root->right, key);
}
node* sil(node* agac, int target) {
if (agac == NULL) {
return NULL;
}
if (target < agac->data) {
agac->left = sil(agac->left, target);
} else if (target > agac->data) {
agac->right = sil(agac->right, target);
} else {
if (agac->left == NULL && agac->right == NULL) {
free(agac);
return NULL;
}
if (agac->left == NULL) {
node* temp = agac->right;
free(agac);
return temp;
} else if (agac->right == NULL) {
node* temp = agac->left;
free(agac);
return temp;
}
int minValue = min(agac->right);
agac->data = minValue;
agac->right = sil(agac->right, minValue);
}
return agac;
}
void preorder(node *root) {
if (root == NULL) return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
void inorder(node *root) {
if (root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
void postorder(node *root) {
if (root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
int main() {
node *root = NULL;
root = ekle(root, 8);
ekle(root, 7);
ekle(root, 2);
ekle(root, 9);
ekle(root, 5);
printf("Preorder -> ");
preorder(root);
printf("\n");
printf("Inorder -> ");
inorder(root);
printf("\n");
printf("Postorder -> ");
postorder(root);
printf("\n");
printTree(root);
return 0;
}