Traversing the binary tree data structure

#include <stdio.h>

#include <stdlib.h>
typedef struct Node
{
        char data;
        struct Node* Lchild;
        struct Node* Rchild;
        struct Node* parent;
}BiTNode,*BiTree;
BiTree CreateBiTree(){
    char ch;
    BiTree T;
    scanf("%c",&ch);
    if(ch=='#') T=NULL;
    else
    {
        T =(BiTree)malloc(sizeof(BiTNode));
        T->data = ch;
        T->Lchild = CreateBiTree();
        T->Rchild = CreateBiTree();


    }
    return T;//
}
//先序遍历二叉树
 void PreOrderTraverse(BiTree T)
 {
    if(T)
    {
        The printf ( "% C", T-> Data);
        PreOrderTraverse (T-> Lchild);
        PreOrderTraverse (T-> Rchild);
     }
 }
 // binary tree in preorder
 void InOrderTraverse (BiTree T)
 {
    IF (T)
    {
        InOrderTraverse (T-> Lchild);
        the printf ( "% C", T-> Data);
        InOrderTraverse (T-> Rchild);
     }
  }
  // after traversing binary Tree
  void PostOrderTraverse (BiTree T)
  {
    IF (T)
    {
        PostOrderTraverse (T-> Lchild);
        PostOrderTraverse (T-> Rchild);
        the printf ( "% C", T-> Data);
      }
   }


int main ()
 {
    BiTree T;
    T = CreateBiTree ();
    the printf ( "preorder traversal is: \ n");
    PreOrderTraverse (T);
    the printf ( "\ n-");
    the printf ( "traversal sequence is: \ n ");
    InOrderTraverse (T);
    the printf (" \ n-");
    the printf (" postorder is: \ n-");
    PostOrderTraverse (T);


 }


note 
 /*这里的输入要严格按照正确的顺序才能结束.这里要用到二叉树的一个性质,就是说对于有n个节点的二叉树,就有n+1个空域,在这里即为如果你输入了n个元素,那么一定要有n+1个#才会结束迭代过程.*/创建二叉树的时候 用的是先序遍历 要根据先序的特征来确定#的位置
Published 19 original articles · won praise 8 · views 4141

Guess you like

Origin blog.csdn.net/paohui001lqp/article/details/80321340