#include<queue>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct node
{
char date;
node * Lchild;
node * Rchild;
}BTnode, * BiTree;
void CreateBiTree(BiTree * bitree)
{
char c=getchar();
if(c=='.')
*bitree = NULL;
else
{
(*bitree)=(BiTree)malloc(sizeof(BTnode));
(*bitree)->date = c;
createBiTree(& ((*bitree)->Lchild));
createBiTree(& ((*bitree)->Rchild));
}
}
void PreOder(BiTree root)
{
if(root != NULL)
{
print("%c",root->date);
PreOder(root->Lchild);
PreOder(root->Rchild);
}
}
void InOder(BiTree root)
{
if(root != NULL)
{
InOder(root->Lchild);
print("%c", root->date);
InOder(root->Rchild);
}
}
void PostOder(BiTree bitree)
{
if(root != NULL)
{
PostOder(root->Lchild);
PostOder(root->Rchild);
print("%c",root-date);
}
}
void LayerOder(Bitree bitree)
{
if( bitree == NULL) return;
BTnode * p = (BTnode*)malloc(sizeof(BTnode));
queue <BTnode*> Q;
while( !Q.empty()) Q.pop();
Q.push(bitree);
while( !Q.empty())
{
p = Q.front();
Q.pop();
printf("%c",p->date);
if( p->Lchild ) Q.push( p->Lchild );
if( p->Rchild ) Q.push( p->Rchild );
}
}
int CountLeaf( BiTree root)
{
if( root==NULL ) return 0;
if( root->Lchild==NULL && root->Rchild==NULL) return 1;
return CountLeaf(root->Lchild)+CountLeaf(root->Rchild);
}
int GetBiTreeHigh(BiTree bitree)
{
if(bitree==NULL)
return 0;
return max(GetBiTreeHigh(bitree->Lchild),GetBiTreeHigh(bitree->Rchild))+1;
}
int main()
{
print("Input date of BiTree Node by PreOder , \nUppercase is Ok , .is NULL\n");
int Case = 10;
while(Case--)
{
BiTree tree;
CreateBiTree(&tree);
deepcount = 0;
Leafcount = 0;
printf("\n\nPreOder:\n\n")
PreOder(tree);
printf("\n\nInOder:\n\n")
InOder(tree);
printf("\n\nPostOder:\n\n")
PostOder(tree);
printf("\n\nLayerOder:\n\n")
LayerOder(tree);
printf("\n\n\nLeaves Node:\n")
printf("\n%d Leaves Node in this BiTree\n\n",CountLeaf(tree));
printf("\n%d is the High of this BiTree\n\n\n", GetBiTreeHigh(tree));
printf("\n%d You can start testing this Program again\n");
char ch = getchar();
}
}
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct node
{
char date;
node * Lchild;
node * Rchild;
}BTnode, * BiTree;
void CreateBiTree(BiTree * bitree)
{
char c=getchar();
if(c=='.')
*bitree = NULL;
else
{
(*bitree)=(BiTree)malloc(sizeof(BTnode));
(*bitree)->date = c;
createBiTree(& ((*bitree)->Lchild));
createBiTree(& ((*bitree)->Rchild));
}
}
void PreOder(BiTree root)
{
if(root != NULL)
{
print("%c",root->date);
PreOder(root->Lchild);
PreOder(root->Rchild);
}
}
void InOder(BiTree root)
{
if(root != NULL)
{
InOder(root->Lchild);
print("%c", root->date);
InOder(root->Rchild);
}
}
void PostOder(BiTree bitree)
{
if(root != NULL)
{
PostOder(root->Lchild);
PostOder(root->Rchild);
print("%c",root-date);
}
}
void LayerOder(Bitree bitree)
{
if( bitree == NULL) return;
BTnode * p = (BTnode*)malloc(sizeof(BTnode));
queue <BTnode*> Q;
while( !Q.empty()) Q.pop();
Q.push(bitree);
while( !Q.empty())
{
p = Q.front();
Q.pop();
printf("%c",p->date);
if( p->Lchild ) Q.push( p->Lchild );
if( p->Rchild ) Q.push( p->Rchild );
}
}
int CountLeaf( BiTree root)
{
if( root==NULL ) return 0;
if( root->Lchild==NULL && root->Rchild==NULL) return 1;
return CountLeaf(root->Lchild)+CountLeaf(root->Rchild);
}
int GetBiTreeHigh(BiTree bitree)
{
if(bitree==NULL)
return 0;
return max(GetBiTreeHigh(bitree->Lchild),GetBiTreeHigh(bitree->Rchild))+1;
}
int main()
{
print("Input date of BiTree Node by PreOder , \nUppercase is Ok , .is NULL\n");
int Case = 10;
while(Case--)
{
BiTree tree;
CreateBiTree(&tree);
deepcount = 0;
Leafcount = 0;
printf("\n\nPreOder:\n\n")
PreOder(tree);
printf("\n\nInOder:\n\n")
InOder(tree);
printf("\n\nPostOder:\n\n")
PostOder(tree);
printf("\n\nLayerOder:\n\n")
LayerOder(tree);
printf("\n\n\nLeaves Node:\n")
printf("\n%d Leaves Node in this BiTree\n\n",CountLeaf(tree));
printf("\n%d is the High of this BiTree\n\n\n", GetBiTreeHigh(tree));
printf("\n%d You can start testing this Program again\n");
char ch = getchar();
}
}