![](/img/trans.png)
[英]void implementation error for Insert function of binary search tree (In C)
[英]Binary Search Tree Insert Function In C
对于这个问题的未来查看者,他们可能需要这类问题的帮助:我通过结合两个函数(InsertNode()和InTree())解决了这个问题,我不确定这是否是不好的做法,我将回到你们是否真的能解决问题,或者只是掩盖了问题,但似乎可行...
我浏览了本网站(以及其他网站)上的各种答案,并从中得到了无济于事的解决方案(尝试过,行不通或与我的程序没有不同)。 insert函数(我已经隔离了它,并认为这是有问题的代码)在某处存在导致我的程序崩溃的错误。
NP InTree(NP Node,NP Root)
{
if (Root == NULL)
{
Root=Node;
return Root;
}
else
{
if (Node->Input < Root->Input)
{
return InTree(Node,Root->Left);
}
else if (Node->Input > Root->Input)
{
return InTree(Node,Root->Right);
}
else
{
puts("Duplicate");
return NULL;
}
}
}
void InsertNode(int I, TP Tree)
{
NP Node;
Node=(NP)malloc(sizeof(struct AVLNode));
InitializeNode(Node);
Node->Input=I;
Node->Height=0;
Node->Left=NULL;
Node->Right=NULL;
InTree(Node,Tree->Root);
Tree->Size++;
}
NP是节点指针,TP是树指针
Node变量是通过InsertNode()发送的初始化节点
void InitializeTree(TP Tree)
{
Tree->Root=NULL;
Tree->Size=0;
}
void InitializeNode(NP Node)
{
Node->Input=0;
Node->Height=0;
}
以上是我的Initialize函数,以防万一您需要查看它们。
在调用任何函数之前,在主类中分配了Tree的内存。
从测试中看到的主要问题是,一旦使Root等于Node,它将保持为空。
有什么想法可以解决这个问题吗?
void InsertNode(int I, TP Tree)
为新节点分配mem,但是当您调用NP InTree(NP Node,NP Root)
您仅修改了本地指针地址。 您需要使用指向指针的指针(即NP InTree(NP Node, NP *ppRoot)
)或以下示例:
if (Node->Input < Root->Input) {
if(Root->Left == NULL) {
Root->Left = Node;
} else {
return InTree(Node,Root->Left);
}
} else if (Node->Input > Root->Input) {
if(Root->Right== NULL) {
Root->Right= Node;
} else {
return InTree(Node,Root->Right);
}
} else {
puts("Duplicate");
return NULL;
}
附言 我注意到您分配了struct AVLNode ... NP是AVLNode的typedef( typedef struct AVLNode* NP
)吗? 我不知道您的结构是什么,所以我不能说。 从技术上讲,AVL与B树的不同之处在于它们具有自我平衡功能。http: //en.wikipedia.org/wiki/AVL_tree
在InTree
函数中,使Root
等于Node
,它仅在本地更改内存。
相反,您可能需要使用一个指向该指针的指针来实现您要尝试的功能。
我会做这样的事情。 您的插入函数不会区分特殊情况(空树)和一般情况(非空树)。
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct NODE
{
struct NODE *left ;
struct NODE *right ;
int value ;
int depth ;
} NODE ;
typedef struct TREE
{
NODE *root ;
int count ;
} TREE ;
NODE *node_create( int value )
{
NODE *instance = (NODE*) calloc( 1 , sizeof(NODE) ) ;
instance->value = value ;
return instance ;
}
TREE *tree_create()
{
TREE *instance = (TREE*) calloc( 1 , sizeof(TREE) ) ;
return instance ;
}
NODE *tree_find_and_insert( NODE *parent , int value )
{
NODE *child = NULL ;
if ( value < parent->value )
{
if ( parent->left == NULL )
{
child = node_create(value) ;
child->depth = ++ parent->depth ;
parent->left = child ;
return child ;
}
else
{
return tree_find_and_insert(parent->left , value ) ;
}
}
else if ( value > parent->value )
{
if ( parent->right == NULL )
{
child = node_create(value) ;
child->depth = ++ parent->depth ;
parent->right = child ;
return child ;
}
else
{
return tree_find_and_insert( parent->right , value ) ;
}
}
else /* ( value == parent->value ) */
{
// NO-OP by design: dupes fall out and NULL is returned
}
return child ;
}
NODE *tree_insert( TREE *tree , int value )
{
NODE *inserted = NULL ;
if ( tree->root == NULL )
{
tree->root = node_create( value ) ;
tree->count = 1 ;
inserted = tree->root ;
}
else
{
inserted = tree_find_and_insert( tree->root , value ) ;
}
return inserted ;
}
int main ( int argc , char *argv[] )
{
TREE *my_tree = tree_create() ;
int i ;
for ( i = 1 ; i < argc ; ++i )
{
char *arg = argv[i] ;
int n = atoi(arg) ;
tree_insert( my_tree , n ) ;
}
return 0 ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.