-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin_tree_impl.h
More file actions
218 lines (196 loc) · 5.84 KB
/
bin_tree_impl.h
File metadata and controls
218 lines (196 loc) · 5.84 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#pragma once
#include <algorithm>
#include <iostream>
#include <string>
#include "bin_tree.h"
template <typename T>
int binTree<T>::updateHeight(binNodePos(T) x)
{
return x->height_ = 1 + std::max(stature(x->left_child_), stature(x->right_child_));
}
template <typename T>
void binTree<T>::updateHeightAbove(binNodePos(T) x)
{
while (x) {
updateHeight(x);
x = x->parent_;
}
}
template <typename T>
static int removeAt(binNodePos(T) x)
{
if (!x) {
return 0;
}
int n = 1 + removeAt(x->left_child_) + removeAt(x->right_child_);
delete x;
return n;
}
template <typename T>
int binTree<T>::remove(binNodePos(T) x)
{
FromParentTo(*x) = nullptr;
updateHeightAbove(x->parent_);
int n = removeAt(x);
size_ -= n;
return n;
}
template <typename T>
binNodePos(T) binTree<T>::insertAsRoot(T const& e)
{
if (root_ != nullptr) {
std::cout << "this tree has root!" << std::endl;
} else {
size_++;
return root_ = new binNode<T>(e);
}
}
template <typename T>
binNodePos(T) binTree<T>::insertAsLeftChild(binNodePos(T) x, T const& e)
{
size_++;
x->insertAsLeftChild(e);
updateHeightAbove(x);
return x->left_child_;
}
template <typename T>
binNodePos(T) binTree<T>::insertAsRightChild(binNodePos(T) x, T const& e)
{
size_++;
x->InsertAsRightChild(e);
updateHeightAbove(x);
return x->right_child_;
}
template <typename T>
void binTree<T>::PushToStackForInorder(std::stack<binNodePos(T)> &left_child_stack,
binNodePos(T) x)
{
while(nullptr != x) {
#ifdef DEBUG
std::cout << "push " << x->data_ << " to stack"<< std::endl;
#endif
left_child_stack.push(x);
x = x->left_child_;
}
}
template <typename T>
void binTree<T>::InOrderIteration(std::string &outString, binNodePos(T) const &x)
{
std::stack<binNodePos(T)> left_child_stack;
PushToStackForInorder(left_child_stack, x);
#ifdef DEBUG
std::cout << "stack size : " << left_child_stack.size() << std::endl;
#endif
while(0 != left_child_stack.size()) {
binNodePos(T) currentNode = left_child_stack.top();
outString += std::to_string(currentNode->data_);
outString += " ";
left_child_stack.pop();
if (nullptr != HasRightChild(*currentNode)) {
PushToStackForInorder(left_child_stack, HasRightChild(*currentNode));
}
}
}
template <typename T>
void binTree<T>::InOrderTraverse(std::string &outString, binNodePos(T) const &x)
{
if (nullptr == x) {
return;
}
InOrderTraverse(outString, x->left_child_);
outString += std::to_string(x->data_);
outString += " ";
InOrderTraverse(outString, x->right_child_);
}
template <typename T>
void binTree<T>::PushToStackForPreorder(std::string &outString,
std::stack<binNodePos(T)> &right_child_stack,
binNodePos(T) x)
{
while(nullptr != x) {
outString += std::to_string(x->data_);
outString += " ";
if (nullptr != HasRightChild(*x)) {
right_child_stack.push(x->right_child_);
}
x = x->left_child_;
}
}
template <typename T>
void binTree<T>::PreOrderIteration(std::string &outString, binNodePos(T) const &x)
{
std::stack<binNodePos(T)> right_child_stack;
PushToStackForPreorder(outString, right_child_stack, x);
#ifdef DEBUG
std::cout << "pre order : " << outString << std::endl;
std::cout << "stack size : " << right_child_stack.size() << std::endl;
#endif
while(0 != right_child_stack.size()) {
auto tmpNode = right_child_stack.top();
right_child_stack.pop();
PushToStackForPreorder(outString, right_child_stack, tmpNode);
}
}
template <typename T>
void binTree<T>::PreOrderTraverse(std::string &outString, binNodePos(T) const &x)
{
if (nullptr == x) {
return;
}
outString += std::to_string(x->data_);
outString += " ";
PreOrderTraverse(outString, x->left_child_);
PreOrderTraverse(outString, x->right_child_);
}
template <typename T>
void binTree<T>::PushToQueueForPostorder(std::stack<binNodePos(T)>& left_child_stack,
std::queue<binNodePos(T)>& right_child_queue,
binNodePos(T) x)
{
while(nullptr != x) {
right_child_queue.push(x);
if (nullptr != HasLeftChild(*x)) {
left_child_stack.push(HasLeftChild(*x));
}
x = x->right_child_;
}
}
template <typename T>
void binTree<T>::PostOrderIteration(std::string &outString, binNodePos(T) const &x)
{
std::stack<binNodePos(T)> left_child_stack;
std::queue<binNodePos(T)> right_child_queue;
PushToQueueForPostorder(left_child_stack, right_child_queue, x);
while(0 != left_child_stack.size()) {
auto tmpNode = left_child_stack.top();
left_child_stack.pop();
PushToQueueForPostorder(left_child_stack, right_child_queue, tmpNode);
}
#ifdef DEBUG
std::cout << "reverse order queue" << std::endl;
#endif
auto size_num = right_child_queue.size();
for(int i = 0; i < size_num; i++) {
#ifdef DEBUG
// std::cout << right_child_queue.front()->data_ << std::endl;
#endif
left_child_stack.push(right_child_queue.front());
right_child_queue.pop();
}
for(int i = 0; i < size_num; i++) {
outString += std::to_string(left_child_stack.top()->data_);
outString += " ";
left_child_stack.pop();
}
}
template <typename T>
void binTree<T>::PostOrderTraverse(std::string &outString, binNodePos(T) const &x)
{
if (nullptr == x) {
return;
}
PostOrderTraverse(outString, x->left_child_);
PostOrderTraverse(outString, x->right_child_);
outString += std::to_string(x->data_);
outString += " ";
}