繁体   English   中英

二叉搜索树代码无法正常工作

[英]binary search tree code not working

好吧,我正在使用这个代码创建一个二叉搜索树..就像我看到使用这个代码我发现它是正确的但一些如何创建错误我不知道为什么

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

typedef struct node
{  
    struct node *left;  
    int ele;  
    struct node *right;
}*NODE;

void ins(int x,NODE root )
{
    NODE temp;
    //printf("%d    %d*****%d---%d\n",root->ele,root,(root->left),(root->right));   if    i uncomment this line and then (when this function takes first root as its argument )do trace over using turbo C++ compiler its giving me root->left value not null how ever it just executes the next if statement..this should have happened
    if(x<(root->ele) && (root->left)==NULL)
    {
        temp=(NODE)malloc(sizeof(NODE));
        temp->ele=x;
        temp->left=NULL;
        temp->right=NULL;
        root->left=temp;
    }
    else if(x<root->ele && (root->left)!=NULL)
    {
        ins(x,root->left);
    }

    else if(x>(root->ele) && (root->right)==NULL)
    {
        temp=(NODE)malloc(sizeof(NODE));
        temp->ele=x;
        temp->left=NULL;
        temp->right=NULL;
        root->right=temp;
    }
    else if(x>(root->ele) && (root->right)!=NULL)
    {
        //printf("%d***%d***%d",root->ele,root->right->ele) ;
        ins(x,root->right);
    }
    //printf("%d",x);
}

void intrav(NODE root)
{
    if(root->left!=NULL)
        intrav(root->left);

    printf("%d",root->ele);

    if(root->right!=NULL)
        intrav(root->right);
}


void main()
{
    int no,i,elem[50];
    NODE root=NULL;
    printf("enter the no of elements you want to enter\n");
    scanf("%d",&no);
    for(i=0;i<no;i++)
        scanf("%d",&elem[i]);

    root=(NODE)malloc(sizeof(NODE));
    root->ele=elem[0];
    root->left=NULL;
    root->right=NULL;
    //printf("%d---",root);
    for(i=1;i<no;i++)
        ins(elem[i],root);
    //               printf("%d",root->left);

    intrav(root);
    getch();
}

使用for循环时,每次每次获取一个数组值并将其发送到函数时执行ins函数...然后如果要添加的值

问题在于两个malloc ,但我认为这是一个功课,所以我不会告诉你究竟是什么。

提示:检查NODE类型!

#include <stdio.h>
#include <stdlib.h>

struct node {
    struct node *left;
    struct node *right;
    int ele;
    };

void intrav(struct node * ptr);
void ins(int xxx,struct node **hnd );

void ins(int xxx,struct node **hnd )
{
    while (*hnd) {
       hnd = xxx < (*hnd)->ele ? &(*hnd)->left : &(*hnd)->right;
       }

    *hnd = malloc(sizeof **hnd);
    (*hnd)->left = NULL;
    (*hnd)->right = NULL;
    (*hnd)->ele = xxx;
}

void intrav(struct node * ptr)
{
    if (!ptr) return;

    intrav(ptr->left);
    printf("%d\n", ptr->ele);
    intrav(ptr->right);
}


int main()
{
    int no,i,elem[50];
    struct node *root=NULL;

    printf("enter the no of elements you want to enter\n");
    scanf("%d",&no);
    for(i=0;i<no;i++)
        scanf("%d", &elem[i]);

    //printf("%d---",root);
    for(i=0;i<no;i++)
        ins(elem[i], &root);
    //               printf("%d",root->left);

    intrav(root);
    getc(stdin);
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM