-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Search Tree.cpp
182 lines (159 loc) · 3.88 KB
/
Binary Search Tree.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
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
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
Node *createNode(int data)
{
Node *newNode = (Node *)malloc(sizeof(Node));
if(newNode == NULL){
cout << "Unable to allocate memory\n";
exit(0);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node *SearchKey(Node *root, int key)
{
Node *current = root, *parent = root;
while(current != NULL){
parent = current;
if(current->data == key){
return NULL;
}
else if(current->data> key){
current = current->left;
}
else if(current->data < key){
current = current->right;
}
}
return parent;
}
void insertNode(Node *root, int data)
{
Node *parent = SearchKey(root, data), *newNode = createNode(data);
if(parent == NULL){
cout << "Duplicate Element not allowed in BST\n";
}
else if(parent->data > data){
parent->left = newNode;
}
else if(parent->data < data){
parent->right = newNode;
}
}
void PreOrder(Node *root){
cout << root->data << "\t";
if(root->left != NULL){
PreOrder(root->left);
}
if(root->right != NULL){
PreOrder(root->right);
}
}
void PostOrder(Node *root){
if(root->left != NULL){
PostOrder(root->left);
}
if(root->right != NULL){
PostOrder(root->right);
}
cout << root->data << "\t";
}
void InOrder(Node *root){
if(root->left != NULL){
InOrder(root->left);
}
cout << root->data << "\t";
if(root->right != NULL){
InOrder(root->right);
}
}
int InternalPathLength(Node* root) {
if (root == NULL) {
return 0;
}
queue<Node*> q;
q.push(root);
int pathLen = 0;
int level = 0;
while (!q.empty()) {
int size = q.size();
while (size--) {
Node* node = q.front();
q.pop();
pathLen += level;
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
level += 1;;
}
return pathLen;
}
int ExternalPathLength(Node *root)
{
int pathLength = 0;
int level = 0;
queue<Node *>q;
q.push(root);
while(!q.empty()){
int n = q.size();
while(n--){
Node *node = q.front();
q.pop();
if(node->left == NULL && node->right == NULL){
pathLength += level;
}
if(node->left != NULL){
q.push(node->left);
}
if(node->right != NULL){
q.push(node->right);
}
}
level += 1;
}
return pathLength;
}
int main()
{
string data;
cout << "Enter root Node : ";
cin >> data;
Node *root = createNode(stoi(data));
cout << "Enter more nodes:(Enter any english alphabet to exit)\n";
while(true){
cin >> data;
if((data[0] >= 'A' && data[0] <= 'Z') || (data[0] >= 'a' && data[0] <= 'z')){
break;
}
insertNode(root ,stoi(data));
}
cout << "PreOrder Traverse: ";
PreOrder(root);
cout << "\n";
cout << "PostOrder Traverse: ";
PostOrder(root);
cout << "\n";
cout << "InOrder Traverse: ";
InOrder(root);
cout << "\n";
cout << "Internal Path Length: " << InternalPathLength(root) << endl;
cout << "External Path Length: " << ExternalPathLength(root)<< endl;
if(SearchKey(root, 3)){
cout << "Item Dose not exist\n" << endl;
}
else{
cout << "Item Exist\n";
}
return 0;
}