-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expressions.cpp
74 lines (67 loc) · 1.91 KB
/
Expressions.cpp
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
#include "Expressions.h"
binaryNode::binaryNode(const std::string &currNodeVal, binaryNode *lptr, binaryNode *rptr) : treeNode(currNodeVal)
{
this -> leftPtr = lptr;
this -> rightPtr = rptr;
}
binaryNode::binaryNode(const std::string &currNodeVal) : treeNode(currNodeVal)
{
this -> leftPtr = NULL;
this -> rightPtr = NULL;
}
// binaryNode::binaryNode(integerNode ptr) : treeNode(ptr -> getNodeValue())
// {
// printf("CONSTRUCTOR CALLED\n");
// this -> leftPtr = NULL;
// this -> rightPtr = NULL;
// }
integerNode::integerNode(int val) : binaryNode(std::to_string(val))
{
}
int integerNode::getIntegerNodeValue()
{
return std::stoi(this -> getNodeValue());
}
treeNode::treeNode(const std::string &value)
{
this -> nodeValue = value;
}
std::string &treeNode::getNodeValue()
{
return this -> nodeValue;
}
void binaryNode::printBinaryTree(std::string &result, binaryNode *ptr, std::string padding, std::string pointer)
{
if(ptr != NULL)
{
result += '\n';
result += padding;
result += pointer;
result += ptr -> getNodeValue();
std::string paddingBuilder = padding;
if(ptr -> rightPtr != NULL)
{
paddingBuilder += "│ ";
}
else
{
paddingBuilder += " ";
}
std::string paddingForBoth = paddingBuilder;
std::string pointerForRight = "└──";
std::string pointerForLeft = (ptr -> rightPtr != NULL) ? "├──" : "└──";
printBinaryTree(result, ptr -> leftPtr, paddingForBoth, pointerForLeft);
printBinaryTree(result, ptr -> rightPtr, paddingForBoth, pointerForRight);
}
}
void binaryNode::printNode()
{
std::string result;
printBinaryTree(result, this, "", "");
printf("%s\n", result.c_str());
}
void integerNode::printNode()
{
printf("Integer Node value = %d\n", this -> getIntegerNodeValue());
return;
}