-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_iterator.cc
More file actions
171 lines (163 loc) · 3.63 KB
/
tree_iterator.cc
File metadata and controls
171 lines (163 loc) · 3.63 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
#include "tree_definition.h"
#include <cstddef>
#include <stack>
class InorderIterator : public Iterator {
public:
InorderIterator(TreeNode_p_t node) {
TreeNode_p_t current = node;
while (NULL != current) {
mStack.push(current);
current = current->lchild;
}
}
virtual TreeNode_p_t next() {
if (mStack.empty()) {
return NULL;
}
TreeNode_p_t top = mStack.top();
mStack.pop();
if (NULL != top->rchild) {
TreeNode_p_t current = top->rchild;
while (NULL != current) {
mStack.push(current);
current = current->lchild;
}
}
return top;
}
private:
std::stack<TreeNode_p_t> mStack;
};
class PostorderIterator : public Iterator {
public:
PostorderIterator(TreeNode_p_t node) {
TreeNode_p_t current = node;
while(NULL != current){
mStack.push(current);
while (NULL != current->lchild) {
mStack.push(current->lchild);
current = current->lchild;
}
current = current->rchild;
}
}
virtual TreeNode_p_t next() {
if (mStack.empty()) {
return NULL;
}
TreeNode_p_t result = mStack.top();
mStack.pop();
if(mStack.empty())
return result;
TreeNode_p_t top = mStack.top();
if(top->rchild == result)
return result;
TreeNode_p_t current = top->rchild;
while(NULL != current){
mStack.push(current);
while (NULL != current->lchild) {
mStack.push(current->lchild);
current = current->lchild;
}
current = current->rchild;
}
return result;
}
private:
std::stack<TreeNode_p_t> mStack;
};
class PreorderIterator : public Iterator {
public:
PreorderIterator(TreeNode_p_t node) {
TreeNode_p_t current = node;
mStack.push(current);
}
virtual TreeNode_p_t next() {
if (mStack.empty()) {
return NULL;
}
TreeNode_p_t top = mStack.top();
mStack.pop();
if(NULL != top->rchild){
TreeNode_p_t current = top->rchild;
mStack.push(current);
}
if(NULL != top->lchild) {
TreeNode_p_t current = top->lchild;
mStack.push(current);
}
return top;
}
private:
std::stack<TreeNode_p_t> mStack;
};
class InorderLazyIterator : public Iterator {
public:
InorderLazyIterator(TreeNode_p_t node):m_list(NULL) {
m_list = make_inorder_tree_iterator(node);
}
virtual TreeNode_p_t next() {
TreeNode_p_t node = first(m_list);
m_list = rest(m_list);
return node;
}
private:
List_p_t m_list;
};
class PreorderLazyIterator : public Iterator {
public:
PreorderLazyIterator(TreeNode_p_t node):m_list(NULL) {
m_list = make_preorder_tree_iterator(node);
}
virtual TreeNode_p_t next() {
TreeNode_p_t node = first(m_list);
m_list = rest(m_list);
return node;
}
private:
List_p_t m_list;
};
class PostorderLazyIterator : public Iterator {
public:
PostorderLazyIterator(TreeNode_p_t node):m_list(NULL) {
m_list = make_postorder_tree_iterator(node);
}
virtual TreeNode_p_t next() {
TreeNode_p_t node = first(m_list);
m_list = rest(m_list);
return node;
}
private:
List_p_t m_list;
};
Iterator* Iterator::AskIterator(ITERATOR_type_t type, TreeNode_p_t root)
{
switch(type)
{
case ITERATOR_PRE_ORDER:
return new PreorderIterator(root);
break;
case ITERATOR_IN_ORDER:
return new InorderIterator(root);
break;
case ITERATOR_POST_ORDER:
return new PostorderIterator(root);
break;
case ITERATOR_PRE_ORDER_LAZY:
return new PreorderLazyIterator(root);
break;
case ITERATOR_IN_ORDER_LAZY:
return new InorderLazyIterator(root);
break;
case ITERATOR_POST_ORDER_LAZY:
return new PostorderLazyIterator(root);
break;
default:
break;
};
return NULL;
}
void Iterator::Release(Iterator* iter)
{
delete iter;
}