| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 548 人关注过本帖
标题:二叉排序树的问题
收藏  订阅  推荐  打印
wangyinshiwo
Rank: 2
等级:注册会员
帖子:72
积分:1142
注册:2007-11-9
二叉排序树的问题

/* Note:Your choice is C IDE */
#include "stdio.h"
#define MAX 100
#include<malloc.h>
typedef int keytype;
typedef struct node
{
    keytype key;
    char data;
    struct node *lchild,*rchild;
}BSTNode;
void displayBST(BSTNode *root);
int insertBST(BSTNode *p,keytype k)
{
    if(!p)
    {
        p=(BSTNode *)malloc(sizeof(BSTNode));
        p->key=k;
        p->lchild=p->rchild=NULL;
        return 1;
    }
    else if(k==p->key)
    return 0;
    else if(k<p->key)
    return insertBST(p->lchild,k);
    else
    return insertBST(p->rchild,k);
}
BSTNode *creatBST(keytype a[],int n)
{
    BSTNode *root=NULL;
    int i=0;
    while(i<n)
    {
        if(insertBST(root,a[i])==1)
        {
            printf("第%d步,插入%d:",i+1,a[i]);
            displayBST(root);printf("\n");
            i++;
        }
    }
    return root;
}
void displayBST(BSTNode *root)
{
    if(root)
    {
        printf("%3d",root->key);
        if(root->lchild||root->rchild)
        {
            
            printf("(");
            displayBST(root->lchild);
            if(root->rchild)
            printf(",");
            displayBST(root->rchild);
            printf(")");
        }
    }
}
void main()
{
    BSTNode *root;
    keytype k=6;
    int a[]={4,9,0,1,8,6,3,5,2,7},n=10;
    printf("创建一颗BST树:\n");
    root=creatBST(a,n);
    printf("BST:");displayBST(root);printf("\n");
   
}
为什么程序运行的时候没有调用输出函数啊!奇怪啊!
2008-7-27 20:48
geninsf009
Rank: 3Rank: 3
等级:中级会员
威望:1
帖子:219
积分:2704
注册:2008-8-16

你的main()里不是调用了吗?
displayBST(root);这个函数不就是以广义表显示二叉排序树吗?
2008-8-22 21:17
cillin
Rank: 2
等级:注册会员
帖子:40
积分:508
注册:2005-3-10

不是没执行输出模块,而是你的整个树结构都丢了从你建立第一个结点那刻起。其他地方没细看……不好意思呵呵

2008-8-30 08:55
共有 547 人关注过本帖
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.051310 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved