Skip to content

Commit 54bb1dc

Browse files
committed
Added BST template & standards
1 parent 1d163b5 commit 54bb1dc

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

CONTRIBUTING.MD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a BST template.cpp for understanding c++11 and coding so that the code becomes readable.

template_bst.cpp

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
template <typename T>
5+
class BinaryTreeNode {
6+
public:
7+
T data;
8+
BinaryTreeNode<T> *left;
9+
BinaryTreeNode<T> *right;
10+
11+
BinaryTreeNode(T data) {
12+
this->data = data;
13+
this->left = NULL;
14+
this->right = NULL;
15+
}
16+
};
17+
18+
class BST
19+
{
20+
BinaryTreeNode<int> *root;
21+
22+
private:
23+
24+
BinaryTreeNode<int> *insert(BinaryTreeNode<int> *node,int data)
25+
{
26+
if(node == NULL)
27+
{
28+
node = new BinaryTreeNode<int>(data);
29+
return node;
30+
}
31+
if(node->data < data)
32+
{
33+
node->right = insert(node->right,data);
34+
}
35+
else if(node->data > data)
36+
{
37+
node->left = insert(node->left,data);
38+
}
39+
return node;
40+
}
41+
42+
BinaryTreeNode<int> *deleteData(BinaryTreeNode<int> *node,int data)
43+
{
44+
if(node == NULL)
45+
{
46+
return NULL;
47+
}
48+
if(node->data == data)
49+
{
50+
if(node->left == NULL && node->right == NULL)
51+
{
52+
free(node);
53+
return NULL;
54+
}
55+
56+
else if(node->left == NULL && node->right != NULL)
57+
{
58+
BinaryTreeNode<int> *nextNode = node->right;
59+
free(node);
60+
return nextNode;
61+
}
62+
63+
else if(node->left != NULL && node->right == NULL)
64+
{
65+
BinaryTreeNode<int> *nextNode = node->left;
66+
free(node);
67+
return nextNode;
68+
}
69+
70+
else if(node->left != NULL && node->right != NULL)
71+
{
72+
BinaryTreeNode<int> * inOrdSuc = node->right;
73+
74+
while(inOrdSuc->left != NULL)
75+
inOrdSuc = inOrdSuc->left;
76+
77+
node->data = inOrdSuc->data;
78+
node->right = deleteData(node->right,inOrdSuc->data);
79+
80+
return node;
81+
}
82+
}
83+
84+
if(node->data < data)
85+
node->right = deleteData(node->right,data);
86+
87+
else if(node->data > data)
88+
node->left = deleteData(node->left,data);
89+
90+
return node;
91+
92+
}
93+
94+
bool hasData(int data,BinaryTreeNode<int> *root)
95+
{
96+
if(root == NULL)
97+
return false;
98+
99+
if(root->data == data)
100+
return true;
101+
102+
if(root->data < data)
103+
{
104+
return hasData(data,root->right);
105+
}
106+
else if(root->data > data)
107+
{
108+
return hasData(data,root->left);
109+
}
110+
}
111+
112+
void printTree(BinaryTreeNode<int> *node)
113+
{
114+
if(node == NULL)
115+
return;
116+
117+
cout << node->data << ":";
118+
119+
if(node->left != NULL)
120+
cout << "L:" << node->left->data << ",";
121+
122+
if(node->right != NULL)
123+
cout << "R:" << node->right->data;
124+
cout << endl;
125+
126+
printTree(node->left);
127+
printTree(node->right);
128+
}
129+
130+
public:
131+
132+
BST()
133+
{
134+
root = NULL;
135+
}
136+
137+
void insert(int data)
138+
{
139+
root = insert(root,data);
140+
}
141+
142+
void deleteData(int data)
143+
{
144+
root = deleteData(root,data);
145+
}
146+
147+
bool hasData(int data)
148+
{
149+
return hasData(data,root);
150+
}
151+
152+
void printTree()
153+
{
154+
printTree(root);
155+
}
156+
157+
};
158+
159+
int main()
160+
{
161+
BST *tree = new BST();
162+
int choice, input;
163+
while(true){
164+
cin>>choice;
165+
switch(choice){
166+
case 1:
167+
cin >> input;
168+
tree->insert(input);
169+
break;
170+
case 2:
171+
cin >> input;
172+
tree->deleteData(input);
173+
break;
174+
case 3:
175+
cin >> input;
176+
if(tree->hasData(input)) {
177+
cout << "true" << endl;
178+
}
179+
else {
180+
cout << "false" << endl;
181+
}
182+
break;
183+
default:
184+
tree->printTree();
185+
break;
186+
}
187+
}
188+
return 0;
189+
190+
}

0 commit comments

Comments
 (0)