# include<stdio.h>
# include<malloc.h>
typedef char ElemType;
typedef struct Node
{
ElemType data;
struct Node *LChild;
struct Node *RChild;
}BiTNode,*BiTree;
// 二叉链表的建立
void CreateBiTree ( BiTree * bt )
{
char ch ;
ch = getchar();
if (ch=='. ') * bt=NULL;
else
{
*bt=(BiTree)malloc(sizeof(BiTNode));
(* bt) -> data = ch ;
CreateBiTree ( & ( ( * bt ) -> LChild ) );
CreateBiTree ( & ( ( * bt ) -> RChild ) );
}
}
//输出二叉树的结点 先序
void PreOrder1 ( BiTree root )
{
if(root!=NULL)
{
printf("%c",root->data );
PreOrder1( root -> LChild );
PreOrder1( root -> RChild );
}
}
//输出二叉树的结点 中序
void PreOrder2 ( BiTree root)
{
if(root!=NULL)
{
PreOrder2( root ->LChild);
printf("%c",root->data );
PreOrder2( root -> RChild);
}
}
//输出二叉树的结点 后序
void PreOrder3 ( BiTree root )
{
if (root!=NULL)
{
PreOrder3( root-> LChild );
PreOrder3( root-> RChild );
printf("%c",root->data );
}
}
//先序输出二叉树的叶子结点
void PreOrder(BiTree root)
{
if (root!=NULL)
{
if(root-> LChild== NULL && root-> RChild==NULL)
printf("%c",root->data);
PreOrder(root->LChild);
PreOrder(root->RChild);
}
}
//先序遍历二叉树的高度
void PreTreeDepth ( BiTree bt, int h )
{
int depth=0;
if(bt!=NULL)
{
if ( h>depth)
depth = h;
PreTreeDepth(bt->LChild,h+1);
PreTreeDepth(bt->RChild,h+1);
}
}
void main ( )
{
BiTree A;
int h=0;
printf ("请输入二叉树的扩展先序序列:\n");
CreateBiTree(&A);
printf ("按先序遍历法输出该二叉树的元素为:\n");
PreOrder1( A );
printf ("按中序遍历法输出该二叉树的元素为:\n");
PreOrder2( A ) ;
printf ("按后序遍历法输出该二叉树的元素为:\n");
PreOrder3( A );
printf ("按先序遍历法输出该二叉树的叶子结点元素为:");
PreOrder( A );
printf ("按先序遍历法输出该二叉树的高度为:");
PreTreeDepth(A,h);
}
# include<malloc.h>
typedef char ElemType;
typedef struct Node
{
ElemType data;
struct Node *LChild;
struct Node *RChild;
}BiTNode,*BiTree;
// 二叉链表的建立
void CreateBiTree ( BiTree * bt )
{
char ch ;
ch = getchar();
if (ch=='. ') * bt=NULL;
else
{
*bt=(BiTree)malloc(sizeof(BiTNode));
(* bt) -> data = ch ;
CreateBiTree ( & ( ( * bt ) -> LChild ) );
CreateBiTree ( & ( ( * bt ) -> RChild ) );
}
}
//输出二叉树的结点 先序
void PreOrder1 ( BiTree root )
{
if(root!=NULL)
{
printf("%c",root->data );
PreOrder1( root -> LChild );
PreOrder1( root -> RChild );
}
}
//输出二叉树的结点 中序
void PreOrder2 ( BiTree root)
{
if(root!=NULL)
{
PreOrder2( root ->LChild);
printf("%c",root->data );
PreOrder2( root -> RChild);
}
}
//输出二叉树的结点 后序
void PreOrder3 ( BiTree root )
{
if (root!=NULL)
{
PreOrder3( root-> LChild );
PreOrder3( root-> RChild );
printf("%c",root->data );
}
}
//先序输出二叉树的叶子结点
void PreOrder(BiTree root)
{
if (root!=NULL)
{
if(root-> LChild== NULL && root-> RChild==NULL)
printf("%c",root->data);
PreOrder(root->LChild);
PreOrder(root->RChild);
}
}
//先序遍历二叉树的高度
void PreTreeDepth ( BiTree bt, int h )
{
int depth=0;
if(bt!=NULL)
{
if ( h>depth)
depth = h;
PreTreeDepth(bt->LChild,h+1);
PreTreeDepth(bt->RChild,h+1);
}
}
void main ( )
{
BiTree A;
int h=0;
printf ("请输入二叉树的扩展先序序列:\n");
CreateBiTree(&A);
printf ("按先序遍历法输出该二叉树的元素为:\n");
PreOrder1( A );
printf ("按中序遍历法输出该二叉树的元素为:\n");
PreOrder2( A ) ;
printf ("按后序遍历法输出该二叉树的元素为:\n");
PreOrder3( A );
printf ("按先序遍历法输出该二叉树的叶子结点元素为:");
PreOrder( A );
printf ("按先序遍历法输出该二叉树的高度为:");
PreTreeDepth(A,h);
}