`
java-mans
  • 浏览: 11414738 次
文章分类
社区版块
存档分类
最新评论

二叉树的建立及递归遍历

 
阅读更多
#include<stdio.h>
#include<malloc.h>
#include<iostream>

//定义节点 
typedef struct BiNode{
        char data;
        struct BiNode *lch;
        struct BiNode *rch;
}BiNode,*BiTree;

//先序拓展序列建立二叉树 
void Create(BiTree &T)
{
        T =(BiNode*) malloc (sizeof(BiNode));
        
        printf("Enter the data \n");
        scanf(" %c",&T->data);
        if(T->data=='#') T = NULL;
        if(T){
                printf("");
                Create(T->lch);
                Create(T->rch);
        }
}

//先序遍历 (递归)
void Preorder (BiTree T)
{                    
   if (T) {
      printf(" %c",T->data);             // 访问根结点
      
      Preorder(T->lch); // 遍历左子树
      Preorder(T->rch);// 遍历右子树
   }
}

//中序遍历 (递归)
void Inorder (BiTree T)
{
     if(T) {
       Inorder(T->lch);
       
       printf(" %c",T->data);
       
       Inorder(T->rch);    
       }
} 

//后序遍历 (递归)
void Postorder (BiTree T)
{
     if(T) {
       Postorder(T->lch);
       Postorder(T->rch);
       
       printf(" %c",T->data); 
     }
} 

int main()
{
    //建树 
    printf("The fuction Create() is called.\n");
    BiTree T;
    Create(T);
    
    //三种遍历递归算法 
    printf("\n");    
    printf("The fuction Preorder() is called.\n");
    Preorder(T);
    
    printf("\n");
    printf("The fuction Inorder() is called.\n");
    Inorder(T);
    
    printf("\n");
    printf("The fuction Postorder() is called.\n");
    Postorder(T);
    
    
    printf("\n");
    system("pause");
    
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics