回 帖 发 新 帖 刷新版面

主题:[原创]二叉树的应用~!

小弟刚学的二叉树.
就把他综合了一下.
请大家指教:

//二叉树的基本操作///////////////////////////
#include <stdio.h>
#include <malloc.h>
//定义二叉树
typedef struct bitnode

    int data;
    struct bitnode *lchild,*rchild;
}bitnode,*bitree;
//创建二叉树利用先序创建
void createbitree(bitree *T)
{
    int dat;
    printf("Please Input data:");
    scanf("%d",&dat);
    if(dat==0) (*T) = NULL;
    else 
    {
        (*T) = (bitree)malloc(sizeof(bitnode));
        (*T)->data=dat;      
        createbitree(&((*T)->lchild));      
        createbitree(&((*T)->rchild));
    }
}
//二叉树的先序周游递归算法
void postorder10(bitree T)
{
    if (T != NULL)
    {
       printf("%d\t",T->data);
       postorder10(T->lchild);
       postorder10(T->rchild); 

     }

}
//二叉树的先序周游非递归算法
void postorder11(bitree t)
{ bitree s[100];
  int top=0;
  while (t!=NULL || top!=0) 
   { 
      while (t!=NULL) 
      { 
           printf("%d\t",t->data); s[++top]=t; t=t->lchild ;
      }
         if (top!=0) 
         { 
            t=s[top--]->rchild;
         }
   }  
 }
//二叉树的中序周游递归算法
void postorder20(bitree T)
{
    if (T != NULL)
    {
      postorder20(T->lchild);
       printf("%d\t",T->data);
       postorder20(T->rchild); 
    }

}

//二叉树的中序周游非递归算法
void postorder21(bitree t)
   {
    bitree s[100];//指针数组(栈),存放按中序访问的节点的地址
     int top=0;
     while (t!=NULL || top!=0)
      {
         while (t!=NULL) 
         { 
             s[++top]=t;   t=t->lchild ;
         }
           if (top!=0)
           {
            t=s[top--];  printf("%d\t",t->data);  t=t->rchild;
           }
      }
   }
//二叉树的后序周游递归算法
void postorder30(bitree T)
{
    if (T != NULL)
    {
       postorder30(T->lchild);
       postorder30(T->rchild);
       printf("%d\t",T->data);
    }

}
//二叉树的后序周游非递归算法
void postorder31(bitree  t) 
   {
    typedef struct node 
       { bitree  t;  int tag;   
       } stack;
      stack s[100] ;
       int top=0;
      while (t!=NULL|| top!=0)
      {while (t!=NULL) 
         {s[++top].t=t;  s[top].tag=0;  t=t->lchild; }
      while (top!=0 && s[top].tag==1) 
          printf("%d\t" ,s[top--].t->data);
      if (top) 
        { s[top].tag=1;  t=s[top].t->rchild ; }
     } 
   }

回复列表 (共13个回复)

11 楼


非常感谢了

12 楼

那我来补充一下,知道前序遍历和中序遍历,创建二叉树的结构吧
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 27

typedef struct node
{
    char data;
    struct node *lchild,*rchild;
}binnode;
typedef  binnode *bintree;
 /* la 前序遍历的起始位置 ,ra前序遍历的末位置 ,lb 前序遍历的起始位置,rb中序遍历的末位置 */
void change(char a[], int la,int ra,char b[],int lb,int rb,bintree *t)
{
   //生成当前根结点。 
    (*t)=(bintree)malloc(sizeof(binnode));
    (*t)->data=a[la];
    (*t)->lchild=NULL;
    (*t)->rchild=NULL;
  // 生成子树。 
  if(la<ra || lb<rb)
  {
   int i=lb;
   
   while(a[la]!=b[i])
     i++;
    //递归生成左右子树。 
     if(i-lb>0) /*存在左子树 */
         change(a,la+1,la+i-lb,b,lb,i-1,&(*t)->lchild);

     if(rb-i>0)/* 存在右子树  */
         change(a,la+i+1-lb,ra,b,i+1,rb,&(*t)->rchild);

  }
}

void preorder(bintree t)
{
    if(t)
    {
        printf("%c",t->data);
        preorder(t->lchild);
        preorder(t->rchild);
    }
}
void inorder(bintree t)
{
    if(t)
    {
        inorder(t->lchild);
        printf("%c",t->data);
        inorder(t->rchild);
    }
}
void postorder(bintree t)
{
    if(t)
    {
        postorder(t->lchild);
        postorder(t->rchild);
        printf("%c",t->data);
    }
}
int main()
{
    bintree t,tt;
    
    char  a[N],b[N];
    
    printf("请输入前序遍历的结果:");
    scanf("%s",a);
    printf("请输入中序遍历的结果:");
    scanf("%s",b);
    change( a,0,strlen(a)-1,b,0,strlen(b)-1,&t);
    printf("前序遍历的结果 :");
    preorder(t);
    printf("\n");
    inorder(t);
    printf("\n");
    postorder(t);
    printf("\n");
    system("pause");
    return 0;
}

13 楼

不能实现啊,有点问题

我来回复

您尚未登录,请登录后再回复。点此登录或注册