-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path666.cpp
More file actions
278 lines (256 loc) · 4.8 KB
/
Copy path666.cpp
File metadata and controls
278 lines (256 loc) · 4.8 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*#include<stdio.h>
#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const MAXSIZE=100;
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}*BiTree;
BiTree Build()
{
char ch;
scanf("%c",&ch);
if(ch=='#')
return NULL;
BiTree tree=new BiTNode;
tree->data=ch;
tree->lchild=Build();
tree->rchild=Build();
return tree;
}
//建立二叉链表
void CreateBiTree(BiTree &T)
{
//在先序遍历二叉树过程中输入结点字符,建立二叉链表存储结构
//指针T指向所建二叉树的根结点
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else
{
T=new BiTNode;//生成根结点
T->data=ch;
CreateBiTree(T->lchild);//递归建左子树
CreateBiTree(T->rchild);//递归建右子树
}
}
//求二叉树的树深
void BiTreeDepth(BiTree T,int h, int &depth)
{//h为T指向的结点所在层次,T指向二叉树的根,则h的初值为1
//depth为当前求得的最大层次,其初值为0
if(T)
{
if(h>depth) depth=h;
BiTreeDepth(T->lchild ,h+1,depth);
BiTreeDepth(T->rchild ,h+1,depth);
}
}
char tag[100][100];
int m,w=0,deep;
int Levelorder(BiTree tree,int m,int w, int q)
{//层次遍历
if(tree)
{
if(q==0)
{
m=pow(2.0,deep);
tag[++w][m]=tree->data;
int ww=w,uu=w,mm=m,nn=m;
deep--;
Levelorder(tree->lchild,mm,ww,-1);
deep++;
Levelorder(tree->rchild,nn,uu,1);
}
else if(q==-1)
{
m-=pow(2.0,deep);
tag[++w][m]=tree->data;
int ww=w,uu=w,mm=m,nn=m;
deep--;
Levelorder(tree->lchild,mm,ww,-1);
Levelorder(tree->rchild,nn,uu,1);
deep++;
}
else if(q==1)
{
m+=pow(2.0,deep)+1;
tag[++w][m]=tree->data;
int ww=w,uu=w,mm=m,nn=m;
deep--;
Levelorder(tree->lchild,mm,ww,-1);
Levelorder(tree->rchild ,nn,uu,1);
}
}
return 0;
}
void Levelorder(BiTree tree)
{
Queue q;
//树为空,直接返回
if (tree == NULL)
{
exit(-1);
}
QueueInit(q);
//先将根节点入队
QueuePush(q, tree);
while (QueueEmpty( q))
{
//出队保存队头并访问
BTNode* front = QueueFront(&q);
printf("%c", front->_data);
QueuePop(&q);
//将出队结点的左子树根入队
if (front->_left)
QueuePush(&q, front->_left);
//将出队结点的右子树根入队
if (front->_right)
QueuePush(&q, front->_right);
}
}
int main()
{
int i,j,d,h,depth;
char tag[80][80];
memset(tag,0,sizeof(tag));
printf("请按顺序输入字母(若输入字符'#',则表明该二叉树为空树):");
BiTree tree=Build();
d=BiTreeDepth(tree,h,depth);
printf("树的深度为:%d\n",&d);
Levelorder(tree,m,w,0);
for(i=0;i<=10;i++)
{
for(j=0;j<=64;j++)
{
if(tag[i][j])
printf("%c",tag[i][j]);
else
printf(" ");
}
}
return 0;
}*/
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct treeNode{
treeNode *left;
treeNode *right;
char data;
}*TreeNode;
typedef struct queueNode{
TreeNode data;
queueNode *next;
}*QueueNode;
typedef struct queue{
QueueNode front;
QueueNode rear;
}*Queue;
void Build(TreeNode &t)
{
char ch;
cin>>ch;
if(ch=='#') {t=NULL;}
else
{
t=new treeNode;
t->data=ch;
Build(t->left);
Build(t->right);
}
}
Queue init(Queue &q){
q->front=new queueNode;
q->front->next=NULL;
q->rear=q->front;
return q;
}
void Enqueue(Queue &q,TreeNode t){
QueueNode New=new queueNode;
New->data=t;
New->next=NULL;
q->rear->next=New;
q->rear=New;
}
TreeNode Dequeue(Queue &q){
QueueNode pTemp=new queueNode;
pTemp=q->front->next;
if(pTemp->next==NULL){
q->rear=q->front;
}else{
q->front->next=pTemp->next;
}
TreeNode x=pTemp->data;
free(pTemp);
return x;
}
void LevelOrderBinaryTree(TreeNode t){
Queue q=new queue;
q=init(q);
Enqueue(q,t);
while(q->rear!=q->front){
TreeNode x=Dequeue(q);
cout<<x->data<<" ";
if(x->left){
Enqueue(q,x->left);
}
if(x->right){
Enqueue(q,x->right);
}
}
}
void inorder_zhong(TreeNode t)
{
if(t)
{
inorder_zhong(t->left);
cout<<t->data<<" ";
inorder_zhong(t->right);
}
}
void inorder_hou(TreeNode t)
{
if(t)
{
inorder_hou(t->left);
inorder_hou(t->right);
cout<<t->data<<" ";
}
}
void BiTreeDepth(TreeNode t,int h, int &depth)
{
if(t)
{
if(h>depth) depth=h;
BiTreeDepth(t->left ,h+1,depth);
BiTreeDepth(t->right ,h+1,depth);
}
}
void print(TreeNode t,int level)
{
int i=0;
if(t==NULL)return;
print(t->right,level+1);
for(i=0;i<level;i++)
printf(" ");
printf("%c\n",t->data);
print(t->left,level+1);
}
void main(){
TreeNode t;
Build(t);
printf("层序遍历如下:");
LevelOrderBinaryTree(t);
printf("\n");
printf("树的深度:");
int m=0;
BiTreeDepth(t,1,m);
printf("%d\n",m-1);
printf("树:\n");
print(t,0);
printf("\n");
}