Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2338095
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:07:56

 #include
#include

struct tree
{
    struct tree *lchild;
    int data;
    struct tree *rchild;
};
typedef struct tree treenode;
typedef treenode *bintree;
bintree insert_node(bintree root,int node)
{
    bintree newnode;
    bintree currentnode;
    bintree parentnode;
    newnode=(bintree)malloc(sizeof(treenode));
    newnode->data=node;
    newnode->rchild=NULL;
    newnode->lchild=NULL;
    if(root==NULL)
        return newnode;
    else
    {
        currentnode=root;
        while(currentnode!=NULL)
        {
            parentnode=currentnode;
            if(currentnode->data>node)
                currentnode=currentnode->lchild;
            else
                currentnode=currentnode->rchild;
        }
        if(parentnode->data>node)
            parentnode->lchild=newnode;
        else
            parentnode->rchild=newnode;
    }
    return root;
}
bintree create_tree(int data[],int len)
{
    bintree root;
    int i;
    root=NULL;
    for(i=0;i        root=insert_node(root,data[i]);
    return root;
}
void preorder(bintree point)
{
    if(point!=NULL)
    {
        printf("%d",point->data);
        preorder(point->lchild);
        preorder(point->rchild);
    }
}
 void inorder(bintree point)
{
       if(point!=NULL)
      {
         inorder(point->lchild);
         printf("%d",point->data);
         inorder(point->rchild);
       }
}
void postorder(bintree point)
{
       if(point!=NULL)
      {
         postorder(point->lchild);
         postorder(point->rchild);
         printf("%d",point->data);
       }
}
void main()
{
    bintree root=NULL;
    int i,index;
    int value;
    int x;
    int nodelist[20];
    clrscr();
    printf("please input the elements of binary tree(Exit for 0):\n\n");
    index=0;
    scanf("%d",&value);
    printf("qing xuan ze xiang yong de bian li fa :\n");
    printf("0 dai biao xianxu bianli ,\n");
    printf("1 dai biao zhong xu bian li ,2 dai biao houbian li:\n ");
    scanf("%d",&x);
    printf("\n");
    while(value!=0)
    {
        nodelist[index]=value;
        index++;
        scanf("%d",&value);
    }

    switch (x)
    {
      case 0:
          root=create_tree(nodelist,index);
           printf("th preorder reaversal result is:[");
            inorder(root);
           printf("]\n\n");
       printf("\n\n");
         printf("\n");
          printf("\n");
         getch();
       case 1:
            root=create_tree(nodelist,index);
           printf("th preorder reaversal result is:[");
            preorder(root);
           printf("]\n\n");
       printf("\n\n");
         printf("\n");
          printf("\n");
          getch();
       case 2:
            root=create_tree(nodelist,index);
           printf("th preorder reaversal result is:[");
            postorder(root);
           printf("]\n\n");
       printf("\n\n");
         printf("\n");
          printf("\n");
          getch();
} ;
getch();

}
为什么在遍历小树的时候不能正确的调用顺序函数,
帮帮忙吧,各位大哥大姐,

--------------------next---------------------

阅读(1167) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~