Skip to content

Commit 4901587

Browse files
committed
use template class in Binary_Search_Tree.cpp
1 parent 522c958 commit 4901587

File tree

1 file changed

+117
-109
lines changed

1 file changed

+117
-109
lines changed

3_Data_Structures/12_Binary_Search_Tree/Binary_Search_Tree.cpp

Lines changed: 117 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -16,157 +16,165 @@ left sub-tree, and less than or equal to any key stored in the right sub-tree.
1616

1717
using namespace std;
1818

19+
template<class DataType>
1920
class Node{
20-
public:
21-
Node *parent, *left, *right;
22-
int value;
23-
24-
Node(Node *Parent = NULL, int Value = 0, Node *Left = NULL, Node *Right = NULL){
25-
parent = Parent;
26-
value = Value;
27-
left = Left;
28-
right = Right;
29-
}
21+
public:
22+
Node<DataType> *parent, *left, *right;
23+
DataType value;
24+
25+
Node(Node<DataType> *parent, DataType value,
26+
Node<DataType> *left = NULL, Node<DataType> *right = NULL) {
27+
this->parent = parent;
28+
this->value = value;
29+
this->left = left;
30+
this->right = right;
31+
}
32+
3033
};
3134

35+
template<class DataType>
3236
class BST{
33-
private:
34-
Node *root;
35-
public:
36-
BST(){
37-
root = NULL;
38-
}
39-
bool is_empty(){
40-
return root == NULL;
41-
}
42-
void insert(int val){
43-
if( is_empty() )
44-
root = new Node(NULL, val);
45-
else{
46-
Node *current = root, *parent;
47-
while( current != NULL ){
48-
parent = current;
49-
current = ( val <= current->value ) ? current->left : current->right;
50-
}
51-
52-
if( val <= parent->value )
53-
parent->left = new Node(parent, val);
54-
else
55-
parent->right = new Node(parent, val);
37+
private:
38+
Node<DataType> *root;
39+
40+
public:
41+
BST() {
42+
root = NULL;
43+
}
44+
45+
bool is_empty() {
46+
return root == NULL;
47+
}
48+
49+
void insert(DataType val) {
50+
51+
if( is_empty() )
52+
root = new Node<DataType>(NULL, val);
53+
else {
54+
Node<DataType> *current = root, *parent;
55+
while( current != NULL ) {
56+
parent = current;
57+
current = ( val <= current->value ) ? current->left : current->right;
5658
}
59+
60+
if( val <= parent->value )
61+
parent->left = new Node<DataType>(parent, val);
62+
else
63+
parent->right = new Node<DataType>(parent, val);
5764
}
65+
}
5866

59-
void preorder(Node *current = NULL){
60-
if( current == NULL ) current = root;
67+
void preorder(Node<DataType> *current = NULL){
68+
if( current == NULL ) current = root;
6169

62-
// Pre-Order = (Root, Left, Right)
63-
if(current != NULL){
64-
cout<<current->value <<", ";
65-
if( current->left != NULL ) preorder(current->left);
66-
if( current->right != NULL ) preorder(current->right);
67-
}
70+
// Pre-Order = (Root, Left, Right)
71+
if(current != NULL){
72+
cout<<current->value <<", ";
73+
if( current->left != NULL ) preorder(current->left);
74+
if( current->right != NULL ) preorder(current->right);
6875
}
76+
}
6977

70-
void inorder(Node *current = NULL){
71-
if( current == NULL ) current = root;
78+
void inorder(Node<DataType> *current = NULL){
79+
if( current == NULL ) current = root;
7280

73-
// In-Order = (Left, Root, Right)
74-
if( current != NULL ){
75-
if( current->left != NULL ) inorder(current->left);
76-
cout<<current->value <<", ";
77-
if( current->right != NULL ) inorder(current->right);
78-
}
81+
// In-Order = (Left, Root, Right)
82+
if( current != NULL ){
83+
if( current->left != NULL ) inorder(current->left);
84+
cout<<current->value <<", ";
85+
if( current->right != NULL ) inorder(current->right);
7986
}
87+
}
8088

81-
void postorder(Node *current = NULL){
82-
if( current == NULL ) current = root;
89+
void postorder(Node<DataType> *current = NULL){
90+
if( current == NULL ) current = root;
8391

84-
// Post-Order = (Left, Right, Root)
85-
if( current != NULL ){
86-
if( current->left != NULL ) postorder(current->left);
87-
if( current->right != NULL ) postorder(current->right);
88-
cout<<current->value <<", ";
89-
}
92+
// Post-Order = (Left, Right, Root)
93+
if( current != NULL ){
94+
if( current->left != NULL ) postorder(current->left);
95+
if( current->right != NULL ) postorder(current->right);
96+
cout<<current->value <<", ";
9097
}
98+
}
9199

92-
void levelorder(Node *current = NULL){
100+
void levelorder(Node<DataType> *current = NULL){
93101

94-
vector<Node*> next_level;
95-
96-
if( current == NULL and root != NULL ){
97-
next_level.push_back(root);
98-
}
102+
vector<Node<DataType>*> next_level;
99103

100-
while( next_level.size() > 0 ){
101-
cout <<next_level[0]->value <<", ";
104+
if( current == NULL and root != NULL ){
105+
next_level.push_back(root);
106+
}
102107

103-
if( next_level[0]->left != NULL ){
104-
next_level.push_back(next_level[0]->left);
105-
}
106-
if( next_level[0]->right != NULL ){
107-
next_level.push_back(next_level[0]->right);
108-
}
108+
while( next_level.size() > 0 ){
109+
cout <<next_level[0]->value <<", ";
109110

110-
next_level.erase(next_level.begin());
111+
if( next_level[0]->left != NULL ){
112+
next_level.push_back(next_level[0]->left);
113+
}
114+
if( next_level[0]->right != NULL ){
115+
next_level.push_back(next_level[0]->right);
111116
}
117+
118+
next_level.erase(next_level.begin());
112119
}
120+
}
113121

114-
Node *search(int find){
115-
Node *current = root;
122+
Node<DataType> *search(DataType find){
123+
Node<DataType> *current = root;
116124

117-
while( current != NULL and current->value != find )
118-
current = (current->value < find) ? current->right : current->left;
125+
while( current != NULL and current->value != find )
126+
current = (current->value < find) ? current->right : current->left;
119127

120-
return current;
121-
}
128+
return current;
129+
}
122130

123-
Node *maximum(Node *current = NULL){
124-
if( current == NULL ) current = root;
131+
Node<DataType> *maximum(Node<DataType> *current = NULL){
132+
if( current == NULL ) current = root;
125133

126-
while( current != NULL and current->right != NULL ) current = current->right;
134+
while( current != NULL and current->right != NULL ) current = current->right;
127135

128-
return current;
129-
}
136+
return current;
137+
}
130138

131-
Node *minimum(Node *current = NULL){
132-
if( current == NULL ) current = root;
139+
Node<DataType> *minimum(Node<DataType> *current = NULL){
140+
if( current == NULL ) current = root;
133141

134-
while( current != NULL and current->left != NULL ) current = current->left;
142+
while( current != NULL and current->left != NULL ) current = current->left;
135143

136-
return current;
137-
}
144+
return current;
145+
}
138146

139-
Node *successor(Node *current){
147+
Node<DataType> *successor(Node<DataType> *current){
140148

141-
if( current == NULL ) return NULL;
149+
if( current == NULL ) return NULL;
142150

143-
if( current->right == NULL ){
144-
while( current->parent != NULL and current->parent->right == current )
145-
current = current->parent;
146-
return current->parent;
147-
}
148-
else
149-
return minimum(current->right);
151+
if( current->right == NULL ){
152+
while( current->parent != NULL and current->parent->right == current )
153+
current = current->parent;
154+
return current->parent;
150155
}
156+
else
157+
return minimum(current->right);
158+
}
151159

152-
Node *predecessor(Node *current){
160+
Node<DataType> *predecessor(Node<DataType> *current){
153161

154-
if( current == NULL ) return NULL;
162+
if( current == NULL ) return NULL;
155163

156-
if( current->left == NULL ){
157-
while( current->parent != NULL and current->parent->left == current )
158-
current = current->parent;
159-
return current->parent;
160-
}
161-
else
162-
return maximum(current->left);
164+
if( current->left == NULL ){
165+
while( current->parent != NULL and current->parent->left == current )
166+
current = current->parent;
167+
return current->parent;
163168
}
169+
else
170+
return maximum(current->left);
171+
}
164172
};
165173

166174
int main(){
167175

168176
int ins_values[] = {28, 33, 21, 37, 21, 47, 40, 22, 1, 16};
169-
BST a;
177+
BST<int> a;
170178

171179
for(int i=0; i<10; i++)
172180
a.insert(ins_values[i]);
@@ -181,7 +189,7 @@ int main(){
181189
a.levelorder();
182190

183191
int search = 33;
184-
Node *srh = a.search(search);
192+
Node<int> *srh = a.search(search);
185193
if( ! srh )
186194
cout <<"\n\n" <<search <<" not found in tree";
187195
else
@@ -190,7 +198,7 @@ int main(){
190198
cout<<"\n\nMax node = " <<a.maximum()->value;
191199
cout<<"\nMin node = " <<a.minimum()->value;
192200

193-
Node *temp = a.successor(a.search(22));
201+
Node<int> *temp = a.successor(a.search(22));
194202
if( temp != NULL ) cout<<"\n\nSuccessor of 47 = " <<temp->value;
195203
temp = a.predecessor(a.search(22));
196204
if( temp != NULL ) cout<<"\nPredecessor of 47 = " <<temp->value;

0 commit comments

Comments
 (0)