-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbet.cpp
More file actions
186 lines (162 loc) · 5.4 KB
/
Copy pathbet.cpp
File metadata and controls
186 lines (162 loc) · 5.4 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "bet.h"
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
BET::BET()
{
start = nullptr; //initialize root as null pointer
}
BET::BET(const string& postfix)
{
start = nullptr; //initialize root as nullptr
buildFromPostfix(postfix); //build tree from given postfix
}
BET::BET(const BET& n)
{
start = clone(n.start); //deep copy of tree
}
BET::~BET()
{
makeEmpty(start); //deletes all nodes from tree
}
bool BET::buildFromPostfix(const string& postfix)
{
stack<BinaryNode *> stack; //stack container to hold nodes
string node; //string to hold each number of symbol
istringstream pos(postfix); //read the postfix expression
while(pos >> node) //read each symbol from postfix
{
if(isalnum(node[0])) //check if symbol is number or variable
{
BinaryNode *t = new BinaryNode(node, nullptr, nullptr); //leaf node
stack.push(t); //push node onto stack
}
else if(node == "+" || node == "-" || node == "*" || node == "/") //check if symbol is operator
{
BinaryNode *t = new BinaryNode(node, nullptr, nullptr); //new node
t->right = stack.top(); //set right node
stack.pop(); //remove top node
t->left = stack.top(); //set left node
stack.pop(); //remove top node
stack.push(t); //push new tree onto stack
}
else
{
cout << "Cannot build the tree based on postfix\n"; //error message and return false
return false;
}
}
start = stack.top(); //set tree root to node on the stack
stack.pop(); //clean stack
return true; //return true when new tree is built successfully
}
const BET & BET::operator=(const BET & n)
{
if(this != &n) //check self assignment
{
makeEmpty(start); //clear tree
}
start = clone(n.start); //deep copy the tree from n
return *this; //return by referance
}
void BET::printInfixExpression()const
{
printInfixExpression(start); //start printing from root
cout << endl;
}
void BET::printPostfixExpression()const
{
printPostfixExpression(start); //start printing from root
cout << endl;
}
size_t BET::size()const
{
return size(start); //return number of nodes in tree
}
size_t BET::leaf_nodes()const
{
return leaf_nodes(start); //return number of leaves in tree
}
bool BET::empty()const
{
if(start == nullptr) //if tree is empty return true by checking root
{
return true;
}
return false; //return false otherwise
}
void BET::printInfixExpression(BinaryNode *n)const
{
if(n == nullptr) //check if tree is empty
{
return;
}
if(n->left != nullptr && n->right != nullptr) //if node has children add parenthesis
{
cout << "(";
}
printInfixExpression(n->left); //print left subtree
cout << n->element << " "; //print current element
printInfixExpression(n->right); //print right subtree
if(n->left != nullptr && n->right != nullptr) //if node has children closes the parenthesis
{
cout << ")";
}
}
void BET::makeEmpty(BinaryNode* &t)
{
if(t != nullptr) //check if node is not null
{
makeEmpty(t->left); //deallocate left subtree
makeEmpty(t->right); //deallocate right subtree
delete t; //delete current node
}
}
BET::BinaryNode * BET::clone(BinaryNode *t)const
{
if(t == nullptr) //if node is null return null
{
return nullptr;
}
else //clone the node and left and right subtree
{
return new BinaryNode(t->element, clone(t->left), clone(t->right));
}
}
void BET::printPostfixExpression(BinaryNode *n)const
{
if(n == nullptr) //if node is null return
{
return;
}
printPostfixExpression(n->left); //print left subtree
printPostfixExpression(n->right); //print right subtree
cout << n->element << " "; //print current node
}
size_t BET::size(BinaryNode *t)const
{
if(t == nullptr) //if node is null return 0
{
return 0;
}
else //count the nodes in the tree by adding 1 for root and the counting left and right subtree
{
return 1 + size(t->left) + size(t->right);
}
}
size_t BET::leaf_nodes(BinaryNode *t)const
{
if(t == nullptr) //if node is null return 0
{
return 0;
}
if(t->left == nullptr && t->right == nullptr) //check if current node is leaf or not and return one if so
{
return 1;
}
else //count lead nodes in left and right subtree and addd them
{
return leaf_nodes(t->left) + leaf_nodes(t->right);
}
}