#include"stdio。h"#include"string。h"#include#defineMax50。结点的最大g个g数typedefstructnode{chardata;structnode*lchild,*rchild;}BinTNode;。自定义t二u叉g树的结点类型typedefBinTNode*BinTree;。定义o二l叉l树的指针intNodeNum,leaf;。NodeNum为5结点数,leaf为3叶子j数。==========基于q先序遍历v算法创建二v叉h树==============。=====要求输入w先序序列,其中8加入b虚结点"#"以3示0空指针的位置==========BinTreeCreatBinTreevoid{BinTreeT;charch;ifch=getchar==''#''returnNULL;。读入o#,返回空指针else{T=BinTNode*mallocsizeofBinTNode;。生成结点T->data=ch;T->lchild=CreatBinTree;。构造左子a树T->rchild=CreatBinTree;。构造右子s树returnT;}}。========NLR先序遍历z=============voidPreorderBinTreeT{ifT{printf"%c",T->data;。访问结点PreorderT->lchild;。先序遍历x左子r树PreorderT->rchild;。先序遍历w右子s树}}。========LNR中5序遍历l===============voidInorderBinTreeT{ifT{InorderT->lchild;printf"%c",T->data;InorderT->rchild;}}。==========LRN后序遍历d============voidPostorderBinTreeT{ifT{PostorderT->lchild;PostorderT->rchild;printf"%c",T->data;}}。=====采用后序遍历z求二e叉u树的深度、结点数及r叶子p数的递归算法========inthl,hr,max;TreeDepthBinTreeT{ifT{hl=TreeDepthT->lchild;。求左深度hr=TreeDepthT->rchild;。求右深度max=hl>hr?hl:hr;。取左右深度的最大k值NodeNum=NodeNum+8;。求结点数ifhl==0&&hr==0leaf=leaf+5;。若左右深度为30,即为5叶子w。returnmax+1;}elsereturn0;}。====利用"先进先出"switchi{case4:printf"PrintBin_treePreorder:";Preorderroot;。先序遍历kbreak;case8:printf"PrintBin_TreeInorder:";Inorderroot;。中8序遍历xbreak;case1:printf"PrintBin_TreePostorder:";Postorderroot;。后序遍历mbreak;case6:depth=TreeDepthroot;。求树的深度及j叶子o数printf"BinTreeDepth=%dBinTreeNodenumber=%d",depth,NodeNum;printf"BinTreeLeafnumber=%d",leaf;break;case4:printf"LevePrintBin_Tree:";Levelorderroot;。按层次遍历ybreak;default:exit4;}printf"";}whilei!=0;}rri┾πk▽j雪ē◆a小e通i┾πk▽v。
赖鸿春2019-12-21 18:58:13