二叉树——数据结构课堂作业

↖(^ω^)↗

 1 /*
 2 
 3 编写算法函数void preorder1(bintree t)实现二叉树t的非递归前序遍历。
 4 
 5 */
 6 
 7 #include "bintree.h"
 8 char *a="ABC##D#E##F##";  /*扩充二叉树序树t的前序序列*/
 9 
10 /*函数preorder1()的功能是非递归前序遍历二叉树t,请将函数补充完整并调试运行*/
11 void preorder1(bintree t)
12 {
13     bintree p = t;
14 
15     seqstack s;
16     init(&s);
17     while(p!=NULL||!empty(&s))
18     {
19 
20         while(p!=NULL)
21         {
22             printf("%c",p->data);
23             push(&s,p);
24             p = p->lchild;
25         }
26 
27         if(!empty(&s))
28         {
29             p=pop(&s);
30             p=p->rchild;
31         }
32     }
33 }
34 
35 int main()
36 {
37     bintree t;
38     t=creatbintree();   /*建立二叉树t的存储结构*/
39     printf("二叉树的前序序列为:
");
40     //preorder(t);
41     preorder1(t);       /*前序非递归遍历二叉树*/
42     return 0;
43 }
实现二叉树t的非递归前序遍历
 1 /*
 2 编写算法函数void levelbintree(bintree t),实现二叉树的层次遍历。
 3 */
 4 
 5 #include "bintree.h"
 6 #include "queue"
 7 
 8 using namespace std;
 9 
10 char *a="ABC##D#E##F##";              /*扩充二叉树序树t的前序序列*/
11 void levelbintree(bintree t)
12 {
13     queue<bintree> q;
14     q.push(t);
15 
16     while(!q.empty()) {
17         bintree s = q.front();
18         q.pop();
19         printf("%c",s->data);
20         if(s->lchild!=NULL) {
21             q.push(s->lchild);
22         }
23         if(s->rchild!=NULL) {
24             q.push(s->rchild);
25         }
26 
27     }
28 }
29 
30 int main()
31 {   bintree t;
32     t=creatbintree();       /*建立二叉树t的存储结构*/
33     printf("二叉树的层次序列为:
");
34     levelbintree(t);       /*层次遍历二叉树*/
35     return 0;
36 }
实现二叉树的层次遍历
 1 /*
 2 编写函数bintree prelist(bintree t),bintree postfirst(bintree t),
 3 分别返回二叉树t在前序遍历下的最后一个结点地址和后序遍历下的第一个结点地址。
 4 */
 5 
 6 #include "bintree.h"
 7 char *a="ABC##D##EF#G###";  /*扩充二叉树序树t的前序序列*/
 8 
 9 
10 bintree prelast(bintree t)
11 {
12     while(t->lchild!=NULL||t->rchild!=NULL)
13     {
14         if(t->rchild!=NULL)
15             while(t->rchild!=NULL)
16                 t = t->rchild;
17         if(t->lchild!=NULL)
18             t= t->lchild;
19     }
20     return t;
21 
22 }
23 
24 bintree postfirst(bintree t)
25 {
26     while(t->lchild!=NULL||t->rchild!=NULL)
27     {
28         if(t->lchild!=NULL)
29             while(t->lchild!=NULL)
30                 t = t->lchild;
31 
32         if(t->rchild!=NULL)
33             t= t->rchild;
34     }
35     return t;
36 
37 }
38 
39 int main()
40 {
41     bintree t,p,q;
42     t=creatbintree();       /*建立二叉树t的存储结构*/
43    // preorder(t);
44     p=prelast(t);
45     q=postfirst(t);
46     if (t!=NULL)
47     {
48         printf("前序遍历最后一个结点为:%c
",p->data);
49         printf("后序遍历第一个结点为:%c
",q->data);
50     }
51     else    printf("二叉树为空!");
52     return 0;
53 }
分别返回二叉树t在前序遍历下的最后一个结点地址和后序遍历下的第一个结点地址
 1 /*
 2 假设二叉树采用链式方式存储,t为其根结点,编写一个函数int Depth(bintree t, char x),求值为x的结点在二叉树中的层次。
 3 */
 4 #include "bintree.h"
 5 char *a="ABC##D##EF#G###";          /*扩充二叉树序树t的前序序列*/
 6 
 7 /*
 8      函数Depth,功能:求结点x所在的层次
 9 */
10 
11 int d;
12 int dfs(bintree t,char x,int level)
13 {
14     if(t->data==x)
15     {
16         d = level;
17     }
18     if(t->lchild!=NULL)
19     {
20         dfs(t->lchild,x,level+1);
21     }
22     if(t->rchild!=NULL)
23     {
24         dfs(t->rchild,x,level+1);
25     }
26 }
27 
28 int Depth(bintree t,char x)
29 {
30     dfs(t,x,0);
31     return d;
32 }
33 
34 int main()
35 {
36     bintree root;
37     char x;
38     int k=0;
39     root=creatbintree();
40     printf("请输入树中的1个结点值:
");
41     scanf("%c",&x);
42     k=Depth(root,x);
43     printf("%c结点的层次为%d
",x,k);
44 }
求值为x的结点在二叉树中的层次
 1 /*
 2    试编写一个函数,将一棵给定二叉树中所有结点的左、右子女互换。
 3 */
 4 #include "bintree.h"
 5 #include "algorithm"
 6 
 7 using namespace std;
 8 
 9 char *a="ABC##D##EF#G###";          /*扩充二叉树序树t的前序序列*/
10 /*请将本函数补充完整,并进行测试*/
11 
12 void change(bintree t)
13 {
14     if(t->lchild!=NULL&&t->rchild!=NULL) {
15         swap(t->lchild,t->rchild);
16         change(t->lchild);
17         change(t->rchild);
18     }
19 
20 }
21 
22 int main()
23 {
24     bintree root;
25     root=creatbintree();
26     change(root);
27     preorder(root);
28 }
给定二叉树中所有结点的左、右子女互换
 1 /*
 2 试编写一个递归函数bintree buildBintree(char *pre, char *mid, int length),
 3 根据二叉树的前序序列pre、中序序列mid和前序序列长度length,构造二叉树的二叉链表存储结构,
 4 函数返回二叉树的树根地址。
 5 */
 6 
 7 #include "bintree.h"
 8 #include <string.h>
 9 char *a="";
10 /*请将本函数补充完整,并进行测试*/
11 
12 char pre[100],in[100];
13 
14 int find(int l,int r,char ch)
15 {
16     for(int i=l;i<r;i++)
17         if(in[i] == ch)
18             return i;
19     return -1;
20 }
21 
22 void make(int prel,int prer,int inl,int inr)
23 {
24     if(prel>=prer)
25         return ;
26     if(prel==prer-1)
27     {
28         printf("%c",pre[prel]);
29         return ;
30     }
31 
32     int pos = find(inl,inr,pre[prel]);
33 
34     make(prel+1,prel+pos-inl+1,inl,pos);
35     make(prel+pos-inl+1,prer,pos+1,inr);
36     printf("%c",pre[prel]);
37 }
38 
39 
40 bintree buildBintree(char *pre, char *mid,int length)
41 {
42     make(0,length,0,length);
43     puts("");
44 }
45 
46 int main()
47 {
48     bintree root;
49 
50     puts("请输入前序序列:");
51     gets(pre);
52     puts("请输入中序序列:");
53     gets(in);
54     puts("后序序列是:");
55     //root=buildBintree(pre,mid,strlen(pre));
56     buildBintree(pre,in,strlen(pre));
57 
58     //postorder(root);
59 }
根据二叉树的前序序列pre、中序序列mid和前序序列长度length,构造二叉树的二叉链表存储结构
原文地址:https://www.cnblogs.com/TreeDream/p/6209279.html