-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.c
166 lines (155 loc) · 3.68 KB
/
bst.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
#include<stdio.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* Newnode(int data)
{
struct Node* node = malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
int lookup(struct Node* node, int target)
// Function to search for an element
{
if(node == NULL) {
return 0;
}
if(node->data == target) {
return 1;
}
else {
if (target <= node->data) {
return lookup(node->left, target);
}
else {
return lookup(node->right, target);
}
}
}
struct Node* insertNode(struct Node* node, int data)
// Function to insert a new node to the BST
{
if(node == NULL) {
return Newnode(data);
}
else {
if (data < node->data) {
node->left = insertNode(node->left, data);
//node->left = Newnode(data);
}
else {
node->right = insertNode(node->right, data);
//node->right = Newnode(data);
}
}
return node;
}
/*else {
printf("Oops element already exists");
return(node);
}*/
int size(struct Node* node)
{
if(node == NULL) {
return(0);
} else {
return(size(node->left) + 1 + size(node->right));
}
}
void printTree(struct Node* node)
{
if (node == NULL) return;
printTree(node->left);
printf("%i ",node->data);
printTree(node->right);
}
struct Node* findMin(struct Node* node)
{
while(node->left != NULL) {
node = node->left;
}
return node;
}
struct Node* delete(struct Node* node, int data)
{
//struct Node* parent;
if(node == NULL) {
return node;
}
if (data < node->data) {
//parent = node;
node->left = delete(node->left, data);
}
if(data > node->data) {
//parent = node;
node->right = delete(node->right, data);
}
else {
if (node->left == NULL && node->right == NULL) {
free(node);
node = NULL;
} else if(node->left == NULL) {
struct Node* temp = node;
node = node->right;
free(temp);
} else if(node->right == NULL) {
struct Node* temp = node;
node = node->left;
free(temp); }
else {
struct Node* temp = findMin(node->right);
temp->left = node->left;
temp = node;
//strcpy(root->data,temp->data);
node = node->right;
//root->right = delete(root->right,temp->data);
}
//return node;
}
return node;
}
void main()
{
struct Node* root;
root = Newnode(1);
//root->data = NULL;
printf("%i\n",size(root));
printf("%i\n",lookup(root, 6));
printTree(root);
//delete(root, 8);
printf("\n");
//printTree(root);
insertNode(root, 10);
printf("\n");
printTree(root);
//int choice, item;
//printf("%i\n",root->data);
/*do {
printf("BST options:\n1.search\n2.insertion\n3.deletion\n");
scanf("%d\n",&choice);
switch(choice) {
case 1:
printf("\nInput search element:");
scanf("\n%d",&item);
printf("\n");
lookup(root, item);
break;
case 2:
printf("\nInput element to insert:");
scanf("\n%d",&item);
insertNode(root, item);
printTree(root);
printf("\n");
break;
case 3:
exit(0);
default:
printf("error:unknown");
break;
}
} while(choice != 3); */
}