欢迎来到天天文库
浏览记录
ID:38044791
大小:20.00 KB
页数:4页
时间:2019-05-25
《数据结构实验二叉树程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#includetypedefstructbtnode{chardata;structbtnode*Llink;structbtnode*Rlink;}btnode,*btree;//创建voidCreateTree(btree*root){charch;ch=getchar();if(ch=='.')*root=NULL;else{*root=(btree)malloc(sizeof(btnode));(*root)->data=ch;CreateTree(&((*root)->Llink)
2、);CreateTree(&((*root)->Rlink));}}//输出voidPrintTree(btreeroot,intnLayer){if(root==NULL)return;PrintTree(root->Llink,nLayer+1);PrintTree(root->Rlink,nLayer+1);for(inti=0;ida
3、ta);PreOrder(root->Llink);PreOrder(root->Rlink);}}//中序遍历显示二叉树voidInOrder(btreeroot){if(root!=NULL){InOrder(root->Llink);printf("%c",root->data);InOrder(root->Rlink);}}//后序遍历显示二叉树voidPostOrder(btreeroot){if(root!=NULL){PostOrder(root->Llink);PostOrder(root->Rlink);printf("%c",
4、root->data);}}//显示叶子结点个数intleaf(btreeroot){intLeafCount;if(root==NULL)LeafCount=0;elseif((root->Llink==NULL)&&(root->Rlink==NULL))LeafCount=1;elseLeafCount=leaf(root->Llink)+leaf(root->Rlink);returnLeafCount;}voidMenu(){printf("*****************************");printf("(1)创建二
5、叉树。");printf("(2)二叉树图形。");printf("(3)先序遍历显示树。");printf("(4)中序遍历显示树。");printf("(5)后序遍历显示树。");printf("(6)统计该树叶子节点个数。");printf("(0)退出。");printf("***************************");}voidActionMenu(btree*lroot){intn=1;btreeflag=NULL,root=*lroot;chare,e1,e2;intdepth=0,h=1,
6、cflag=0;while(n){printf("请选择一个操作:");scanf("%d",&n);switch(n){case1:printf("请输入二叉树:");getchar();CreateTree(&root);break;case2:printf("该二叉树图形为:");PrintTree(root,1);break;case3:printf("先序遍历显示:");PreOrder(root);printf("");break;case4:printf("中序遍历显示:");InOrder(root);printf
7、("");break;case5:printf("后序遍历显示:");PostOrder(root);printf("");break;case6:printf("该树的叶子节点数目为:%d",leaf(root));break;case0:break;default:printf("非法输入!");}}}voidmain(){btreerootx=NULL;Menu();ActionMenu(&rootx);}
此文档下载收益归作者所有