复习下二叉树,创建二叉树,分别以先序,中序,后续三种遍历访问二叉树,输出二叉树的叶子节点及叶子节点的个数,并输出二叉树的高度
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- using namespace std;
- typedef struct BiTNode
- {
- char data;
- struct BiTNode *lchild,*rchild;
- }BiTNode,*BiTree;
- int Max(int x,int y)
- {
- return x>y?x:y;
- }
- void Create(BiTree &T)
- {
- char ch;
- scanf("%c",&ch);
- if(ch==‘#‘)
- T=NULL;
- else
- {
- T=(BiTNode *)malloc(sizeof(BiTNode));
- T->data=ch;
- Create(T->lchild);
- Create(T->rchild);
- }
- }
- void Preorder(BiTree &root)
- {
- if(root!=NULL)
- {
- printf("%c ",root->data);
- Preorder(root->lchild);
- Preorder(root->rchild);
- }
- }
- void Inorder(BiTree &root)
- {
- if(root!=NULL)
- {
- Inorder(root->lchild);
- printf("%c ",root->data);
- Inorder(root->rchild);
- }
- }
- void Postorder(BiTree &root)
- {
- if(root!=NULL)
- {
- Postorder(root->lchild);
- Postorder(root->rchild);
- printf("%c ",root->data);
- }
- }
- void Preorderleaf(BiTree &root)
- {
- if(root!=NULL)
- {
- if(root->lchild==NULL&&root->rchild==NULL)
- printf("%c ",root->data);
- Preorderleaf(root->lchild);
- Preorderleaf(root->rchild);
- }
- }
- int LeafCount(BiTree &root)
- {
- int leaf;
- if(root==NULL)
- leaf=0;
- else if(root->lchild==NULL&&root->rchild==NULL)
- leaf=1;
- else
- leaf=LeafCount(root->lchild)+LeafCount(root->rchild);
- return leaf;
- }
- int PostTreeDepth(BiTree &root)
- {
- int hl,hr,max;
- if(root!=NULL)
- {
- hl=PostTreeDepth(root->lchild);
- hr=PostTreeDepth(root->rchild);
- max=Max(hl,hr);
- return max+1;
- }
- else
- return 0;
- }
- void dowork()
- {
- BiTree cam;
- Create(cam);
- Preorder(cam);
- printf("\n");
- Inorder(cam);
- printf("\n");
- Postorder(cam);
- printf("\n");
- printf("叶子节点:");
- Preorderleaf(cam);
- printf("\n");
- printf("叶子节点的个数为:%d\n",LeafCount(cam));
- printf("树的深度为:%d\n",PostTreeDepth(cam));
- }
- int main()
- {
- dowork();
- return 0;
- }