发布时间 : 星期六 文章南昌大学数据结构实验程序更新完毕开始阅读6ac0e95e844769eae009ed63
} else
{s=stackpush(s,*p);k++; if(k==1)
printf(\}
if(s.top==-1&&*p=='\\0') printf(\}
四、思考与提高
1.栈的两种存储结构在判别栈空与栈满时,所依据的条件有何不同?
实验五 二叉树基本操作实验
一 、实验目的
1.学会实现二叉树结点结构和对二叉树的基本操作。
2.掌握对二叉树每种操作的具体实现,学会利用递归方法编写对二叉树这种递归数据结构进行处理的算法。
二 、实验要求
1.认真阅读和掌握和本实验相关的教材内容。 2.编写完整程序完成下面的实验内容并上机运行。 3.整理并上交实验报告。
三、实验内容
1. 编写程序生成下面所示的二叉树; 2.用中序遍历的方法输出树中各结点值; 3.用先序遍历的方法输出树中各结点值; 4.用后序遍历的方法输出树中各结点值;
程序如下:
#include
struct node *lchild,*rchild; }binode,*bitree; /*建立树*/
binode *creat_tree() { binode *t; char c;
c=getchar();
if(c=='#') return(NULL); else
{t=(binode *)malloc(sizeof(binode)); t->data=c;
t->lchild=creat_tree(); t->rchild=creat_tree(); }
return(t); }
/*先序遍历*/
void preorder(binode *p) { if(p!=NULL) {
printf(\preorder(p->lchild); preorder(p->rchild); } }
/*中序遍历*/
void inorder(binode *p) { if(p!=NULL) { inorder(p->lchild); printf(\inorder(p->rchild); } }
/*后序遍历*/
void postorder(binode *p) { if(p!=NULL)
{ postorder(p->lchild); postorder(p->rchild); printf(\} }
/*主函数*/ void main() {binode *t; t=creat_tree();
printf(\先序遍历*******:\\n\preorder(t);