-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.c
More file actions
180 lines (159 loc) · 4.05 KB
/
Copy pathbinary_tree.c
File metadata and controls
180 lines (159 loc) · 4.05 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
#include "binary_tree.h"
void link_child_to_parent(struct node* parent, struct node* child)
{
if (parent == NULL)
return;
child->parent = parent;
if (parent->data > child->data)
parent->left = child;
else
parent->right = child;
}
struct node* delete_node(struct node* root_node, int data)
{
struct node *node_for_destroy, *child, *child_left, *child_right, *parent,
*deep_right_node, *new_root;
new_root = root_node;
node_for_destroy = lookup(root_node, data);
if (node_for_destroy == NULL)
return root_node;
/* delete leaf - node doesn't have any child */
if (node_for_destroy->left == NULL && node_for_destroy->right == NULL)
{
if (node_for_destroy == root_node)
{
free(node_for_destroy);
return NULL;
}
else
{
if (node_for_destroy->parent->data > node_for_destroy->data)
node_for_destroy->parent->left = NULL;
else
node_for_destroy->parent->right = NULL;
free(node_for_destroy);
}
}
/* node has 2 children */
else if (node_for_destroy->left != NULL && node_for_destroy->right != NULL)
{
child_left = node_for_destroy->left;
child_right = node_for_destroy->right;
parent = node_for_destroy->parent;
if (node_for_destroy->parent == NULL)
new_root = child_left;
free(node_for_destroy);
/* move left child branch higher, to the place of node_for_destroy */
link_child_to_parent(parent, child_left);
/* searching the deepest right node of left child,
we'll link right child branch to it */
deep_right_node = child_left;
while (deep_right_node->right != NULL)
{
deep_right_node = deep_right_node->right;
}
link_child_to_parent(deep_right_node, child_right);
}
/* node has only 1 child */
else
{
if (node_for_destroy->left != NULL)
child = node_for_destroy->left;
else
child = node_for_destroy->right;
parent = node_for_destroy->parent;
if (parent == NULL)
new_root = child;
link_child_to_parent(parent, child);
free(node_for_destroy);
}
return new_root;
}
struct node* new_node(int data, struct node* parent) {
struct node* node = malloc( sizeof(struct node) );
node->data = data;
node->left = NULL;
node->right = NULL;
node->parent = parent;
return(node);
}
struct node* insert(struct node* node, int data, struct node* parent)
{
if( node == NULL )
{
node = new_node(data, parent);
return(node);
}
else
{
if (data < node->data)
{
node->left = insert(node->left, data, node);
}
else
{
node->right = insert(node->right, data, node);
}
}
return(node);
}
struct node* lookup(struct node* node, int target)
{
if (node == NULL) {
return NULL;
}
else
{
if (target == node->data)
{
return node;
}
else
{
if (target < node->data)
{
return(lookup(node->left, target));
}
else
{
return(lookup(node->right, target));
}
}
}
}
void print_preorder(struct node* tree)
{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(struct node* tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}
void print_postorder(struct node* tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void destroy_tree(struct node* node)
{
if ( node != NULL )
{
destroy_tree(node->left);
destroy_tree(node->right);
free(node);
}
}