二叉排序树插入结点算法
答案:2 悬赏:20
解决时间 2021-03-08 04:59
- 提问者网友:乱人心
- 2021-03-07 12:30
二叉链表结构定义
typedef int datatype;
typedef struct nodeT
{datatype data;
struct nodeT *lchild, *rchild;
}BinaryTree;
//二叉排序树插入运算 插入成功返回1 否则返回0
int Insert_BinarySortTree(BinaryTree *T,datatype x){
BinaryTree *p=T,*father=null;
//创建新二叉树结点容纳x
BinaryTree *BT=new BinaryTree;
BT->data=x;
BT->lchild=BT->rchild=null;
//寻找插入位置
while(p){
if(p->data==x) {cout<<"this value is already exit in BinaryTree!"<<endl; return 0;}
father=p;//记忆
p=(p->data<x) ? p->rchild :p->lchild;
}
//T是空树 插入在树根
if(T==null) {T=BT; return 1;}
//T不是空树 插入在非树根
((father->data<x) ? father->rchild :father->lchild) =BT;
return 0'
}
void main(){
BinaryTree *BT=null;
Insert_BinarySortTree(BT,20);
if(BT==null) cout<<"yes";
}
我运行这个程序的输出就是yes 这是为什么?
我在Insert_BinarySortTree里面有if(T==null) {T=BT; return 1;}这句话 那main中的BT怎么可能还是null呢
最佳答案
- 二级知识专家网友:嗷呜我不好爱
- 2021-03-07 13:45
问题应该出在
int Insert_BinarySortTree(BinaryTree *T,datatype x)
你修改为int Insert_BinarySortTree(BinaryTree **T,datatype x) ==》 指针的指针
试试看,否则传值而已,等函数返回 BT 该是NULL还是NULL
int Insert_BinarySortTree(BinaryTree *T,datatype x)
你修改为int Insert_BinarySortTree(BinaryTree **T,datatype x) ==》 指针的指针
试试看,否则传值而已,等函数返回 BT 该是NULL还是NULL
全部回答
- 1楼网友:woshuo
- 2021-03-07 14:13
nclude iostream.h
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