資源描述:
《二叉樹的建立與遍歷、深度.doc》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、個(gè)人收集整理勿做商業(yè)用途二叉樹的創(chuàng)建、遍歷、深度、葉子節(jié)點(diǎn)個(gè)數(shù)2007-04-2512:18749人閱讀評論(0)收藏舉報(bào)#include<stdio.h>#include<malloc.h>typedefstruct bnode?...{?chardata;?structbnode*left,*right;}btree;?btree(cuò)*creat()...{? btree(cuò)*t;charp;? p=getchar();if(p=='#')?returnNULL;?else...{?t=(btree*)malloc(sizeof(btree));?t->data=p;t-
2、>left=creat();?t->right=creat(yī)();?returnt;??。?}?voidpreorder(btree*t)?...{?if(t!=NULL)...{printf("%3c",t->dat(yī)a);preorder(t->left); preorder(t->right);}?}voidinorder(btree*t)?...{if (t!=NULL)?...{?inorder(t->left);printf("%3c",t->data);inorder(t->right);?}}?voidpostorder(btree*t)...{?if (
3、t!= NULL)...{?postorder(t->left);postorder(t->right);printf("%3c",t->data);}?}int?。?0;?intleaves(btree*t)...{if(t!= NULL)...{?if(t->left==NULL&&t->right== NULL)?m++;?else?...{?leaves(t->left);?leaves(t->right);}}returnm;}?int depth(btree*t)...{intdep1,dep2;if (t==NULL)?return0;?else個(gè)人收集
4、整理勿做商業(yè)用途...{?dep1=?。鋏pth(t->left);dep2=depth(t->right);?return(dep1>dep2?dep1+1: dep2+ 1);}?}?intmain()?...{printf("輸入創(chuàng)建二叉樹的字符");btree(cuò)*tree_1=?。悖騟at(yī)();?printf("先序遍歷");?preorder(tree_1);printf(" "); printf("中序遍歷");? inorder(tree_1);?printf("");printf("后序遍歷");?postorder(tree_1);?printf(""
5、);int num=leaves(tree_1);printf("葉節(jié)點(diǎn)的個(gè)數(shù):%d",num);printf("二叉樹的深度:%d",depth(tree(cuò)_1));}