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
192
|
#include <stdio.h>
#include <stdlib.h>
struct b_node {
int key;
struct b_node *left;
struct b_node *right;
};
struct b_node* init_node(int key)
{
struct b_node *node = malloc(sizeof(struct b_node));
if (!node) {
printf("Error initializing the node\n");
goto out;
}
node->key = key;
node->left = NULL;
node->right = NULL;
out:
return node;
}
struct b_node* search_node(struct b_node *ROOT, int key)
{
}
void alloc_root(struct b_node **ROOT)
{
int key;
struct b_node *new;
printf("Type the ROOT key: ");
scanf(" %d", &key);
new = malloc(sizeof(struct b_node));
new->key = key;
new->left = NULL;
new->right = NULL;
*ROOT = new;
printf("ROOOOT = %p\n", new);
}
int insert_node(struct b_node *ROOT)
{
struct b_node *cur;
struct b_node *new;
int key;
int error = 0;
if (!ROOT) {
printf("Please initialize root node\n");
return 0;
}
printf("Type a new key: ");
scanf(" %d", &key);
new = init_node(key);
if (!new)
return 1;
cur = ROOT;
// printf("new item key = %d\n", new->key);
// printf("cur = %p ROOT = %p\n", cur, ROOT);
while (1) {
if (new->key <= cur->key) {
/*go left*/
if (cur->left == NULL) {
cur->left = new;
break;
} else {
cur = cur->left;
continue;
}
} else {
printf("Current right: %p\n", cur);
/*go right*/
if (cur->right == NULL) {
cur->right = new;
break;
} else {
cur = cur->right;
continue;
}
}
}
// printf("KEEP ROOT = %p\n",ROOT);
return 0;
}
int delete_node(struct b_node *ROOT, int key)
{
}
void print_tree(struct b_node *ROOT)
{
}
void print_right(struct b_node *ROOT)
{
struct b_node *cur;
if (ROOT == NULL) {
printf ("Tree doesn't exist\n");
return;
}
cur = ROOT;
while (cur) {
printf(" Node: %p - Key : %d\n", cur, cur->key);
cur = cur->right;
}
}
void print_left(struct b_node *ROOT)
{
struct b_node *cur;
if (ROOT == NULL) {
printf ("Tree doesn't exist\n");
return;
}
cur = ROOT;
while (cur) {
printf(" Node: %p - Key : %d\n", cur, cur->key);
cur = cur->left;
}
}
void usage()
{
printf("Usage: \n");
printf("a: Alloc root node\n");
printf("i: Insert node\n");
printf("d: Delete node\n");
printf("s: Search node\n");
printf("r: Print the right nodes \n");
printf("l: Print the left nodes \n");
return;
}
int main(void) {
struct b_node *ROOT = NULL;
while (1) {
char opt;
printf(" Option: ");
scanf(" %c", &opt);
switch(opt) {
case 'a':
alloc_root(&ROOT);
break;
case 'i':
insert_node(ROOT);
break;
case 'l':
print_left(ROOT);
break;
case 'r':
print_right(ROOT);
break;
case 'q':
goto out;
default:
usage();
break;
}
}
out:
if (ROOT)
free(ROOT);
return 0;
}
|