数据结构实验程序参考 联系客服

发布时间 : 星期二 文章数据结构实验程序参考更新完毕开始阅读7b06cd0c763231126edb11c9

{ printf(\ /*访问右孩子并将其指针入队*/ Q[++rear]=p->rchild; } } }

int depth(Bitnode *t) /*求二叉树高度递归算法*/ { int high,lhigh,rhigh; if(!t) high=0; else

{ lhigh=depth(t->lchild); rhigh=depth(t->rchild); if(lhigh>rhigh) high=lhigh+1; else

high=rhigh+1; } return high; }

Bitnode *child_swap(Bitnode *t) /*交换二叉树中每一结点的左右孩子,递归算法*/

{ Bitnode *root; if(!t) root=NULL; else

{ root=(Bitnode *)malloc(sizeof(Bitnode)); root->data=t->data;

root->rchild=child_swap(t->lchild); root->lchild=child_swap(t->rchild); } return root; }

int leafs(Bitnode *t) /*统计二叉树中叶子结点数目,递归算法*/ { int num;

if(t==NULL) num=0; else

if(t->lchild==NULL && t->rchild==NULL) num=1; else num=leafs(t->lchild)+leafs(t->rchild); return num; } main()

{ Bitnode *t,*root;

printf(\ /*root=CreatBitree_pre(); t=child_swap(root); */

t=CreatBitree_pre();

printf(\ preorder(t); printf(\ inorder2(t); printf(\ postorder(t); printf(\ printf(\

printf(\}

5. 二叉排序树

#include \#define MAX 20 typedef struct btnode { int data;

struct btnode *lchild, *rchild; } Bitnode,*Bitree;

insert (Bitree *t, Bitree s) { if (*t==NULL) *t=s; else if (s->data<(*t)->data)

insert (&((*t)->lchild),s);

else insert(&((*t)->rchild),s); }

Bitree creat() { Bitree t, s; int k; t=NULL; scanf(\

while(k>0)

{ s=(Bitnode *)malloc(sizeof(Bitnode)); s->data=k; s->lchild=NULL; s->rchild=NULL; insert(&t,s); scanf(\ }; return t; }

inorder(Bitnode *t) { if (t)

{ inorder(t->lchild); printf(\ inorder(t->rchild); } } main() { Bitnode *t;

printf(\ t=creat();

printf(\ inorder(t); }

6. 图的遍历(用邻接表作存储结构 )

#include