-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
tree_with_parent_inorder.cc
45 lines (39 loc) · 1.47 KB
/
tree_with_parent_inorder.cc
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
#include <vector>
#include "binary_tree_with_parent_prototype.h"
#include "test_framework/generic_test.h"
using std::vector;
vector<int> InorderTraversal(const unique_ptr<BinaryTreeNode<int>>& tree) {
BinaryTreeNode<int>*prev = nullptr, *curr = tree.get();
vector<int> result;
while (curr != nullptr) {
BinaryTreeNode<int>* next;
if (curr->parent == prev) {
// We came down to curr from prev.
if (curr->left != nullptr) { // Keep going left.
next = curr->left.get();
} else {
result.emplace_back(curr->data);
// Done with left, so go right if right is not empty.
// Otherwise, go up.
next = (curr->right != nullptr) ? curr->right.get() : curr->parent;
}
} else if (curr->left.get() == prev) {
// We came up to curr from its left child.
result.emplace_back(curr->data);
// Done with left, so go right if right is not empty. Otherwise, go up.
next = (curr->right != nullptr) ? curr->right.get() : curr->parent;
} else { // Done with both children, so move up.
next = curr->parent;
}
prev = curr;
curr = next;
}
return result;
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"tree"};
return GenericTestMain(args, "tree_with_parent_inorder.cc",
"tree_with_parent_inorder.tsv", &InorderTraversal,
DefaultComparator{}, param_names);
}