-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_to_list.cpp
144 lines (128 loc) · 3.96 KB
/
convert_to_list.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
/*
* BINARY TREE TRAVERSALS WITH VECTOR OUTPUT
*
* This program extends an assumed BinaryTree implementation (from "binary_tree.h") by adding
* inorder and preorder traversal methods that return the result as a std::vector<int>.
*
* The inorder() method uses an iterative approach with a stack to traverse the tree in left-root-right order.
* The preorder() method uses an iterative approach with a stack to traverse the tree in root-left-right order.
*
* ASCII Illustration:
* 9
* / \
* 8 13
* / / \
* 4 10 16
* \
* 7
* \
* 15
*
* Example Input/Output:
* When the tree is built by adding values in the order:
* {9, 8, 13, 4, 10, 16, 7, 15}
* the inorder traversal returns: {4, 7, 8, 9, 10, 13, 15, 16}
* and the preorder traversal returns: {9, 8, 4, 7, 13, 10, 16, 15}
*
* Explanation:
* The tree is traversed using an iterative approach. Inorder traversal visits the left subtree, the root, then the right subtree.
* Preorder traversal visits the root first, then the left subtree followed by the right subtree.
*/
#include "binary_tree.h"
#include <cassert>
#include <stack>
#include <vector>
class TreeWithVector : public BinaryTree {
public:
// Default constructor calls the base default constructor
TreeWithVector() : BinaryTree() {}
// Delete copy semantics
TreeWithVector(const TreeWithVector &) = delete;
TreeWithVector &operator=(const TreeWithVector &) = delete;
// Allow move semantics
TreeWithVector(TreeWithVector &&) = default;
TreeWithVector &operator=(TreeWithVector &&) = default;
// Iterative inorder traversal: left -> root -> right
std::vector<int> inorder() const {
std::vector<int> result;
std::stack<const Node *> s;
const Node *curr = root.get();
while (curr || !s.empty()) {
while (curr) {
s.push(curr);
curr = curr->left.get();
}
curr = s.top();
s.pop();
result.push_back(curr->value);
curr = curr->right.get();
}
return result;
}
// Iterative preorder traversal: root -> left -> right
std::vector<int> preorder() const {
std::vector<int> result;
std::stack<const Node *> s;
if (root)
s.push(root.get());
while (!s.empty()) {
const auto temp = s.top();
s.pop();
result.push_back(temp->value);
if (temp->right)
s.push(temp->right.get());
if (temp->left)
s.push(temp->left.get());
}
return result;
}
};
// Simple test function using assertions
void testTreeWithVector(const TreeWithVector &tree,
const std::vector<int> &inorderExpected,
const std::vector<int> &preorderExpected) {
assert(tree.inorder() == inorderExpected);
assert(tree.preorder() == preorderExpected);
}
int main() {
testTreeWithVector(
[] {
TreeWithVector tree;
for (int val : {9, 8, 13, 4, 10, 16, 7, 15})
tree.add(val);
return tree; // Moved (since copy is deleted)
}(),
{4, 7, 8, 9, 10, 13, 15, 16},
{9, 8, 4, 7, 13, 10, 16, 15}
);
testTreeWithVector(
[] {
TreeWithVector tree;
for (int val : {5, 4, 3, 2, 1})
tree.add(val);
return tree;
}(),
{1, 2, 3, 4, 5},
{5, 4, 3, 2, 1}
);
testTreeWithVector(
[] {
TreeWithVector tree;
for (int val : {1, 2, 3, 4, 5})
tree.add(val);
return tree;
}(),
{1, 2, 3, 4, 5},
{1, 2, 3, 4, 5}
);
testTreeWithVector(
[] {
TreeWithVector tree;
tree.add(1);
return tree;
}(),
{1},
{1}
);
return 0;
}