-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbasic_tree_impl.c
177 lines (139 loc) · 3.51 KB
/
basic_tree_impl.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
#include <stdio.h>
#include <stdlib.h>
typedef struct element {
int val;
struct element *left, *right;
} Element;
Element *create(int val) {
Element *newElement = (Element*) malloc(sizeof(Element));
if( !newElement ) {
fprintf( stderr, "alloc error\n");
exit(1);
}
newElement->val = val;
newElement->left = newElement->right = NULL;
return newElement;
}
Element *find( Element *root, Element **previous, int val ) {
Element *tempPrevious = NULL;
while( root ) {
if( root->val == val )
break;
tempPrevious = root;
if( root->val > val )
root = root->left;
else
root = root->right;
}
if( previous )
*previous = tempPrevious;
return root;
}
int add( Element **root, int val ) {
if( !root ) {
fprintf(stderr, "NULL sent as a root address\n");
return 1;
}
Element *new = create(val);
Element *previous = NULL;
Element *e = find( *root, &previous, val );
/* debug: printf("e=%p;\tprev=%p;\tval=%d\n", e, previous, val);*/
/* check if element with given val already exists */
if( e ) {
new->right = e->right;
e->right = new;
}
else {
/* check if it's root */
if( !previous )
*root = new;
else {
if( val < previous->val )
previous->left = new;
else
previous->right = new;
}
}
return 0;
}
int deleteOneByValue( Element **root, int val ) {
if( !root ) {
fprintf(stderr, "deleteOneByValue: sent value for root's address is NULL\n");
return 1;
}
Element *temp = NULL;
Element *remainingTree = NULL;
Element *previous = NULL;
Element *elementToDelete = find(*root, &previous, val);
/* check if element with given value exists */
if( !elementToDelete ) {
fprintf(stderr, "deleteOneByValue: element with value %d not found\n", val );
return 1;
}
/* element has both left and right child */
if( elementToDelete->left && elementToDelete->right ) {
temp = elementToDelete->left;
/* find right most element */
while( temp->right ) temp = temp->right;
/* connect right child of the element to it's left child */
temp->right = elementToDelete->right;
remainingTree = elementToDelete->left;
}
/* element has only left child */
else if( elementToDelete->left && !elementToDelete->right ) {
remainingTree = elementToDelete->left;
}
/* element has only right child */
else if( !elementToDelete->left && elementToDelete->right ) {
remainingTree = elementToDelete->right;
}
else {
/* element doesn't have neither left nor right child */
remainingTree = NULL;
}
/* check if root is going to be deleted */
if( !previous )
*root = remainingTree;
/* check if remaining tree should be connected as left or as right child */
else if( val < previous->val )
previous->left = remainingTree;
else
previous->right = remainingTree;
return 0;
}
void printTreeInOrder_( Element *root ) {
if( root->left )
printTreeInOrder_( root->left );
printf("%d ", root->val);
if( root->right )
printTreeInOrder_( root->right );
}
void printTreeInOrder( Element *root ) {
printf("\nValues: \n");
if( root )
printTreeInOrder_( root );
printf("\n");
}
int main() {
Element *root = NULL;
printf("Input order: 8,3,5,7,1\n");
add( &root, 8 );
add( &root, 3 );
add( &root, 5 );
add( &root, 7 );
add( &root, 1 );
printTreeInOrder( root );
printf("root=%d\n",root->val);
deleteOneByValue(&root, 8);
printTreeInOrder( root );
printf("root=%d\n",root->val);
deleteOneByValue(&root, 3);
printTreeInOrder( root );
printf("root=%d\n",root->val);
}
/*
* 8
* 3
* 1 5
* 7
* */