-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtree.cpp
More file actions
executable file
·224 lines (200 loc) · 4.9 KB
/
btree.cpp
File metadata and controls
executable file
·224 lines (200 loc) · 4.9 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* =====================================================================================
*
* Filename: btree.cpp
*
* Description:
*
* Version: 1.0
* Created: 2016年05月05日 11时03分59秒
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <iostream>
#include <list>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct tree
{
char data;
struct tree * lchild;
struct tree * rchild;
unsigned int isOut;
}TreeNode,*Tree;
void CreateTree(Tree &t)
{
char ch;
cout<<"please input tree node value, # for NULL, in pre order"<<endl;
cin>>ch;
if (ch == '#')
t = NULL;
else
{
t = (Tree)malloc(sizeof(TreeNode));
t->data = 0;
t->lchild = NULL;
t->rchild = NULL;
if (!t)
{
cout<<"malloc error!"<<endl;
return;
}
t->data = ch;
CreateTree(t->lchild);
CreateTree(t->rchild);
}
}
void PreOrder(Tree t)
{
stack<Tree> mystack;
while (t || !mystack.empty())
{
if (t)
{
cout<<t->data<<endl;
mystack.push(t);
t = t->lchild;
}
else
{
Tree tmp;
tmp = mystack.top();
t = tmp->rchild;
mystack.pop();
}
}
return;
}
void MidOrder(Tree t)
{
stack<Tree> mystack;
while (t || !mystack.empty())
{
if (t)
{
mystack.push(t);
t = t->lchild;
}
else
{
Tree tmp;
tmp = mystack.top();
cout << tmp->data << endl;
t = tmp->rchild;
mystack.pop();
}
}
return;
}
//
void PostOrder(Tree t)
{
t->isOut = 0;
Tree p = t;
list<Tree> mylist;
stack<Tree> mystack;
while (p || !mylist.empty())
{
if (p)
{
if (p->isOut)
{//l/r child are already output
p = mystack.top();
cout<<p->data<<endl;
mystack.pop();
if (!mystack.empty())
p = mystack.top(); //get parent
else
p = NULL;
}
else
{
if ((p->lchild) && (p->lchild->isOut == 1))
{//if there is a left child, and left is already output, means this node is pushed, goto right child
p->isOut = 1;
p = p->rchild;
}
else
{
mystack.push(p);
p = p->lchild;
}
}
}
else
{
if (!mystack.empty())
p = mystack.top();
else
p = NULL;
if (p->rchild)
{
p = p->rchild;
}
else
{
p = mystack.top();
cout<<p->data<<endl;
mystack.pop();
p->isOut = 1;
if (!mystack.empty())
{
p = mystack.top();
if (p->lchild == NULL)
p->isOut = 1; //right child is output, so set parent isOUt to 1
}
else
p = NULL;
}
}
}
}
void PostOrder2(Tree t) //
{
Tree temp;
stack<Tree> mystack;
while (t!=NULL || !mystack.empty())
{
while (t!=NULL) //沿左子树一直往下搜索,依次压栈,直至出现没有左子树的结点
{
t->isOut = true;
mystack.push(t);
t = t->lchild;
}
if (!mystack.empty())
{
temp = mystack.top(); //栈顶元素弹出
mystack.pop();
if (temp->isOut==true) //表示是第一次出现在栈顶,并将标志位赋值为不是第一次出现
{
temp->isOut = false; //再压回栈,并访问其右子树
mystack.push(temp);
t = temp->rchild;
}
else //第二次出现在栈顶
{
cout<<temp->data<<endl; //表示其左子树、右子树都已经访问过
t = NULL; //此时将t赋值为null,继续访问栈中别的元素
}
}
}
}
int main()
{
Tree t;
CreateTree(t);
cout<<"mid order:"<<endl;
MidOrder(t);
cout<<"pre order:"<<endl;
PreOrder(t);
cout<<"post order:"<<endl;
PostOrder2(t);
return 0L;
}