-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.cpp
176 lines (137 loc) · 3.6 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
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
/*
@Chiranjeevi
Date: 12-Jun-2017
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
using namespace std;
struct node{
int data;
int count;
node *left;
node *right;
};
node *newNode (int data){
node *temp = new node;
temp->data = data;
temp->count = 1;
temp->left = temp->right = NULL;
return temp;
}
int returnCount (node *ptr){
if (ptr==NULL)
return 0;
else
return ptr->count;
}
node *insert (int data, node *ptr){
if (ptr == NULL)
return newNode(data);
else if (data < ptr->data)
ptr->left = insert(data, ptr->left);
else if (data >= ptr->data)
ptr->right = insert(data, ptr->right);
ptr->count += 1;
return ptr;
}
bool search (int data, node *ptr){
if (ptr == NULL)
return false;
else if (data < ptr->data)
return search(data, ptr->left);
else if (data > ptr->data)
return search(data, ptr->right);
else return true;
}
int floor (int data, node *ptr){
if (ptr == NULL)
return INT_MIN;
else if (data < ptr->data)
return floor(data, ptr->left);
else if (data > ptr->data)
return max(ptr->data, floor(data, ptr->right));
else
return data;
}
int ceil (int data, node *ptr){
if (ptr == NULL)
return INT_MAX;
else if(data < ptr->data)
return min(ptr->data, ceil(data, ptr->left));
else if (data > ptr->data)
return ceil(data, ptr->right);
else
return data;
}
int rankOfNode (int data, node *ptr){
if (ptr == NULL)
return 0;
else if (data < ptr->data )
return rankOfNode(data, ptr->left);
else if (data > ptr->data)
return 1 + returnCount(ptr->left) + rankOfNode(data, ptr->right);
else
return returnCount(ptr->left);
}
void inorder (node *ptr){
if(ptr == NULL)
return;
inorder(ptr->left);
cout<<char(ptr->data)<<" ";
inorder(ptr->right);
}
node *deleteNode (int data, node *ptr){
if (ptr == NULL)
return ptr;
else if (data < ptr->data)
ptr->left = deleteNode(data, ptr->left);
else if (data > ptr->data)
ptr->right = deleteNode(data, ptr->right);
else{
if (ptr->left == NULL)
return ptr->right;
if (ptr->right == NULL)
return ptr->left;
node *temp = ptr->right;
while (temp->left != NULL)
temp = temp->left;
ptr->data = temp->data;
ptr->right = deleteNode(temp->data, ptr->right);
}
ptr->count -= 1;
return ptr;
}
void levelorder (node *ptr){
queue<node *> levelt;
node *temp;
if (ptr != NULL)
levelt.push(ptr);
while (!levelt.empty()){
temp = levelt.front();
levelt.pop();
cout<<char(temp->data)<<" ";
if(temp->left != NULL)
levelt.push(temp->left);
if(temp->right != NULL)
levelt.push(temp->right);
}
}
int main(){
node *root = NULL;
root = insert('S', root);
root = insert('E', root);
root = insert('X', root);
root = insert('A', root);
root = insert('R', root);
root = insert('C', root);
root = insert('H', root);
root = insert('G', root);
root = insert('M', root);
inorder(root);
return 0;
}