为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

数据结构二叉树的实现与三种遍历

2019-04-28 6页 doc 17KB 40阅读

用户头像

is_721103

暂无简介

举报
数据结构二叉树的实现与三种遍历// 实现二叉树的先序、中序和后序遍历 #define MAX 30 //二叉树中最多结点数 #define NULL 0 #include #include typedef struct btnode //二叉树的结点类型 { char data; struct btnode *lchild,*rchild; }bttree; bttree *cre_tree(char *str,int i,int m) //将字符串中的第i个字符到第个m字符作为数据生成对应的满二叉树 { bttree *p;...
数据结构二叉树的实现与三种遍历
// 实现二叉树的先序、中序和后序遍历 #define MAX 30 //二叉树中最多结点数 #define NULL 0 #include #include typedef struct btnode //二叉树的结点类型 { char data; struct btnode *lchild,*rchild; }bttree; bttree *cre_tree(char *str,int i,int m) //将字符串中的第i个字符到第个m字符作为数据生成对应的满二叉树 { bttree *p; if(i>m) //无效结点 return NULL; p=(bttree *)malloc(sizeof(bttree)); //生成新结点 p->data=str[i]; p->lchild=cre_tree(str,2*i+1,m); //创建左子树 p->rchild=cre_tree(str,2*i+2,m); //创建右子树 return p; } void lev_order(char s[],int n) //层次遍历,即输出字符数组的元素 { int i; for(i=0;i"); } } void preorder(bttree *t) //先序遍历二叉树 { if(t!=NULL) { printf("%c",t->data); if(t->lchild) { printf("->"); preorder(t->lchild); } if(t->rchild) { printf("->"); preorder(t->rchild); } } } void inorder(bttree *t) //中序遍历二叉树{ if(t!=NULL) { inorder(t->lchild); printf("%c",t->data); printf("->"); inorder(t->rchild); } } void postorder(bttree *t) //后序遍历二叉树{ if(t!=NULL) { postorder(t->lchild); postorder(t->rchild); printf("%c",t->data); printf("->"); } } main() //主函数 { int i,n; char str[MAX]; bttree *root; //指向根结点的指针 printf("please input a bttree node number:\n"); scanf("%d",&n); getchar(); //输入数字 printf("please input a string which length is %d:",n); for(i=0;i
/
本文档为【数据结构二叉树的实现与三种遍历】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索