-
Notifications
You must be signed in to change notification settings - Fork 1
/
postorder.cpp
187 lines (156 loc) · 3.72 KB
/
postorder.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
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
// {"category": "Tree", "notes": "Post-order traversal (iterative)"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <memory>
using namespace std;
//------------------------------------------------------------------------------
//
// Post-order binary tree traversal
//
// [10]
// / \
// [4] [16]
// / \ / \
// [2] [8] [12] [18]
// / \ \
// [6] [14] [20]
// \
// [24]
// /
// [22]
//
// Post-order traversal sequence is 2, 6, 8, 4, 14, 12, 22, 24, 20, 18, 16, 10
//
//------------------------------------------------------------------------------
template<class Object>
class BinaryTreeNode
{
public:
BinaryTreeNode(
const Object& object,
BinaryTreeNode<Object>* pParent = nullptr);
~BinaryTreeNode();
void Add(const Object& object);
BinaryTreeNode<Object>* GetFirst();
Object m_object;
BinaryTreeNode<Object>* m_pLeft;
BinaryTreeNode<Object>* m_pRight;
BinaryTreeNode<Object>* m_pParent;
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
BinaryTreeNode<Object>* GetNext();
};
template<class Object>
BinaryTreeNode<Object>* BinaryTreeNode<Object>::GetNext()
{
if (nullptr == m_pParent)
{
return nullptr;
}
if (m_pParent->m_pRight == this)
{
return m_pParent;
}
BinaryTreeNode<Object>* pNext = m_pParent;
while (pNext->m_pRight != nullptr)
{
pNext = pNext->m_pRight;
while (pNext->m_pLeft != nullptr)
{
pNext = pNext->m_pLeft;
}
}
return pNext;
}
//------------------------------------------------------------------------------
//
// Demo execution
//
//------------------------------------------------------------------------------
template<class Object>
BinaryTreeNode<Object>::BinaryTreeNode(
const Object& object,
BinaryTreeNode<Object>* pParent)
: m_object(object), m_pLeft(nullptr), m_pRight(nullptr), m_pParent(pParent)
{
}
template<class Object>
BinaryTreeNode<Object>::~BinaryTreeNode()
{
if (m_pLeft != nullptr)
{
delete m_pLeft;
}
if (m_pRight != nullptr)
{
delete m_pRight;
}
}
template<class Object>
void BinaryTreeNode<Object>::Add(const Object& object)
{
if (object < m_object)
{
if (nullptr == m_pLeft)
{
m_pLeft = new BinaryTreeNode<Object>(object, this);
}
else
{
m_pLeft->Add(object);
}
}
if (object > m_object)
{
if (nullptr == m_pRight)
{
m_pRight = new BinaryTreeNode<Object>(object, this);
}
else
{
m_pRight->Add(object);
}
}
}
template<class Object>
BinaryTreeNode<Object>* BinaryTreeNode<Object>::GetFirst()
{
BinaryTreeNode<Object>* pFirst = this;
while (pFirst->m_pParent != nullptr)
{
pFirst = pFirst->m_pParent;
}
while (pFirst->m_pLeft != nullptr)
{
pFirst = pFirst->m_pLeft;
}
return pFirst;
}
int _tmain(int argc, _TCHAR* argv[])
{
BinaryTreeNode<int> root(10);
root.Add(4);
root.Add(2);
root.Add(8);
root.Add(6);
root.Add(16);
root.Add(12);
root.Add(18);
root.Add(14);
root.Add(20);
root.Add(24);
root.Add(22);
BinaryTreeNode<int>* pNode = root.GetFirst();
while (pNode != nullptr)
{
cout << pNode->m_object << " ";
pNode = pNode->GetNext();
}
cout << endl;
return 0;
}