![](/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.