-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBInarySearchTree.cpp
More file actions
159 lines (143 loc) · 3.77 KB
/
BInarySearchTree.cpp
File metadata and controls
159 lines (143 loc) · 3.77 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
#include <iostream>
using namespace std;
// data structure
struct Node {
int data;
Node *left;
Node *right;
};
Node *start = NULL;
// helper function to create node element
Node* CreateNode (int data) {
Node *element = new Node;
element->data = data;
element->left = element->right = NULL;
return element;
}
// insert function
Node* Insert (Node *root, Node* element) {
if (root == NULL)
root = element;
else if (element->data < root->data)
root->left = Insert (root->left, element);
else if (root->data < element->data)
root->right = Insert (root->right, element);
return root;
}
// delete function
Node* Delete (Node *root, Node *element) {
Node *temp;
if (element->data < root->data)
root->left = Delete (root->left, element);
else if (root->data < element->data)
root->right = Delete (root->right, element);
else if (root->data == element->data) {
if ((root->left == NULL) && (root->right == NULL))
root = NULL; // no child nodes
else if (root->left == NULL) // one child node
root = root->right;
else if (root->right == NULL)
root = root->left;
else { // two child node
temp = root->right;
while (temp->left != NULL) {
temp = temp->left;
}
// temp->left = root->left; temp->right = root->right;
root->data = temp->data;
root->right = Delete (root->right, temp);
}
delete element;
}
return root;
}
// traverse tree
// inorder traversal
void InOrder (Node *root) {
if (root->left != NULL) InOrder (root->left);
cout << root->data << " ";
if (root->right != NULL) InOrder (root->right);
}
// preorder traversal. Thanks to BHups!
void PreOrder (Node *root) {
cout << root->data << " ";
if (root->left != NULL) PreOrder (root->left);
if (root->right != NULL) PreOrder (root->right);
}
// wrapping inorder, preorder printing
void Print (Node *root) {
cout << "Tree in PRE-ORDER traversal: " << endl;
PreOrder (root);
cout << "\nTree in IN-ORDER traversal: " << endl;
InOrder (root);
cout << endl;
}
// sub-function for searching
int Exists (Node *root, int data) {
if (root->data == data) return 1;
else if ((data < root->data) && (root->left != NULL))
return Exists (root->left, data);
else if ((root->data < data) && (root->right != NULL))
return Exists (root->right, data);
else return 0;
}
// search function using Exists function
void Search (Node *root, int data) {
if (!Exists (root, data)) cout << data << " not found!" << endl;
else cout << data << " found!" << endl;
}
// function to initialise the tree
Node* Create (Node *root) {
int N, data;
cout << "Enter number of elements to be inserted: ";
cin >> N;
cout << "Enter elements: ";
for (int i = 0; i < N ; i++) {
cin >> data;
root = Insert (root, CreateNode (data));
}
Print (root);
return root;
}
// Tree function
void Tree () {
int data, choice;
char ans = 'Y';
cout << "Create Tree: " << endl;
start = Create (start);
do {
cout << "1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1: cout << "Enter data: ";
cin >> data;
start = Insert (start, CreateNode (data));
cout << "Data element inserted!" << endl;
break;
case 2: cout << "Enter data to be deleted: ";
cin >> data;
if (Exists (start, data)) {
start = Delete (start, CreateNode (data));
cout << "Data element deleted!" << endl;
}
else cout << data << " not in tree. Can't delete." << endl;
break;
case 3: cout << "Enter data to be searched: ";
cin >> data;
Search (start, data);
break;
case 4: Print (start);
break;
case 5: cout << "Exiting..." << endl;
return; // exit (1);
default: cout << "Invalid input!" << endl;
}
cout << "Continue? (Y/N): ";
cin >> ans;
} while (ans == 'Y' || ans == 'y');
}
int main (void) {
Tree ();
return 0;
}