-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree2.c
182 lines (154 loc) · 4.36 KB
/
Tree2.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
178
179
180
181
182
/*Binary Tree with Basic Operation
Insert - Delete - Search - Traversal*/
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *left;
struct Node *right;
} Node;
Node *createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory Allocation is Failed realllocing the memory\n");
newNode = (Node *)realloc(newNode,sizeof(newNode));
}
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
Node* findMin(Node* root) {
if (root == NULL) {
return -1;
}
Node *current = root;
while (current != NULL && current->left != NULL) {
current = current->left;
}
return current;
}
Node* findMax(Node* root) {
if (root == NULL) {
return -1;
}
Node *current = root;
while (current != NULL) {
current = current->right;
}
return current;
}
Node *insertNode(Node *root, int data)
{
if (root == NULL)
{
root = createNode(data);
return root;
}
if (root->data < data)
{
insertNode(root->left, data);
}
else if (data < root->data)
{
insertNode(root->right, data);
}
return root;
}
Node *searchNode(Node *root, int key) {
if (root == NULL || root == key) {
return root;
}
if (key < root->data){
return searchNode(root->left, key);
}
else if(key > root->data) {
return searchNode(root->right, key);
}
}
Node *deleteNode(Node *root, int key) {
if (root == NULL) {
return root;
}
/*if the key to be deleted Node less than key
It will lies on the left side off the root Node*/
if (key < root->data) {
root->left = deleteNode(root->left, key);
}
/*if the key to be deleted Node Greater than key
It will lies on the right side off the root Node*/
else if (key > root->data) {
root->right = deleteNode(root->right, key);
}
/*if The key is the same as root key, Than this is node
to be deleted*/
else {
/*If the root node have one child node*/
if (root->left == NULL) {
Node *temp = root->right;
free(root);
return temp;
}
/*If the root node have one child node*/
else if (root->right == NULL) {
Node *temp = root->left;
free(root);
return temp;
}
/*if the tree has two children Node: Get inorder successor in right subtree*/
Node *temp = findMin(root->right);
/*Copy the data of inorder successor to the root Node*/
root->data = temp->data;
/*Delete in order successor*/
root->right = deleteNode(root->right, temp->data);
}
return root;
}
void inOrder(Node* root) {
if (root == NULL ) {
return;
}
inOrder(root->left);
printf("%d -> ", root->data);
inOrder(root->right);
}
int main() {
Node *root = NULL;
int choice, key;
while (1) {
printf("\nMenu:\n1. Insert\n2. Delete\n3. Search\n4. Inorder Traversal\n5. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter key to insert: ");
scanf("%d", &key);
insertNode(root, key);
break;
case 2:
printf("Enter Key to delete: ");
scanf("%d", &key);
deleteNode(root, key);
break;
case 3:
printf("Enter key to search: ");
scanf("%d", &key);
Node *res = searchNode(root, key);
if (res == NULL) {
printf("Key %d Not Found in the Tree.\n", key);
}
else {
printf("Key %d Found in the tree.\n", key);
}
break;
case 4:
printf("In Order Traversal: ");
inOrder(root);
printf("NULL\n");
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}