-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Tree.c
106 lines (96 loc) · 1.74 KB
/
Binary_Tree.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
#include <stdio.h>
#include <string.h>
typedef struct binary_tree
{
int item;
struct binary_tree *left;
struct binary_tree *right;
}b_tree;
b_tree *create_b_tree(int item)
{
b_tree *new_tree = (b_tree *) malloc(sizeof(b_tree));
new_tree->item = item;
new_tree->left = new_tree->right = NULL;
return new_tree;
}
b_tree *create_empty_b()
{
return NULL;
}
b_tree *insert(b_tree *tree, int item)
{
if(tree == NULL){
tree = create_b_tree(item);
}else{
if(item >= tree->item){
tree->right = insert(tree->right, item);
}else{
tree->left = insert(tree->left, item);
}
}
return tree;
}
void free_tree(b_tree *tree)
{
if(tree == NULL)
return;
free_tree(tree->left);
free_tree(tree->right);
free(tree);
}
void printOrder(b_tree* tree)
{
if(tree == NULL) return;
else{
printf(" %d ", tree->item);
printOrder(tree->left);
printOrder(tree->right);
}
}
b_tree *minTree(b_tree *node)
{
b_tree* current = node;
while(current->left != NULL){
current = current->left;
}
return current;
}
b_tree *remove_node(b_tree *tree, int item)
{
if(tree == NULL){
return tree;
}
if(item < tree->item){
tree->left = remove_node(tree->left, item);
}else if(item > tree->item){
tree->right = remove_node(tree->right, item);
}else{
b_tree *temp;
if(tree->left == NULL){
temp = tree->right;
free(tree);
return temp;
}else if(tree->right == NULL){
temp = tree->left;
free(tree);
return temp;
}
temp = minTree(tree->right);
tree->item = temp->item;
tree->right = remove_node(tree->right, temp->item);
}
return tree;
}
int main()
{
b_tree *tree = create_empty_b();
int n;
while(scanf("%d", &n) != EOF)
{
tree = insert(tree, n);
}
remove_node(tree, 15);
printOrder(tree);
free_tree(tree);
return 0;
}