为了账号安全,请及时绑定邮箱和手机立即绑定

C语言二叉查找树

C语言二叉查找树

一执1929 2017-10-09 14:26:12
C语言二叉查找树问题,在 return searchBST( T->rchild, val, f, p); 处出错,求教出错原理,这里应该涉及了双重指针的问题,我理不清。#include <stdio.h>#include <malloc.h>#define OK 1#define TRUE 1#define ERROR -1#define FALSE 0typedef int ElemType;typedef struct Node{    ElemType data;    struct Node *lchild,*rchild;}NODE,*PNODE;//prompt error info and exit.void errorInfo(char str[]){    printf("%s\n",str);    exit(-1);}//prompt error if there is an error in locating memoryvoid mallocErr(PNODE p){    if(NULL==p)        errorInfo("Error in locating memory.");}//search binary sort treeint searchBST(PNODE T,int val,PNODE *f,PNODE *p){    if(!T)    {        *p = *f;        return FALSE;    }    else if(val == T->data)    {        *p = T;        return TRUE;    }else if(val < T->data)    {        printf("run this code 52\n");        *f = T;        return searchBST(T->lchild,val,f,p);    }else    {        printf("run this code 56\n");        *f = T;        printf("run this code 58\n");        printf("T->data : %d \n",T->data);        //运行到此处出错        return searchBST( T->rchild, val, f, p);    }}int insertBST(PNODE *T,int val){    PNODE p = NULL, s = NULL,f = NULL;    int res = 0;    res = searchBST(*T,val,&f,&p);    if(!res) //val does not exist in the array.    {        s = (PNODE)malloc(sizeof(NODE));        mallocErr(s);        s->data = val;        if(!p) //p is null        {            printf("run this code 75\n");            printf("Create Tree with val:%d\n",val);            *T = s;        }else if(val < p->data)        {            printf("run this code 79\n");            printf("Insert Key To Tree(left):%d\n",val);            p->lchild = s;        }else        {            printf("run this code 84\n");            printf("Insert Key To Tree(right):%d\n",val);            p->rchild = s;        }        return TRUE;    }else   //val already exists in the array.    {        return FALSE;    }}int main(){    PNODE T = NULL, p = NULL;    insertBST(&T,100);    insertBST(&T,199);    return 0;}
查看完整描述

1 回答

?
SapereAudor

TA贡献22条经验 获得超11个赞

建议你直接问老师,然后再自己整理一遍。

查看完整回答
1 反对 回复 2017-10-09
  • 1 回答
  • 0 关注
  • 1791 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信