-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbst.cpp
More file actions
157 lines (126 loc) · 2.69 KB
/
bst.cpp
File metadata and controls
157 lines (126 loc) · 2.69 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
/*
Program : Binary Search Tree Implementation
Author : © Vipin Kumar
Created on : April 13, 2018 10:47 IST
*/
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <stdlib.h>
using namespace std;
struct node {
int data;
node* left;
node* right;
};
class BinarySearchTree {
public:
node * root;
int element;
node* newNode (int);
BinarySearchTree ();
~BinarySearchTree ();
node* insertion (node*, int);
void inorder (node*);
void preorder (node*);
void postorder (node*);
bool search (node*, int);
node* findMin (node*);
};
BinarySearchTree::BinarySearchTree () {
root = NULL;
}
BinarySearchTree::~BinarySearchTree () {
delete root;
}
node* BinarySearchTree::newNode (int value) {
node* temp = new node;
temp->data = value;
temp->left = NULL;
temp->right = NULL;
return temp;
}
node* BinarySearchTree::insertion (node* root, int value) {
if (root == NULL) {
root = newNode (value);
}
else {
if (value < root->data) {
root->left = insertion(root->left, value);
}
else if (value > root->data) {
root->right = insertion (root->right, value);
}
else {
cout << "ERROR: Duplicate values not allowed in BST.";
}
}
return root;
}
void BinarySearchTree::inorder (node* root) {
if (root != NULL) {
inorder (root->left);
cout << root->data << " ";
inorder (root->right);
}
}
void BinarySearchTree::preorder (node* root) {
if (root != NULL) {
cout << root->data << " ";
preorder (root->left);
preorder (root->right);
}
}
void BinarySearchTree::postorder (node* root) {
if (root != NULL) {
postorder (root->left);
postorder (root->right);
cout << root->data << " ";
}
}
bool BinarySearchTree::search (node* root, int value) {
if (root == NULL) {
return false;
}
else {
if (value == root->data) {
return true;
}
else {
if (value < root->data) {
search (root->left, value);
}
else if (value > root->data) {
search (root->right, value);
}
}
}
}
node* BinarySearchTree::findMin (node* root) {
node* present = root;
while (present->left != NULL) {
present = present->left;
}
return present;
}
int main () {
BinarySearchTree bst;
bst.root = bst.insertion (bst.root, 10);
bst.root = bst.insertion (bst.root, 5);\
bst.root = bst.insertion (bst.root, 15);
bst.root = bst.insertion (bst.root, 2);
bst.root = bst.insertion (bst.root, 20);
bst.root = bst.insertion (bst.root, 25);
bst.root = bst.insertion (bst.root, 18);
cout << "\nInorder";
bst.inorder (bst.root);
cout << "\nPreorder";
bst.preorder (bst.root);
cout << "\nPostorder";
bst.postorder (bst.root);
cout << bst.search (bst.root, 10);
cout << bst.search (bst.root, 18);
cout << bst.search (bst.root, 19);
getch ();
return 0;
}