二叉树的一些运算

来源:互联网 发布:软件狗软件怎么破解 编辑:程序博客网 时间:2024/05/21 17:33
我的输入测试树为:ABC#D###EF##G##,这是按照先序创建二叉树的

可是我的程序中返回双亲结点:char Parent(BiTree T,ElemType e)
              返回左孩子  :char Leftchild(BiTree T,ElemType e)
              返回左孩子  :char Rightchild(BiTree T,ElemType e)

              这三个功能在测试时只能显示B,E的双亲,和A的左右孩子,其他的显示全为空,求指导!
源代码如下:
C/C++ code
?
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include<stdio.h>
#include<stdlib.h>
 
#define OK  1
#define ERROR  0
 
int LeafCount=0;
typedef char ElemType;
 
typedef struct BiTNode        //结点结构体定义
{
    ElemType Data;
    struct BiTNode * Lchild;   //左子树指针
    struct BiTNode * Rchild;   //右子树指针
}BiTNode,*BiTree;
 
void CreateBiTree(BiTree *T)
{
    ElemType ch;
     
    ch=getchar();
    if(ch=='#')
        *T=NULL;
    else
    {
        *T=(BiTree)malloc(sizeof(BiTNode));
        (*T)->Data=ch;
        CreateBiTree(&((*T)->Lchild));
        CreateBiTree(&((*T)->Rchild));
    }
}
 
void ClearBiTree(BiTree *T)
{
    //后序
    if(*T!=NULL)
    {
        ClearBiTree(&((*T)->Lchild));
        ClearBiTree(&((*T)->Rchild));
        free(*T);
    }
}
 
//先序遍历
void PreOrder(BiTree T)
{
    if(T!=NULL)
    {
        printf("%c ",T->Data);
        PreOrder(T->Lchild);
        PreOrder(T->Rchild);
    }
}
 
//中序遍历
void InOrder(BiTree T)
{
    if(T!=NULL)
    {
        InOrder(T->Lchild);
        printf("%c ",T->Data);
        InOrder(T->Rchild);
    }
}
 
//后序遍历
void PostOrder(BiTree T)
{
    if(T!=NULL)
    {
        PostOrder(T->Lchild);
        PostOrder(T->Rchild);
        printf("%c ",T->Data);
    }
}
 
//输出二叉树的叶子节点
void outleaf(BiTree T)
{
    if(T!=NULL)
    {
        if((T->Lchild==NULL)&&(T->Rchild==NULL))
            printf("%c ",T->Data);
        outleaf(T->Lchild);
        outleaf(T->Rchild);
    }
 
}
//统计叶子节点数目
void leaf(BiTree T)
{
    if(T!=NULL)
    {
        leaf(T->Lchild);
        leaf(T->Rchild);
        if((T->Lchild==NULL)&&(T->Rchild)==NULL)
            LeafCount++;
    }
}
 
//二叉树的高度(深度)
int PostTreeDepth(BiTree T)
{
    int hl,hr,max;
    if(T!=NULL)
    {
        hl=PostTreeDepth(T->Lchild);
        hr=PostTreeDepth(T->Rchild);
        max=hl>hr?hl:hr;
        return(max+1);
    }
    else 
        return 0;
}
 
//返回根结点
char Root(BiTree T)
{
    char root;
    if(T!=NULL)
    {
        root=T->Data;
        printf("\n根结点为:%c",root);
        return root;
    }
    else
        printf("\n二叉树为空,无根结点");
        return ' ';
}
 
//判空
void BiTreeEmpty(BiTree T)
{
    if(T!=NULL)
        printf("\n二叉树不为空!");
    else
        printf("\n二叉树为空!");
}
 
