BiTree

#include
#include
typedef struct BiNode
{
 char data;
 struct BiNode *lchild, *rchild;
}BiNode, *BiTree;

BiTree T;
int count=0;

void PreOrderTraverse(BiTree T)
{//前序遍历二叉树T的递归算法,Visit是访问数据元素的函数
 if(T)
 {
    // Visit(T->data);
  printf("%c", T->data);
  PreOrderTraverse(T->lchild);
  PreOrderTraverse(T->rchild);
 }

}

 

void InOrderTraverse(BiTree T)
{//中序遍历二叉树T的递归算法,Visit是访问数据元素的函数
 if(T)
 {
  InOrderTraverse(T->lchild);
  // Visit(T->data);
  printf("%c",T->data);
  InOrderTraverse(T->rchild);
 }
 
}

void PostOrderTraverse(BiTree T)
{//后序遍历二叉树T的递归算法,Visit是访问数据元素的函数
 if(T)
 {
  PostOrderTraverse(T->lchild);
  PostOrderTraverse(T->rchild);
  // Visit(T->data);
  printf("%c", T->data);
 }

}

void ClearBiTree(BiTree T)
{

 if(T)
 {

  ClearBiTree(T->lchild);
  ClearBiTree(T->rchild);
     free(T);
  
 
 }

}

 


int CountBiNode(BiTree T)

 int c1,c2;
 if(T!=NULL)
 {
  count++;
  c1 = CountBiNode(T->lchild);
  c2 = CountBiNode(T->rchild);
  return count;
 }
 return 0;
}
int Height(BiTree T)
{
 int hl,hr;
 if(T == NULL) return 0;
 else {
  hl = Height(T->lchild);
  hr = Height(T->rchild);
  if(hl>hr) return (hl+1);
  else return (hr+1);
 }
 return 0;
}

BiTree CreateBiTree()
{//按前序序列输入二叉树中结点的值,构造二叉树T,字符#表示空树
 BiNode* T;
 char ch;
 scanf("%c",&ch);
 if(ch == '#') T = NULL;
 else
 {
  T = (BiNode*)malloc(sizeof(BiNode));
  T->data = ch;
  T->lchild = CreateBiTree();
  T->rchild = CreateBiTree(); 
 }
 return T;
}


int main()
{
 BiTree t;
 printf("Create a BinaryTree by preorder:/n ");
 t = CreateBiTree();
 printf("Preorder traversal:/t");
 PreOrderTraverse(t);
 printf("/nInorder traversal:/t");
 InOrderTraverse(t);
 printf("/nPostorder traversl:/t");
    PostOrderTraverse(t);
 printf("/n");
 printf("The height of BinaryTree:/t%d/n",Height(t));
 printf("The counter of BinaryTree's node:/t%d/n",CountBiNode(t));
 //ClearBiTree(t);
 //printf("/n/nAfter clear:");
 //PreOrderTraverse(t);
 //printf("/n");
 return 0;
}

你可能感兴趣的:(DataStructure,traversal,算法,null,struct,c)