欢迎来到天天文库
浏览记录
ID:9314830
大小:72.51 KB
页数:9页
时间:2018-04-27
《关于树的各种c语言代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、集合了树的各种算法,已经运行过,本人亲自所写,先序,后序,等全包括#include#include#defineMax100typedefintStatus;typedefstructBiTNode{chardata;structBiTNode*lchild,*rchild;}BiTNode,*BiTree;intcount;StatusCreatBiTree(BiTree*bt)/*1.按先序遍历序列创造二叉树的二叉链表*/{charch;getchar();scanf
2、("%c",&ch);if(ch==''){*bt=NULL;}else{*bt=(BiTree)malloc(sizeof(BiTNode));(*bt)->data=ch;printf("t请输入%c结点的左孩子:",(*bt)->data);CreatBiTree(&((*bt)->lchild));printf("t请输入%c结点的右孩子:",(*bt)->data);CreatBiTree(&((*bt)->rchild));}return1;}voidPreOrder(BiTreebt
3、)/*2.先序遍历二叉树*/{if(bt!=NULL){printf("%c",bt->data);PreOrder(bt->lchild);PreOrder(bt->rchild);}}voidInOrder(BiTreebt)/*3.中序遍历二叉树*/{if(bt!=NULL){InOrder(bt->lchild);printf("%c",bt->data);InOrder(bt->rchild);}}voidPostOrder(BiTreebt)/*4.后序遍历二叉树*/{if(bt!=NUL
4、L){PostOrder(bt->lchild);PostOrder(bt->rchild);printf("%c",bt->data);}}voidPreOrderLeaf(BiTreebt)/*5.输出所有的叶子结点*/{if(bt!=NULL){if((bt->lchild==NULL)&&(bt->rchild==NULL)){printf("%c",bt->data);}PreOrderLeaf(bt->lchild);PreOrderLeaf(bt->rchild);}}StatusLea
5、f(BiTreebt)/*6.统计叶子结点数目,即度为零的结点数目*/{if(bt==NULL){count=0;}elseif((bt->lchild==NULL)&&(bt->rchild==NULL)){count=1;}else{count=Leaf(bt->lchild)+Leaf(bt->rchild);}returncount;}voidDegree1Node(BiTreebt)/*7.输出度为一的结点*/{if(bt!=NULL){if(((bt->lchild!=NULL)
6、
7、(bt->rc
8、hild!=NULL))&&(!((bt->lchild!=NULL)&&(bt->rchild!=NULL)))){printf("%c",bt->data);}Degree1Node(bt->lchild);Degree1Node(bt->rchild);}}voidDegree2Node(BiTreebt)/*8.输出度为二的结点*/{if(bt!=NULL){if((bt->lchild!=NULL)&&(bt->rchild!=NULL)){printf("%c",bt->data);}De
9、gree2Node(bt->lchild);Degree2Node(bt->rchild);}}StatusCountNode(BiTreebt)/*9.统计二叉树中结点的总数*/{if(bt==NULL){return0;}else{count++;CountNode(bt->lchild);CountNode(bt->rchild);returncount;}}StatusTreeDepth(BiTreebt)/*10.求二叉树的深度*/{intldep,rdep;if(bt==NULL){return0
10、;}else{ldep=TreeDepth(bt->lchild);rdep=TreeDepth(bt->rchild);if(ldep>rdep){return(ldep+1);}else{return(rdep+1);}}}voidPrintTree(BiTreebt,intnlayer)/*11.按树状打印二叉树*/{if(bt==NULL)/*如果是空则直接退出函数*/{return;}Pr
此文档下载收益归作者所有