-
Notifications
You must be signed in to change notification settings - Fork 0
/
C++_OOP_Binary_Search_Tree
150 lines (150 loc) · 2.58 KB
/
C++_OOP_Binary_Search_Tree
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
#include<iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(){};
Node(int& data);
};
Node::Node(int &data) {
this->data = data;
this->left = this->right = NULL;
}
class Queue {
Node* arr;
int rear, front;
public:
Queue();
void Enqueue(int &v);
Node dequeue();
void print();
};
Queue::Queue() {
arr = new Node[1000];
this->rear = this->front = -1;
}
void Queue::Enqueue(int& v){
this->rear++;
arr[this->rear] = v;
}
Node Queue::dequeue() {
this->front++;
return this->arr[this->front];
}
void Queue::print() {
int k = front;
while (k != rear) {
cout << arr[k].data << " ";
k++;
}
cout << arr[rear].data << endl;
}
class BST {
Node* root;
int rh,lh;
public:
BST() {
this->root = NULL;
this->rh = this->lh = 0;
}
bool insert(int v) {
Node* nnew = new Node(v);
if (root == NULL) {
root = nnew;
return true;
}
else {
Node* curr = root,*prev=NULL;
while (curr != NULL){
prev = curr;
if (curr->data > v) {
lh += 1;
curr = curr->left;
}
else if(curr->data<v){
rh += 1;
curr = curr->right;
}
else {
cout << " Element already exist ! ";
return false;
}
}
if (prev->data > v) {
prev->left = nnew;
return true;
}
else {
prev->right = nnew;
return true;
}
}
}
BST(const BST &a){
inorder_copy(a.root,root);
}
void inorder_copy(Node*src,Node*&dest) {
if (src == NULL) {
dest = NULL;
}
else {
dest = new Node(src->data);
inorder_copy(src->left,dest->left);
inorder_copy(src->right,dest->right);
}
}
void inorder_print(Node*curr) {
if (curr != NULL) {
inorder_print(curr->left);
visit(curr);
inorder_print(curr->right);
}
}
void visit(Node* curr) { cout << curr->data << " "; }
void wrapper_print() {
inorder_print(root);
}
int wrapper_height(){
if (lh > rh)
return (lh-1);
return (rh-1);
}
void levelorderPrint(Node*curr,int level) {
if (curr == NULL)
return;
if (level == 0)
cout << curr->data << " ";
else {
levelorderPrint(curr->left,level-1);
levelorderPrint(curr->right, level - 1);
}
}
void wrapper_level() {
for (int i = 0; i < wrapper_height(); i++){
levelorderPrint(root, i);
}
}
/*void levelorderPrint(Node*curr) {
curr = root;
while
}*/
};
int main() {
BST b1;
b1.insert(43);
b1.insert(54);
b1.insert(22);
b1.insert(10);
b1.insert(66);
b1.insert(21);
b1.wrapper_print();
BST b2(b1);
cout << "\n After Deep Copy : \n";
b2.wrapper_print();
cout << endl<<" Level order print is : ";
b1.wrapper_level();
//b1.wrapper_inorder();
//BST b2 = b1;
return 0;
}