-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScapeGoatUtilities.h
171 lines (154 loc) · 4.04 KB
/
ScapeGoatUtilities.h
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
#pragma once
#include <iostream>
#include <cmath>
class ScapeGoatNode
{
public:
ScapeGoatNode *right, *left, *parent;
int key;
int object;
ScapeGoatNode()
{
key = 0;
object = 0;
right = nullptr;
left = nullptr;
parent = nullptr;
}
ScapeGoatNode(int k, int o)
{
key = k;
object = 0;
right = nullptr;
left = nullptr;
parent = nullptr;
}
~ScapeGoatNode(){
delete left;
delete right;
}
};
void traversePreorder(ScapeGoatNode* rootNode){
using namespace std;
if (rootNode != nullptr)
{
cout << rootNode->key << " --> " << rootNode->object << endl;
if (rootNode->left != nullptr)
{
traversePreorder(rootNode->left);
}
if (rootNode->right != nullptr)
{
traversePreorder(rootNode->right);
}
}
}
void traverseInorder(ScapeGoatNode* rootNode){
using namespace std;
if (rootNode != nullptr)
{
if (rootNode->left != nullptr)
{
traverseInorder(rootNode->left);
}
cout << rootNode->key << " --> " << rootNode->object << endl;
if (rootNode->right != nullptr)
{
traverseInorder(rootNode->right);
}
}
}
void traversePostorder(ScapeGoatNode* rootNode){
using namespace std;
if (rootNode != nullptr)
{
if (rootNode->left != nullptr)
{
traversePostorder(rootNode->left);
}
if (rootNode->right != nullptr)
{
traversePostorder(rootNode->right);
}
cout << rootNode->key << " --> " << rootNode->object << endl;
}
}
void printBT(const std::string& prefix, const ScapeGoatNode* node, bool isLeft)
{
if( node != nullptr )
{
std::cout << prefix;
std::cout << "|" << std::endl;
std::cout << prefix;
std::cout << (isLeft ? "|--" : "'--" );
// print the value of the node
std::cout << node->key << "-->" << node->object << std::endl;
// enter the next tree level - left and right branch
printBT( prefix + (isLeft ? "| " : " ") , node->left, true);
printBT( prefix + (isLeft ? "| " : " ") , node->right, false);
}
}
/* Function to count number of nodes recursively */
int size(ScapeGoatNode *root)
{
if (root == nullptr)
return 0;
else
{
int numbers = 1;
numbers += size(root->left);
numbers += size(root->right);
return numbers;
}
}
int const log32(int q)
{
double const log23 = 2.4663034623764317;
return (int)ceil(log23 * log(q));
}
ScapeGoatNode *buildBalanced(ScapeGoatNode **a, int i, int ns)
{
if (ns == 0)
return nullptr;
int m = ns / 2;
a[i + m]->left = buildBalanced(a, i, m);
if (a[i + m]->left != NULL)
a[i + m]->left->parent = a[i + m];
a[i + m]->right = buildBalanced(a, i + m + 1, ns - m - 1);\
if (a[i + m]->right != NULL)
a[i + m]->right->parent = a[i + m];
return a[i + m];
}
int packIntoArray(ScapeGoatNode *u, ScapeGoatNode *a[], int i)
{
if (u == NULL)
{
return i;
}
i = packIntoArray(u->left, a, i);
a[i++] = u;
return packIntoArray(u->right, a, i);
}
std::pair<int, int> findMin(ScapeGoatNode* root)
{
while(root->left != nullptr)
root = root->left;
std::pair<int, int> k {root->key, root->object};
ScapeGoatNode *temp = root->right;
if (temp == nullptr)
{
if (root->parent->left == root)
root->parent->left = nullptr;
else
root->parent->right = nullptr;
}
else{
if (root->parent->left == root)
root->parent->left = temp;
else
root->parent->right = temp;
temp->parent = root->parent;
}
delete root;
return k;
}