| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 688 人关注过本帖
标题:二叉排序树究竟是什么??
只看楼主 加入收藏
yxm870915
Rank: 1
等 级:新手上路
帖 子:18
专家分:2
注 册:2011-2-10
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
二叉排序树究竟是什么??
本人处于C学习阶段,最近在学习二叉查找树,一直弄不懂这个是不是需要基于已有的树....越看越迷茫,索性谢了一段求修改或者给点相关材料研究一下也行
#include <stdio.h>
#include <stdlib.h>

typedef struct BSTNode
{
    int elem;
    struct BSTNode *Lchild;
    struct BSTNode *Rchild;
}Node;


void BST_Insert(Node *T,int x)
{
    T=(Node*)malloc(sizeof(Node));
    if(T==NULL)
    {
        T->elem=x;
        T->Lchild=NULL;
        T->Rchild=NULL;
    }
    else if(T!=NULL&&T->elem>=x)
        BST_Insert(T->Lchild,x);
    else if(T!=NULL&&T->elem<x)
        BST_Insert(T->Rchild,x);
    else
        printf("ERROR!!!\n");
}
void BST_Create(Node *T,int array[],int size)
{
    int i;
    for(i=0;i<size;i++)
    {
        BST_Insert(T,array[i]);
        printf("%d;",T->elem);
    }
}
void main()
{
    int i;
    int size=5;
    int array[5]={1,2,3,4,5};
    Node *T;
   
    T=(Node*)malloc(sizeof(Node));

    T=NULL;

    //printf("input the size of the array:");
    //scanf("%d",&size);
    for(i=0;i<size;i++)
    {
        printf("Input the No. %d elem:",i);
        scanf("%d",&array[i]);
    }
    BST_Create(T,array,size);
    free(T);
}
先谢谢了~
搜索更多相关主题的帖子: include 
2011-02-10 15:14
yxm870915
Rank: 1
等 级:新手上路
帖 子:18
专家分:2
注 册:2011-2-10
收藏
得分:0 
补充一下本人只会C语言,所以希望用C语法挨批
2011-02-10 15:17
『点点滴滴』
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:168
专家分:1035
注 册:2007-7-9
收藏
得分:20 
#include <stdio.h>
#include <stdlib.h>

typedef struct BSTNode
{
    int elem;
    struct BSTNode *Lchild;
    struct BSTNode *Rchild;
}Node;


void BST_Insert(Node *T,int x)
{
    //  T=(Node*)malloc(sizeof(Node));    先判断根是不是有值,-1表示没
    if(T->elem==-1)
    {
        T->elem=x;
        return ;
    }
    /*  else if(T!=NULL&&T->elem>=x)
    BST_Insert(T->Lchild,x);
    else if(T!=NULL&&T->elem<x)
    BST_Insert(T->Rchild,x);
    else
    printf("ERROR!!!\n");   */
    Node *p=T;
    if(p->elem>=x)      //  如果x比当前根的值小,则插入左子树
    {
        if(p->Lchild==NULL)    //如果左子树为空,直接插入
        {
            Node *q=(Node *)malloc(sizeof(Node));
            if(!q)
            {
                printf("creat node error!");
                exit(0);
            }
            q->elem=x;
            q->Lchild=NULL;
            q->Rchild=NULL;
            p->Lchild=q;
        }
        
        else         //否则,当前根的左孩子当做新的根节点,插入
        {
            BST_Insert(p->Lchild,x);
        }
    }
    else       //右子树同理
    {
        if(p->Rchild==NULL)
        {
            Node *q=(Node *)malloc(sizeof(Node));
            if(!q)
            {
                printf("creat node error!");
                exit(0);
            }
            q->elem=x;
            q->Lchild=NULL;
            q->Rchild=NULL;
            p->Rchild=q;
        }
        
        else
        {
            BST_Insert(p->Rchild,x);
        }
    }
}
void BST_Create(Node *T,int array[],int size)   
{
    int i;
    for(i=0;i<size;i++)
    {
        BST_Insert(T,array[i]);
        //    printf("%d;",T->elem);
    }
}
void print(Node *T)  //中序遍历验证创建的树是否正确
{
    if(!T)
        return ;
    print(T->Lchild);
    printf("%d ",T->elem);
    print(T->Rchild);
}
int  main()
{
    int i;
    int size=5;
    int array[5]={1,2,3,4,5};
    Node *T;
   
    T=(Node*)malloc(sizeof(Node));
   
    // T=NULL;
   
    T->elem=-1;   //如果为-1表示根没有赋值
    T->Lchild=NULL;
    T->Rchild=NULL;   //初始化根节点
    //printf("input the size of the array:");
    //scanf("%d",&size);
    for(i=0;i<size;i++)
    {
        printf("Input the No. %d elem:",i);
        scanf("%d",&array[i]);
    }
    BST_Create(T,array,size);
    print(T);   //中序遍历
    free(T);
    return 0;
}

注意什么时候该申请内存!
2011-02-10 16:05
jefffyang
Rank: 1
等 级:新手上路
帖 子:10
专家分:7
注 册:2011-2-10
收藏
得分:0 
3楼的程序调试会出错啊
2011-02-10 16:24
zjsxwc
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:252
专家分:601
注 册:2011-1-20
收藏
得分:0 
找本数据结构的书看吧

The tools I recommended:
GUI: CSharp(VS), QT;    Core Code: Plain C (Tiny C Compiler);    Web: Python, JavaScript;    Android: Java;    Embedded System: ASM&C (Linux)
2011-02-10 16:47
yxm870915
Rank: 1
等 级:新手上路
帖 子:18
专家分:2
注 册:2011-2-10
收藏
得分:0 
回复 3楼 『点点滴滴』
呵呵,谢谢了,但是那个中间变量不需要free吗,还有就是这个能如果要进行双向查询能用链表实现吗?
2011-02-10 20:50
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:0 
编码需要的吧

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2011-02-10 20:52
CCFzeroOH
Rank: 2
等 级:论坛游民
帖 子:79
专家分:85
注 册:2009-12-22
收藏
得分:0 
回复 6楼 yxm870915
那个变量是说p变量吗?
p变量没开辟新空间,并且p=T,不能free。

这个是树形结构,而双向链表是线性结构,不能用双向链表遍历
2011-02-11 16:18
快速回复:二叉排序树究竟是什么??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018262 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved