-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraverseLoop.cpp
executable file
·212 lines (177 loc) · 3.75 KB
/
traverseLoop.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
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
#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;
struct treeNode
{
int key;
treeNode *leftChild;
treeNode *rightChild;
};
void pre_traverseLoop(treeNode *T)
{
if(!T) cerr<<"treeNode is invalid"<<endl;
stack<treeNode *> nodes;
treeNode *cur=T;
do
{
cout<<cur->key<<" ";
nodes.push(cur);
cur=cur->leftChild;
while(!cur && !nodes.empty())
{
cur=nodes.top();
nodes.pop();
cur=cur->rightChild;
}
}while(cur);
cout<<endl;
}
void in_traverseLoop(treeNode *T)
{
if(!T)
{
cerr<<"T is nullptr"<<endl;
return;
}
stack<treeNode *>nodes;
treeNode *cur=T;
while(cur)
{
nodes.push(cur);
cur=cur->leftChild;
while(!cur && !nodes.empty())
{
cur=nodes.top();
nodes.pop();
cout<<cur->key<<" ";
cur=cur->rightChild;
}
}
cout<<endl;
}
void last_traverseLoop(treeNode *T)
{
if(!T)
{
cerr<<"T is nullptr"<<endl;
return;
}
stack<treeNode *> nodes;
treeNode *cur=T;
treeNode *pre=nullptr;
while(cur || !nodes.empty())
{
while(cur)
{
nodes.push(cur);
cur=cur->leftChild;
}
if(!nodes.empty())
{
cur=nodes.top();
}
if(cur->rightChild==nullptr || (pre==cur->rightChild))
{
cout<<cur->key<<" ";
pre=cur;
cur=nullptr;
nodes.pop();
}
else
{
cur=cur->rightChild;
}
}
cout<<endl;
}
void postOrder3(treeNode *root) //非递归后序遍历
{
stack<treeNode*> s;
treeNode *cur; //当前结点
treeNode *pre=nullptr; //前一次访问的结点
s.push(root);
while(!s.empty())
{
cur=s.top();
if((cur->leftChild==nullptr&&cur->rightChild==nullptr)||
(pre!=nullptr&&(pre==cur->leftChild||pre==cur->rightChild)))
{
cout<<cur->key<<" "; //如果当前结点没有孩子结点或者孩子节点都已被访问过
s.pop();
pre=cur;
}
else
{
if(cur->rightChild!=nullptr)
s.push(cur->rightChild);
if(cur->leftChild!=nullptr)
s.push(cur->leftChild);
}
}
}
treeNode *insert(treeNode *root,int key)
{
if(!root)
{
treeNode *newnode=new treeNode;
newnode->key=key;
newnode->leftChild=nullptr;
newnode->rightChild=nullptr;
root=newnode;
return root;
}
if(key>root->key) root->rightChild=insert(root->rightChild,key);
else if(key<root->key) root->leftChild=insert(root->leftChild,key);
return root;
}
void PrintLevel(treeNode *root,int level)
{
if(!root) return;
if(level<1) return;
if(level==1) cout<<root->key<<" ";
else if(level>1)
{
PrintLevel(root->leftChild,level-1);
PrintLevel(root->rightChild,level-1);
}
}
void print(treeNode *root,int levels)
{
if(!root) return;
for(int i=1;i<=levels;++i)
{
PrintLevel(root,i);
cout<<endl;
}
}
int main()
{
treeNode *root1=nullptr,*root2=nullptr;
int a[]={12,10,17,7,11,14,18,13,16};
int b[]={17,14,18};
int len=sizeof(a)/sizeof(a[0]);
int len2=sizeof(b)/sizeof(b[0]);
for(int i=0;i<len;++i)
{
root1=insert(root1,a[i]);
}
for(int i=0;i<len2;++i)
{
root2=insert(root2,b[i]);
}
cout<<"level----------------->"<<endl;
print(root1,4);
cout<<"==========="<<endl;
print(root2,2);
cout<<"pre------------------>"<<endl;
pre_traverseLoop(root1);
cout<<"=========="<<endl;
pre_traverseLoop(root2);
cout<<"in=========="<<endl;
in_traverseLoop(root1);
cout<<"post========"<<endl;
last_traverseLoop(root1);
return 0;
}