0

对于可能需要此类问题帮助的此问题的未来查看者:我通过结合 2 个函数(InsertNode() 和 InTree())来修复它我不确定这是否是不好的做法,我会回到你们是否真的确实解决了问题,或者它只是掩盖了它,但它似乎正在工作......

我浏览了这个网站(以及其他网站)上的各种答案,并从那些我得到的解决方案中得到了没有帮助的解决方案(尝试过但没有奏效,或者与我的程序没有区别)。插入函数(我已将其隔离并认为这是有问题的代码)在某处有一些错误导致我的程序崩溃。

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;
}

以上是我的初始化函数,以防您需要查看它们。

树的内存在调用任何函数之前在主类中分配。

我通过测试看到的主要问题是,一旦 Root 等于 Node,它就保持为空。

有什么想法可以解决这个问题吗?

4

3 回答 3

0

我会做这样的事情。您的插入函数不区分特殊情况(空树)和一般情况(非空树)。

#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 ;
}
于 2013-04-10T20:18:27.487 回答
0

void InsertNode(int I, TP Tree)为一个新节点分配内存,但是当你调用时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 struct AVLNode* NP) 的 typedef 吗?我不知道你的结构是什么所以我不能说。从技术上讲,AVL 与 B 树的不同之处在于它们是自我平衡的…… http://en.wikipedia.org/wiki/AVL_tree

于 2013-04-10T19:10:27.213 回答
0

在使 等于的InTree函数中,它仅在本地更改内存。RootNode

相反,您可能需要使用指向指针的指针来实现您正在尝试的内容。

于 2013-04-10T19:12:33.467 回答