-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.cpp
212 lines (191 loc) · 6.26 KB
/
tree.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
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
203
204
205
206
207
208
209
210
211
212
//--------------------------------------------------------------------
// CS315-001 Assignment 2
//--------------------------------------------------------------------
// Author: Justin Hamilton
// Date: 3/9/21
// Description: Add values to a ternary tree and then print them
// Assistance: Tau Beta Pi tutoring
//--------------------------------------------------------------------
#include <iostream>
#include <climits>
using namespace std;
//Node Class
class node
{
private:
int small;
int large;
node * left;
node * middle;
node * right;
public:
friend class tree;
node();
node(int value);
};
//--------------------------------------------------------------------------------------
// node()
//--------------------------------------------------------------------------------------
node::node(){
small = INT_MAX;
large = INT_MIN;
left = NULL;
right = NULL;
middle = NULL;
}
//--------------------------------------------------------------------------------------
// node()
//--------------------------------------------------------------------------------------
// Given: some int
// Create node with said int in data
//--------------------------------------------------------------------------------------
node::node(int value)
{
small = value;
large = INT_MIN;
left = NULL;
right = NULL;
middle = NULL;
}
//Tree Class
class tree
{
private:
node * root;
public:
tree();
node * insert(node * current, int value);
void swap(node * current,int value);
void print(node * current);
node * getRoot();
};
//--------------------------------------------------------------------------------------
// tree()
//--------------------------------------------------------------------------------------
// Default Constructor
//--------------------------------------------------------------------------------------
tree::tree()
{
root = NULL;
}
//--------------------------------------------------------------------------------------
// getRoot()
//--------------------------------------------------------------------------------------
// Given: some int
// Create node with said int in data
//--------------------------------------------------------------------------------------
node * tree::getRoot(){
return root;
}
//--------------------------------------------------------------------------------------
// swap()
//--------------------------------------------------------------------------------------
// Given: node, some int
// Swaps the values in the node if new value is smaller than previous value
//--------------------------------------------------------------------------------------
void tree::swap(node * current, int value){
if (current->large == INT_MIN){
if(value < current->small){
current->large = current->small;
current->small = value;
}
else{
current->large = value;
}
}
}
//--------------------------------------------------------------------------------------
// insert()
//--------------------------------------------------------------------------------------
// Given: node, some int
// Returns: updated node
// Insert node with given value into tree according to parameters
//--------------------------------------------------------------------------------------
node * tree::insert(node * current, int value){
//Base Case
if(current == NULL){
current = new node(value);
if(root == NULL){
root = new node(value);
return root;
}
return current;
}
else{
// If value is smaller than current-> small it swaps, else it just inserts at current->large
if(current->large == INT_MIN){
swap(current,value);
}
//Insert new node if current node is filled according to parameters
else if(value <= current->small){
current->left = insert(current->left,value);
}
else if(value > current->small && value <= current->large){
current->middle = insert(current->middle,value);
}
else if(value > current->large){
current->right = insert(current->right,value);
}
return current;
}
}
//--------------------------------------------------------------------------------------
// print()
//--------------------------------------------------------------------------------------
// Given: a node
// Prints the tree with parenthesis around children
//--------------------------------------------------------------------------------------
void tree::print(node * current)
{
//If Tree is Empty
if(current == NULL){
cout << "There is no tree" <<endl;
return;
}
//Go Down Left Child
if(current->left != NULL){
cout << "(";
print(current->left);
cout << ") ";
}
//Output Child->smaller
if(current->small != INT_MAX){
cout << current->small;
}
//Go down Middle Child
if(current->middle != NULL){
cout << " (";
print(current->middle);
cout << ")";
}
//Output Child->bigger
if(current->large != INT_MIN){
cout << " " << current->large;
}
//Go down Right Child
if (current->right != NULL)
{
cout << " (";
print(current->right);
cout << ")";
}
}
//--------------------------------------------------------------------------------------
// main()
//--------------------------------------------------------------------------------------
int main(int argc, char const *argv[])
{
// New Tree
tree * ternary = new tree();
//Input numberOfInts from Parameters
int numberOfInts = stoi(argv[1]), temp;
//Insert Values into tree
for(int i = 0; i < numberOfInts; i ++){
cin >> temp;
ternary->insert(ternary->getRoot(),temp);
}
//Print
ternary->print(ternary->getRoot());
cout << endl;
return 0;
}