-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.cpp
97 lines (89 loc) · 2.37 KB
/
BST.cpp
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
// BST using double pointers
#include<bits/stdc++.h>
using namespace std;
typedef struct tree{
struct tree *left;
int data;
struct tree *right;
} stree;
void createNode(int n, stree **temp){
*temp = new tree;
if(*temp != NULL){
(*temp)->data = n;
(*temp)->left = NULL;
(*temp)->right = NULL;
cout << "\nNode Inserted!\n";
}
else
cout << "\nMemory Not Allocated!\n";
}
void insertNode(stree **root, stree **temp){
if((*temp)->data < (*root)->data){
if((*root)->left == NULL)
(*root)->left = *temp;
else
insertNode(&((*root)->left), &(*temp));
}
else{
if((*root)->right == NULL)
(*root)->right = *temp;
else
insertNode(&((*root)->right), &(*temp));
}
}
void preOrderDisp(stree *root){
if(root != NULL){
cout << root->data << " ";
preOrderDisp(root->left);
preOrderDisp(root->right);
}
}
void inOrderDisp(stree *root){
if(root != NULL){
inOrderDisp(root->left);
cout << root->data << " ";
inOrderDisp(root->right);
}
}
void postOrderDisp(stree *root){
if(root != NULL){
postOrderDisp(root->left);
postOrderDisp(root->right);
cout << root->data << " ";
}
}
int main(){
stree *root = NULL, *temp = NULL;
int ch;
do{
cout << "\n\tMENU\n1. Insert\n2. Display in Pre-order\n3. Display in in-order\n4. Display in Post-Order\n5. Exit\nEnter your Choice: ";
cin >> ch;
if(ch == 1){
int n;
cout << "Enter Data: ";
cin >> n;
createNode(n, &temp);
if(root == NULL)
root = temp;
else
insertNode(&root, &temp);
}
else if(root == NULL && ch != 5){
cout << "Tree Underflow!";
}
else if(ch == 2){
cout << "Tree in Pre-Order: ";
preOrderDisp(root);
}
else if (ch == 3){
cout << "Tree in In-Order: ";
inOrderDisp(root);
}
else if (ch == 4){
cout << "Tree in Post-Order: ";
postOrderDisp(root);
}
else if (ch != 5)
cout << "Invalid Choice!\n";
} while (ch != 5);
}