- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample02.cpp
More file actions
Latest commit
187 lines (153 loc) · 4.87 KB
/
Copy pathExample02.cpp
File metadata and controls
187 lines (153 loc) · 4.87 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
// TreeNode
#include<iostream>
usingnamespacestd;
structTreeNode {
int value;
TreeNode* sibling;
TreeNode* firstChild;
TreeNode(int value) {
this->value = value;
sibling = NULL;
firstChild = NULL;
}
};
structStack_element {
TreeNode* element;
Stack_element* prev;
Stack_element(TreeNode* element) {
this->element = element;
prev = NULL;
}
};
// Helper class for tracking parent nodes in node tree
classStack {
private:
Stack_element* top;
public:
Stack() {
top = NULL;
}
voidpush(TreeNode* element) {
Stack_element* new_elem = newStack_element(element);
new_elem->prev = top;
top = new_elem;
// cout << "push: " << top->element << " -> " << top->element->value << endl;
}
TreeNode* pop() {
Stack_element* prev;
TreeNode* element = top->element;
if (top == NULL) {
prev = NULL;
element = NULL;
}
else {
prev = top->prev;
element = top->element;
// cout << "pop: " << top->element << " <- " << element->value << endl;
delete top;
top = prev;
}
return element;
}
TreeNode* top_stack() {
if (top == NULL)
returnNULL;
else {
//cout << "top stack: " << top->element << " -> " << top->element->value << endl;
return top->element;
}
}
};
// Node tree
classTree {
private:
TreeNode* root;
public:
Tree() { // create empty tree
root = NULL;
}
// Create node tree from static array
Tree(int* a, int m) { //
TreeNode* prev = NULL; // parent node
TreeNode* curr; // current node
Stack stack; // parent nodes tracker
int even = 0; // count of even nodes
int i = 0;
while (i < m) { // iterate through static array
curr = newTreeNode(a[i]); // create current node
if (i == 0)
root = curr;
else {
// If parent node exists, linking, else skip linking
if (prev != NULL) {
// If parent is odd, link: parent -> firstChild -> current
if ((prev->value) % 2 == 1) {
prev->firstChild = curr;
even = 0;
}
// If parent is even
else {
// Count adjacent even nodes
// Second adjasent even node is leaf
++even;
// If parent is leaf, find first free parent with free sibling link
if (even > 1) {
stack.pop();
do { // reset parent node to first parent with free sibling link
prev = stack.pop();
} while (prev->sibling != NULL);
even = 0;
}
// Link: parent -> sibling -> current
prev->sibling = curr;
}
}
}
stack.push(curr); // track current node
prev = curr; // parent is now current node
++i; // static array iterator
}
}
// Debug: Helper method for printing nodes
voidprint_node(TreeNode* node) {
if (node == NULL) {
cout << "node: NULL" << endl;
return;
}
cout
<< "node: " << node->value
<< "\n first child: " << ((node->firstChild != NULL) ? node->firstChild->value : 0)
<< "\n sibling: " << ((node->sibling != NULL) ? node->sibling->value : 0)
<< endl;
}
// Walk through node tree using recursion
voidpre_order(TreeNode* node) {
if (node == NULL)
return;
print_node(node); // debug: print current node
//cout << node->value << endl;
if (node->firstChild != NULL)
pre_order(node->firstChild);
if (node->sibling != NULL)
pre_order(node->sibling);
}
// Walk through node tree starting from root
voidpre_order() {
pre_order(root);
}
};
intmain() {
int m = 9;
int a[] = { 3, 1, 4, 48, 2, 5, 3, 6, 18 };
// Debug: test stack
/*
Stack stack;
for (int i = 0; i < m; ++i)
stack.push(new TreeNode(a[i]));
for (int i = 0; i < m; ++i)
stack.pop();
*/
Tree tree(a, m);
tree.pre_order();
return0;
}