[问题] 二元搜索树加资料的问题

楼主: Chieng9086 (chieng9086)   2016-05-25 18:17:57
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
当我输入的整数资料,若下一笔资料小于前面某几笔,会出现存取违规
喂入的资料(Input):
EX:15 20 10(10这笔就会造成存取违规)
预期的正确结果(Expected Output):
15 20 10 可以很任意打入大小顺序随意的数
错误结果(Wrong Output):
掷回例外状况: 读取存取违规。
current 为 nullptr。
程式码(Code):(请善用置底文网页, 记得排版)
// 二元数树结构
struct B_tree {
int data;
struct B_tree *Llink;
struct B_tree *Rlink;
};
typedef struct B_tree node;
node *root; //树根
// 将资料加入二元搜索树
node *tree_Insert(node *root, int value) {
node *new_node;
node *current;
node *parent;
new_node = (node *)malloc(sizeof(node));
new_node->data = value;
new_node->Llink = NULL;
new_node->Rlink = NULL;
if (root == NULL)
{
return new_node;
}
else
{
current = root;
while (current != NULL)
{
parent = current;
if (current->data > value)
current = current->Llink;
else
current = current->Rlink;
}
if (parent->data > value)
parent->Llink = new_node;
else
parent->Rlink = new_node;
}
return root;
}
补充说明(Supplement):
出问题时,Visual Studio下面这行都会有箭头
current = current->Llink;
作者: opl164 (opl)   2016-05-26 09:33:00
你的左右子叶都是NULL?
作者: KJFC (磁铁猫)   2016-05-26 10:15:00
学用递回加点吧 好用不容易出错
作者: leo850319 (不要说话)   2016-05-26 12:51:00
你的current会一直指到null还没加下一个点 你就往下指了

Links booklink

Contact Us: admin [ a t ] ucptt.com