数据结构作业(C语言版的)牛人知道一下哈 不胜感激

来源:百度知道 编辑:UC知道 时间:2024/06/08 14:17:49
回答好加分
第一 二叉树及其应用
作业目的:熟悉二叉树的特征,掌握二叉树的基本算法实现。
作业要求:
(1)对未实现的程序部分给出设计的基本思想、原理和算法描述,并对代码给出注释。
(2)对给出的作业程序认真阅读,再将其补充完整后运行。
(3)保存程序的运行结果,并结合程序进行分析。
注意事项:
源程序代码在运行前注意存盘;运行时注意输入数据的格式要求。
作业内容:
要求采用二叉链表作为存储结构,完成二叉树的建立算法、二叉树的非递归遍历的操作;
1、二叉树的递归创建算法:
void createbintree(bintree *t)
{
char ch;
if ((ch=getchar())==' ')*t=NULL;
else
{
*t=(bintnode *)malloc(sizeof(bintnode));
(*t)->data=ch;
createbintree(&(*t)->lchild);
createbintree(&(*t)->rchild);
}
}
2、二叉树前序遍历的非递归函数
void preorder1(bintree t)
{
seqstack s;
s.top=-1;
while ((t) || (s.top!=-1))
{
while (t)
{
printf("%c ",t->data);
s.top++;
s.data[s.top]=t;
t=t->lchild;
}
if (s.top>-1)
{
t=pop(&s);
t=t->rchild;
}
}
}
3、二叉树中序遍历的非递归函数
void inorder1(bintree t)
{
seqstack s

这是我做的两个程序的代码:要是需要更多的排序算法的代码或者其他数据结构实现就跟我联系rain.jiang@qq.com
先给你复制这两个代码:
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define NULL 0
typedef struct bitnode{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
/*创建一个二杈树以#号结束*/
bitree create(bitree t){
char ch;
ch=getchar();
if(ch=='#')
t=NULL;
else{
t=(bitree)malloc(sizeof(bitnode));
t->data=ch;
t->lchild=create(t->lchild);
t->rchild=create(t->rchild);
}
return t;
}
/*递归遍历*/
void preorder(bitree t){
if(t){
printf("%c",t->data); /*先序*/
preorder(t->lchild);
/*printf("%c",t->data); 中序*/
preorder(t->rchild);
/*printf("%c",t->data); 后序*/
}
}