Skip to content

Commit befd043

Browse files
Trees in C++
1 parent 57e8e2e commit befd043

11 files changed

+3593
-0
lines changed

trees/AVLTree.cpp

Lines changed: 561 additions & 0 deletions
Large diffs are not rendered by default.

trees/BTree.cpp

Lines changed: 536 additions & 0 deletions
Large diffs are not rendered by default.

trees/BinarySearchTree.cpp

Lines changed: 518 additions & 0 deletions
Large diffs are not rendered by default.

trees/BinaryTree.cpp

Lines changed: 423 additions & 0 deletions
Large diffs are not rendered by default.

trees/BuildHeap.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//BuildHeap.cpp : Program to build the heap.
2+
3+
#include<iostream>
4+
using namespace std;
5+
6+
void restoreDown(int i, int arr[], int n)
7+
{
8+
int k = arr[i];
9+
int lchild = 2*i;
10+
int rchild = lchild+1;
11+
12+
while(rchild <= n)
13+
{
14+
if(k>=arr[lchild] && k>=arr[rchild])
15+
{
16+
arr[i] = k;
17+
return;
18+
}
19+
else if(arr[lchild] > arr[rchild])
20+
{
21+
arr[i] = arr[lchild];
22+
i = lchild;
23+
}
24+
else
25+
{
26+
arr[i] = arr[rchild];
27+
i = rchild;
28+
}
29+
lchild = i*2;
30+
rchild = lchild+1;
31+
}//End of while
32+
33+
//If number of nodes is even
34+
if(lchild==n && k<arr[lchild])
35+
{
36+
arr[i] = arr[lchild];
37+
i = lchild;
38+
}
39+
40+
arr[i] = k;
41+
}//End of restoreDown()
42+
43+
void buildHeapBottomUp(int arr[], int n)
44+
{
45+
for(int i=n/2; i>=1; i--)
46+
restoreDown(i, arr, n);
47+
}//End of buildHeapBottomUp()
48+
49+
void restoreUp(int i, int arr[])
50+
{
51+
int k = arr[i];
52+
int iParent = i/2;
53+
54+
while(arr[iParent] < k)
55+
{
56+
arr[i] = arr[iParent];
57+
i = iParent;
58+
iParent = i/2;
59+
}
60+
arr[i] = k;
61+
}//End of restoreUp()
62+
63+
void buildHeapTopDown(int arr[], int n)
64+
{
65+
for(int i=2; i<=n; i++)
66+
restoreUp(i, arr);
67+
}//End of buildHeapTopDown()
68+
69+
int main()
70+
{
71+
int arr1[] = {9999, 25, 35, 18, 9, 46, 70, 48, 23, 78, 12, 95};
72+
int n1 = 11;
73+
74+
cout << "Building Heap Botton Up :\n";
75+
buildHeapBottomUp(arr1, n1);
76+
77+
for(int i=1; i<=n1; i++)
78+
cout << arr1[i] << " ";
79+
cout << "\n";
80+
81+
int arr2[] = {9999, 25, 35, 18, 9, 46, 70, 48, 23, 78, 12, 95};
82+
int n2 = 11;
83+
84+
cout << "Building Heap Top Down :\n";
85+
buildHeapTopDown(arr2, n2);
86+
87+
for(int i=1; i<=n2; i++)
88+
cout << arr2[i] << " ";
89+
cout << "\n";
90+
91+
return 0;
92+
}//End of main()
93+

