-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path查找_二叉树查找_.cpp
137 lines (100 loc) · 1.8 KB
/
查找_二叉树查找_.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
#include<stdio.h>
#include<stdlib.h>
#define MAX 256
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*Bitree;
Bitree CreateBitree(Bitree &r)
{
char ch;
scanf("%c",&ch);
printf("顺序表 %c\n",ch);
if (ch=='#')
{
r= NULL;
}
else
{
//printf("顺序表*****************0\n");
if(!(r=(BiTNode *)malloc(sizeof(BiTNode))))
{
exit(-1);
};
r->data=ch;
CreateBitree(r->lchild);
//printf("顺序表*****************1\n");
CreateBitree(r->rchild);
//printf("顺序表*****************2\n");
}
return 0;
}
void Prefirst(Bitree r)
{
if(r!=NULL)
{
printf("%c ",r->data);
Prefirst(r->lchild);
Prefirst(r->rchild);
}
}
Bitree D_serach(Bitree r,char key)
{
if(r==NULL)
{
return NULL;
}
if(key==r->data)
{
printf("successful->%c",r->data);
return r;
}
else if(key<r->data)
{;
return D_serach(r->lchild,key);
}
else if(key>r->data)
{
return D_serach(r->rchild,key);
}
}
Bitree C_serach(Bitree r,char key)
{
while(r!=NULL)
{
if(key==r->data)
{
printf("successful->%c",r->data);
return r;
}
else if(key<r->data)
{
r=r->lchild;
}
else if(key>r->data)
{
r=r->rchild;
}
}
return NULL;
}
int main()
{
int i,a[]={'5','1','8','0','3','7','9','#','#','2','4','#','#','#','#','#','#','#'};
char c;
Bitree r;
printf("input顺序表的结构,\n结束 :\n");
CreateBitree(r);
printf("顺序表的结构,成功 !\n");
printf("二叉树,先序遍历结果: \n");
Prefirst(r) ;
printf("\n ");
//printf("查找结果:%d \n",C_serach(r,7)->data);
C_serach(r,'7');
printf("\n ");
D_serach(r,'7');
//54##87##9## 先序的方法创建
return 0;
}