-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-tree.c
105 lines (86 loc) · 1.63 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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include "lib-binary-tree.h"
int QUIT_CMD = 9;
int value_from_stdin()
{
printf(" Enter your value: ");
int value;
scanf("%d", &value);
return value;
}
bool tree_is_empty(Node *root)
{
if (root->data == NULL)
{
printf(" Tree is empty\n");
return true;
}
return false;
}
void print(Node *root)
{
print_in_order(root);
}
void add_to_tree(Node *root)
{
insert(root, value_from_stdin());
}
Node* find_value(Node *root)
{
return find(root, value_from_stdin());
}
void delete_value(Node *root)
{
delete(root, value_from_stdin());
}
int main(void)
{
int choice;
Node *root = new_node(NULL);
do
{
printf("0: Print\n");
printf("1: Insert\n");
printf("2. Find\n");
printf("3: Delete\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 0:
if (!tree_is_empty(root))
{
printf("Printing the binary tree...\n");
print(root);
}
break;
case 1:
printf("Inserting...\n");
add_to_tree(root);
break;
case 2:
if (!tree_is_empty(root))
{
printf("Finding...\n");
Node *find_result = find_value(root);
if (find_result == NULL)
{
printf(" Value doesn't exist\n");
}
else
{
printf(" Found it!\n");
}
}
break;
case 3:
if (!tree_is_empty(root))
{
printf("Deleting...\n");
delete_value(root);
}
break;
}
} while (choice != QUIT_CMD);
}