trees/ExpressionTree.cpp

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
//ExpressionTree.cpp : Program for Expression Tree.
2+
3+
#include<iostream>
4+
#include<string>
5+
#include<stack>
6+
using namespace std;
7+
8+
class Node
9+
{
10+
public:
11+
char info;
12+
Node *lchild;
13+
Node *rchild;
14+
15+
Node(char ch)
16+
{
17+
info = ch;
18+
lchild = NULL;
19+
rchild = NULL;
20+
}
21+
22+
};//End of class Node
23+
24+
class ExpressionTree
25+
{
26+
private:
27+
Node *root;
28+
bool isOperator(char ch);
29+
void preorder(Node *ptr);
30+
void postorder(Node *ptr);
31+
void inorder(Node *ptr);
32+
void display(Node *ptr, int level);
33+
int evaluate(Node *ptr);
34+
void destroy(Node *ptr);
35+
36+
public:
37+
ExpressionTree();
38+
~ExpressionTree();
39+
void buildTree(string postfix);
40+
void prefix();
41+
void postfix();
42+
void parenthesizedInfix();
43+
void display();
44+
int evaluate();
45+
46+
};//End of class ExpressionTree
47+
48+
ExpressionTree::ExpressionTree()
49+
{
50+
root = NULL;
51+
}//End of ExpressionTree()
52+
53+
ExpressionTree::~ExpressionTree()
54+
{
55+
destroy(root);
56+
root = NULL;
57+
}//End of ExpressionTree()
58+
59+
void ExpressionTree::destroy(Node *ptr)
60+
{
61+
if(ptr == NULL)
62+
return;
63+
destroy(ptr->lchild);
64+
destroy(ptr->rchild);
65+
66+
delete ptr;
67+
}//End of destroy()
68+
69+
bool ExpressionTree::isOperator(char c)
70+
{
71+
if(c=='+' || c=='-' || c=='*' || c=='/')
72+
return true;
73+
return false;
74+
}//End of isOperator();
75+
76+
void ExpressionTree::buildTree(string postfix)
77+
{
78+
stack<Node *> treeStack;
79+
Node *t;
80+
81+
for(int i=0; i<postfix.length(); i++)
82+
{
83+
if(isOperator(postfix[i]))
84+
{
85+
t = new Node(postfix[i]);
86+
t->rchild = treeStack.top();
87+
treeStack.pop();
88+
t->lchild = treeStack.top();
89+
treeStack.pop();
90+
treeStack.push(t);
91+
}
92+
else //operand
93+
{
94+
t = new Node(postfix[i]);
95+
treeStack.push(t);
96+
}
97+
}
98+
99+
root = treeStack.top();
100+
treeStack.pop();
101+
102+
}//End of buildTree()
103+
104+
void ExpressionTree::preorder(Node *ptr)
105+
{
106+
if(ptr == NULL) //Base case
107+
return;
108+
109+
cout << ptr->info;
110+
preorder(ptr->lchild);
111+
preorder(ptr->rchild);
112+
}//End of preorder()
113+
114+
void ExpressionTree::prefix()
115+
{
116+
preorder(root);
117+
cout << "\n";
118+
}//End of prefix()
119+
120+
void ExpressionTree::postorder(Node *ptr)
121+
{
122+
if(ptr == NULL) //Base case
123+
return;
124+
125+
postorder(ptr->lchild);
126+
postorder(ptr->rchild);
127+
cout << ptr->info;
128+
}//End of postorder()
129+
130+
void ExpressionTree::postfix()
131+
{
132+
postorder(root);
133+
cout << "\n";
134+
}//End of postfix()
135+
136+
void ExpressionTree::inorder(Node *ptr)
137+
{
138+
if(ptr == NULL) //Base case
139+
return;
140+
141+
if(isOperator(ptr->info))
142+
cout << "(";
143+
144+
inorder(ptr->lchild);
145+
cout << ptr->info;
146+
inorder(ptr->rchild);
147+
148+
if(isOperator(ptr->info))
149+
cout << ")";
150+
151+
}//End of inorder()
152+
153+
void ExpressionTree::parenthesizedInfix()
154+
{
155+
inorder(root);
156+
cout << "\n";
157+
}//End of parenthesizedInfix()
158+
159+
void ExpressionTree::display(Node *ptr, int level)
160+
{
161+
if(ptr == NULL)
162+
return;
163+
164+
display(ptr->rchild, level+1);
165+
cout << "\n";
166+
167+
for(int i=0; i<level; i++)
168+
cout << " ";
169+
cout << ptr->info;
170+
171+
display(ptr->lchild, level+1);
172+
173+
}//End of display()
174+
175+
void ExpressionTree::display()
176+
{
177+
display(root, 0);
178+
cout << "\n";
179+
}//End of display()
180+
181+
int ExpressionTree::evaluate(Node *ptr)
182+
{
183+
if(!isOperator(ptr->info))
184+
return ptr->info - 48;
185+
186+
int leftValue = evaluate(ptr->lchild);
187+
int rightValue = evaluate(ptr->rchild);
188+
189+
if(ptr->info == '+')
190+
return leftValue + rightValue;
191+
else if(ptr->info == '-')
192+
return leftValue - rightValue;
193+
else if(ptr->info == '*')
194+
return leftValue * rightValue;
195+
else if(ptr->info == '/')
196+
return leftValue / rightValue;
197+
198+
}//End of evaluate()
199+
200+
int ExpressionTree::evaluate()
201+
{
202+
if(root == NULL)
203+
return 0;
204+
205+
return evaluate(root);
206+
}//End of evaluate()
207+
208+
int main()
209+
{
210+
ExpressionTree expTree;
211+
212+
string postfix = "54+12*3*-";
213+
214+
expTree.buildTree(postfix);
215+
expTree.display();
216+
217+
cout << "Prefix : ";
218+
expTree.prefix();
219+
220+
cout << "Postfix : ";
221+
expTree.postfix();
222+
223+
cout << "Infix : ";
224+
expTree.parenthesizedInfix();
225+
226+
cout << "Evaluated Value : " << expTree.evaluate() << "\n";
227+
228+
return 0;
229+
}//End of main()
230+

0 commit comments

Comments
 (0)