Advertisement

先序构建二叉树

阅读量:
复制代码
 #include <stdio.h>

    
 #include <stdlib.h>
    
 typedef struct node {
    
      char data;
    
      struct node  *lch, *rch;
    
 } Bnode, *BiTree;
    
  
    
 void CreateBiTree(BiTree &T)//先序遍历的顺序生成树 
    
 {
    
    char ch;
    
    scanf("%c",&ch);
    
   if (ch=='#')   T=NULL;//当输入#时,代表这个结点为空 (递归结束,建立空树) 
    
   else//递归创建二叉树 
    
   {
    
     T=new Bnode;
    
     T->data=ch; 	//生成根结点
    
     CreateBiTree(T->lch);//递归创建左子树(先序) 
    
     CreateBiTree(T->rch);//递归创建右子树(先序) 
    
   }
    
 }
    
  
    
  
    
  void preorder (BiTree T)//先序遍历 
    
  {
    

全部评论 (0)

还没有任何评论哟~