- #include <iostream.h>
- #include <iomanip.h>
- #include <strstrea.h>
- #include <stdlib.h>
- #include <stdio.h>
- //定义ElemType为char
- typedef char ElemType;
- //单链表中结点的类型
- typedef struct BiTNode
- {
- ElemType data; // 值域
- struct BiTNode *Lchild; // 左孩子域
- struct BiTNode *Rchild; // 右孩子域
- }BiTNode, *BiTree;
- //初始化二叉树
- void InitBiTree(BiTree & BT);
- //根据输入的二叉树的先根遍历序列字符串建立对应的二叉树存储结构
- void CreateBiTree(BiTree & BT );
- //判断二叉树是否为空
- bool BiTreeEmpty(BiTree BT);
- //按某种遍历次序输出二叉树中的所有结点
- void PreOrder(BiTree BT);
- //中序
- void InOrder(BiTree BT);
- //后序
- void PostOrder(BiTree BT);
- //层次void LeverOrder(BiTree BT);
- //求二叉树的深度
- int BiTreeDepth(BiTree BT);
- //求二叉树中所有结点数
- void BiTreeCount(BiTree BT);
- //求二叉树中所有叶子结点数
- void BiTreeLeafCount(BiTree BT);
- //按照二叉树的一种表示方法输出整个二叉树
- void PrintBiTree(BiTree BT);
- //清除二叉树,使之变为一棵空树
- void ClearBiTree(BiTree & BT);
- int count=0,Leafcount=0;
- int m,n;
- void main()
- {
- BiTree bt;
- char flag='y';
- while(flag=='y')
- {
- //初始化二叉树
- InitBiTree(bt);
- cout <<"请输入字符' '作为空结点的二叉树先根表示字符串:" <<endl;
- //建立以bt作为树根指针的二叉树链接存储结构
- CreateBiTree(bt);
- //以广义表形式输出二叉树
- cout <<"广义表:";
- PrintBiTree(bt);
- cout <<endl;
- //先序遍历二叉树bt
- cout <<"先序:";
- PreOrder(bt) ;
- cout <<endl;
- //中序遍历二叉树bt
- cout <<"中序:";
- InOrder(bt); cout <<endl;
- //后序遍历二叉树bt
- cout <<"后序:";
- PostOrder(bt); cout <<endl;
- /*按层遍历二叉树bt
- cout <<"按层:";
- LeverOrder(bt); cout <<endl;*/
- //求二叉树bt的深度
- m=BiTreeDepth(bt);
- printf("该二叉树有%d层:",m);
- //求二叉树bt中所有结点数
- cout <<"二叉树中所有结点数为:";
- BiTreeCount(bt);
- cout <<count <<endl;
- //求二叉树中所有叶子结点数
- cout <<"二叉树中所有叶子结点数为:";
- BiTreeCount(bt);cout <<Leafcount <<endl;
- //清除二叉树,使之变为一棵空树
- ClearBiTree(bt);
- cout <<"二叉树已经清除!" <<endl;
- cout <<"要否继续实验(y/n)?" <<endl;
- cin >>flag;
- if (flag!='y')
- cout <<"再见!" <<endl;
- }
- }
- //根据输入的二叉树的先根遍历序列字符串建立对应的二叉树存储结构
- void CreateBiTree(BiTree &bt)
- {
- char ch;
- cin>>ch;
- ch=getchar();
- if(ch==' ')
- bt=NULL;
- else
- {
- bt=new BiTNode;
- bt->data=ch;
- CreateBiTree(bt->Lchild);
- CreateBiTree(bt->Rchild);
- }
- }
- //初始化
- void InitBiTree(BiTree &bt)
- {
- bt=NULL;
- }
- //判断二叉树是否为空
- bool BiTreeEmpty(BiTree bt)
- {
- if(bt!=NULL)
- return true;
- else
- return false;
- }
- //按某种遍历次序输出二叉树中的所有结点
- void TraverseBiTree(BiTree bt)
- {
- if(!bt)
- {
- printf("%3c",bt->data);
- TraverseBiTree(bt->Lchild);
- TraverseBiTree(bt->Rchild);
- }
- }
- //求二叉树的深度
- int BiTreeDepth(BiTree bt)
- {
- int hl=0,hr=0,max=0;
- if(bt=!NULL)
- {
- hl=BiTreeDepth(bt->Lchild);
- hr=BiTreeDepth(bt->Rchild);
- max=hl>hr?hl:hr;
- return(max+1);
- }
- else
- return(0);
- }
- //求二叉树中所有结点数
- void BiTreeCount(BiTree bt)
- {
- if(bt)
- count++;
- BiTreeCount(bt->Lchild);
- BiTreeCount(bt->Rchild);
- }
- //求二叉树中所有叶子结点数
- void BiTreeLeafCount(BiTree bt)
- {
- if(bt==NULL)
- return;
- else if(bt->Lchild==NULL&&bt->Rchild==NULL)
- Leafcount++;
- else if(!(bt->Lchild==NULL&&bt->Rchild==NULL))
- {
- BiTreeLeafCount(bt->Lchild);
- BiTreeLeafCount(bt->Rchild);
- }
- }
- //按照二叉树的一种表示方法输出整个二叉树
- void PrintBiTree(BiTree bt)
- {
- if(bt!=NULL)
- {
- printf("%3c",bt->data);
- PrintBiTree(bt->Lchild);
- PrintBiTree(bt->Rchild);
- }
- }
- //清除二叉树,使之变为一棵空树
- void ClearBiTree(BiTree &BT)
- {
- if(BT)
- {
- ClearBiTree( BT->Lchild );
- ClearBiTree( BT->Rchild );
- delete(BT);
- BT=NULL;
- }
- }
- //按先序输出
- void PreOrder(BiTree bt)
- {
- if(bt!=NULL)
- {
- printf("%3c",bt->data);
- PreOrder(bt->Lchild);
- PreOrder(bt->Rchild);
- }
- }
- //中序遍历
- void InOrder(BiTree bt)
- {
- if(bt!=NULL)
- {
- InOrder(bt->Lchild);
- printf("%3c",bt->data);
- InOrder(bt->Rchild);
- }
- }
- //后序
- void PostOrder(BiTree bt)
- {
- if(bt!=NULL)
- {
- PostOrder(bt->Lchild);
- PostOrder(bt->Rchild);
- printf("%3c",bt->data);
- }
- }
二叉树
最新推荐文章于 2022-11-19 15:52:24 发布