-
Notifications
You must be signed in to change notification settings - Fork 1
/
treetraversal3.cpp
170 lines (148 loc) · 3.74 KB
/
treetraversal3.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
// {"category": "Tree", "notes": "Breadth first zig-zag order iteration"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <stack>
using namespace std;
//------------------------------------------------------------------------------
//
// Print a tree in zig-zag order. Zig-zag order is a breadth first iteration
// where each successive level is printed in the reverse direction of the
// previous.
//
// [10]
// / \
// [4] [16]
// / \ / \
// [2] [8] [12] [18]
// / \ \
// [6] [14] [20]
// \
// [24]
// /
// [22]
//
// Traversal sequence is 10, 4, 16, 18, 12, 8, 2, 6, 14, 20, 24, 22
//
//------------------------------------------------------------------------------
template<class Object>
class BinaryTreeNode
{
public:
BinaryTreeNode(const Object& object);
~BinaryTreeNode();
void Add(const Object& object);
Object m_object;
BinaryTreeNode<Object>* m_pLeft;
BinaryTreeNode<Object>* m_pRight;
};
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
template<class Object>
void PrintZigZag(BinaryTreeNode<Object>* pRoot)
{
stack<BinaryTreeNode<Object>*> stack1;
stack<BinaryTreeNode<Object>*> stack2;
stack<BinaryTreeNode<Object>*>* pStackCurrent = &stack1;
stack<BinaryTreeNode<Object>*>* pStackNext = &stack2;
bool forward = false;
if (pRoot != nullptr)
{
pStackCurrent->push(pRoot);
}
while (!pStackCurrent->empty() || !pStackNext->empty())
{
if (pStackCurrent->empty())
{
swap(pStackCurrent, pStackNext);
forward = !forward;
}
BinaryTreeNode<Object>* pNode = pStackCurrent->top();
pStackCurrent->pop();
cout << pNode->m_object << " ";
BinaryTreeNode<Object>* pChild1 = pNode->m_pLeft;
BinaryTreeNode<Object>* pChild2 = pNode->m_pRight;
if (!forward)
{
swap(pChild1, pChild2);
}
if (pChild1 != nullptr)
{
pStackNext->push(pChild1);
}
if (pChild2 != nullptr)
{
pStackNext->push(pChild2);
}
}
}
//------------------------------------------------------------------------------
//
// Demo execution
//
//------------------------------------------------------------------------------
template<class Object>
BinaryTreeNode<Object>::BinaryTreeNode(const Object& object)
: m_object(object), m_pLeft(nullptr), m_pRight(nullptr)
{
}
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);
}
else
{
m_pLeft->Add(object);
}
}
if (object > m_object)
{
if (nullptr == m_pRight)
{
m_pRight = new BinaryTreeNode<Object>(object);
}
else
{
m_pRight->Add(object);
}
}
}
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);
PrintZigZag(&root);
cout << endl;
return 0;
}