-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeNode.cpp
More file actions
202 lines (183 loc) · 5.78 KB
/
treeNode.cpp
File metadata and controls
202 lines (183 loc) · 5.78 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// Created by Ricky Marly on 10/25/20.
//
#include "treeNode.h"
//basic tree
treeNode::treeNode()
{
root = nullptr; //always set pointers to null when instantiating
node *left = nullptr; //always set pointers to null when instantiating
node *right = nullptr; //always set pointers to null when instantiating
}
//private version treeNode::insert
void treeNode::insertTreeNode(node* newOtherNode, int newAddInt)//public version being used to insert
{
//inserting new node to the left BST order
if(newOtherNode->getNumberData() > newAddInt)
{
if(newOtherNode->getLeft() != nullptr)//check if node is null
{
insertTreeNode(newOtherNode->getLeft(), newAddInt);//insert value to the left
}
else
{
newOtherNode->setLeft(new node(newAddInt));//node on left is null ( newAddInt ) new node
}
}
//inserting new node to the Right BST order
if(newOtherNode->getNumberData() < newAddInt)
{
if(newOtherNode->getRight() != nullptr)//check if node is null
{
insertTreeNode(newOtherNode->getRight(), newAddInt);//insert value to the right
}
else
{
newOtherNode->setRight(new node(newAddInt));//node on right is null ( newAddInt ) new node
}
}
}
//public version treeNode::insert
void treeNode::insertTreeNode(int newAddInt)//public version being used to insert
{
nodeCountVar++;//used to keep count of the number of nodes created
if(root == nullptr)
{
root = new node(newAddInt);//base case
}
else
{
insertTreeNode(root, newAddInt);
}
}
int treeNode::getTreeNodeCount()//returns the number of nodes created
{
return nodeCountVar;
}
//private version treeNode::printTreeNode
void treeNode::printTreeNode(node* newOtherNode)
{
if(newOtherNode == nullptr)//there are no nodes to print(safety Code)
{
return;
}
else
{
//Create an in-order traversal function and print out each node’s value.
numberCountVar++;
printTreeNode(newOtherNode->getRight());
numberCountVar--;
//print minuses and leaf value
for (int i = 0; i < numberCountVar; i++)
{
cout << "--";
}
cout << newOtherNode->getNumberData() << "\n";
//Create an in-order traversal function and print out each node’s value.
numberCountVar++;
printTreeNode(newOtherNode->getLeft());
numberCountVar--;
}
}
//public version treeNode::printTreeNode
void treeNode::printTreeNode()
{
printTreeNode(root);
}
//private version treeNode::findTreeNode
int treeNode::findTreeNode(node* newOtherNode, int newFindInt)
{
if(newOtherNode == nullptr)//there are no nodes to print(safery Code)
{
return -1;
}
if(newOtherNode->getNumberData() == newFindInt)//ensure the node found at the end is printed
{
cout << newOtherNode->getNumberData() << " Number found: ";
return newOtherNode->getNumberData();
}
if (newOtherNode->getNumberData() > newFindInt)//the value is located at the left of the tree(root)
{
cout << newOtherNode->getNumberData() << " ";//prints space
return findTreeNode(newOtherNode->getLeft(), newFindInt);//print node
}
if (newOtherNode->getNumberData() < newFindInt)//the value is located at the right of the tree(root)
{
cout << newOtherNode->getNumberData() << " ";//prints space
return findTreeNode(newOtherNode->getRight(), newFindInt);//print node
}
}
//public version treeNode::findTreeNode
int treeNode::findTreeNode(int newFindInt)
{
return findTreeNode(root, newFindInt);//calls & returns the method with 2 parameters being passed
}
//private version treeNode::getTreeHeight
int treeNode::getTreeHeight(node* newOtherNode)
{
if(newOtherNode == nullptr)//there are no nodes to print(safery Code)
{
return 0;
}
else
{
int hL = getTreeHeight(newOtherNode->getLeft());//left to int
int hR = getTreeHeight(newOtherNode->getRight());//right to int
if (hL > hR)
{
return (hL + 1);//returns one more to left tree
}
else
{
return (hR + 1);//returns one more to right tree
}
}
}
//public version treeNode::getTreeHeight
int treeNode::getTreeHeight()
{
return getTreeHeight(root);
}
//private version treeNode::horizontalNodePrint
void treeNode::horizontalNodePrint(node* newOtherNode, int maxNodeHeight, string * array)
{
if(newOtherNode == nullptr)//there are no nodes to print(safe Code)
{
return;
}
count++;
horizontalNodePrint(newOtherNode->getLeft(), maxNodeHeight, array);//printing on the on left recursion
for (int i = 0; i < maxNodeHeight; i++)
{
if (i == (maxNodeHeight-count))
{
if (newOtherNode->getNumberData() < 10)
{
array[i] += " ";
array[i] += to_string(newOtherNode->getNumberData());
}
else
{
array[i] += to_string(newOtherNode->getNumberData());
}
}
else
{
array[i] += " ";
}
}
horizontalNodePrint(newOtherNode->getRight(), maxNodeHeight, array);//printing on the on right recursion
count--;
}
//public version treeNode::horizontalNodePrint
void treeNode::horizontalNodePrint()
{
int maxHeight = getTreeHeight() + 1;//get height
string * array;
array = new string[maxHeight];
horizontalNodePrint(root, maxHeight, array);//recursively call horizontalNodePrint within public version
for (int i = maxHeight - 1; i > 0; i--)//print array
{
cout << array[i] << "\n";
}
}