char Parent(BiTree T,ElemType e) //二叉树存在,e是T中某个节点,若e为非根结点,则返回它的双亲,否则返回空
{
 
   if(T!=NULL)
   {
        if((T->Lchild && (T->Lchild->Data==e)) || (T->Rchild && (T->Rchild->Data==e)))
            return(T->Data);   
        else
        {
            Parent(T->Lchild,e);
            Parent(T->Rchild,e);
        }
   }
   return ' ';
 
}
char Leftchild(BiTree T,ElemType e) //二叉树存在,e是T中某个节点,返回左孩子,若无返回空
{
    char temp;
     if(T!=NULL)
     
        if((T->Lchild!=NULL)&&(T->Data==e))
            temp=T->Lchild->Data;
        else
        {
            Leftchild(T->Lchild,e);
            Leftchild(T->Rchild,e);
        }
        return temp;
 
     }
 
     return ' ';
 
}
char Rightchild(BiTree T,ElemType e) //二叉树存在,e是T中某个节点,返回右孩子,若无返回空
{
    char temp;
     if(T!=NULL)
     
        if((T->Rchild!=NULL)&&(T->Data==e))
            temp=T->Rchild->Data;
        else
        {
            Leftchild(T->Lchild,e);
            Leftchild(T->Rchild,e);
        }
        return temp;
 
     }
 
     return ' ';
 
 
}
 
void Assign(BiTree T,ElemType e,ElemType value )
{
    if(T!=NULL)
     
        if(T->Data==e)
            T->Data=value;
        else
        {
            Assign(T->Lchild,e,value);
            Assign(T->Rchild,e,value);
        }
 
     }
 
}
 
void main()
{
    int flag=1,select;
    ElemType e,value;
    BiTree T;
 
    printf("\n请输入字符,若为'#'表示对应孩子指针为空:");
    CreateBiTree(&T);
     
    printf("\n1.先序遍历显示数");
    printf("\n2.中序遍历显示数");
    printf("\n3.后序遍历显示数");
    printf("\n4.显示树中叶子结点的个数");
    printf("\n5.输出叶子节点");
    printf("\n6.计算树的深度");
    printf("\n7.求指定结点的双亲结点");
    printf("\n8.判断二叉树是否为空树");
    printf("\n9.清空二叉树");
    printf("\n10.输出指定结点的左孩子");
    printf("\n11.输出指定结点的右孩子");
    printf("\n12.将二叉树中结点e赋值为value");
    printf("\n13.退出");
    while(flag)
    {
        printf("\n请选择操作序号:\n");
           scanf("%d",&select);
           getchar();     //这里是为了将回车的字符保存,以免出现字符数据上的差错
           switch(select)
           {
             case 1:
                     printf("\n二叉树先序遍历为:");
                     PreOrder(T);
                     break;
             case 2:
                     printf("\n二叉树中序遍历为:");
                     InOrder(T);
                     break;
             case 3:
                     printf("\n二叉树后序遍历为:");
                     PostOrder(T);
                     break;
             case 4:
                     leaf(T);
                     printf("\n叶子节点个数为:%d",LeafCount);
                     break;
             case 5:
                     printf("\n叶子节点为:");
                     outleaf(T);
                     break;
             case 6:
                     printf("\n树的深度为(0表示空树):%d",PostTreeDepth(T));
                     break;
         
             case 7:
                     printf("\n请输入指定的结点的数值:");
                     e=getchar();
                     getchar();     //这里是为了将回车的字符保存,以免出现字符数据上的差错
                     printf("\n结点 %c 的双亲结点为:%c",e,Parent(T,e));
                     break;
             case 8:
                     BiTreeEmpty(T);
                     break;
             case 9:
                     ClearBiTree(&T);
                     break;
             case 10:
                     printf("\n请输入指定的结点的数值:");
                     e=getchar();
                     getchar();         //这里是为了将回车的字符保存,以免出现字符数据上的差错
                     printf("\n结点 %c的左孩子结点为:%c",e,Leftchild(T,e));
                     break;
             case 11:
                     printf("\n请输入指定的结点的数值:");
                     e=getchar();
                     getchar();            //这里是为了将回车的字符保存,以免出现字符数据上的差错
                     printf("\n结点 %c的右孩子结点为:%c",e,Rightchild(T,e));
                     break;
             case 12:
                     printf("\n请输入指定的结点的数值:");
                     e=getchar();
                     getchar();     //这里是为了将回车的字符保存,以免出现字符数据上的差错
                      
                     printf("\n请输入替换后结点的数值:");
                     value=getchar();
                     getchar();     //这里是为了将回车的字符保存,以免出现字符数据上的差错
                     Assign(T,e,value );
                      
                     printf("\n替换数字后二叉树先序遍历为:");
                     PreOrder(T);
                     break;
             case 13:
                     flag=0;
                      
           }
    }
 
     
}
0 0
原创粉丝点击