简体   繁体   English

C中的二进制搜索树插入功能

[英]Binary Search Tree Insert Function In C

For future viewers of this question who might need help in this type of problem: I fixed it by combining the 2 functions (InsertNode() and InTree()) I'm not sure if this is bad practice and I'll get back to you guys with if it actually really does solve the problem or if it just masks it, but it seems to be working... 对于这个问题的未来查看者,他们可能需要这类问题的帮助:我通过结合两个函数(InsertNode()和InTree())解决了这个问题,我不确定这是否是不好的做法,我将回到你们是否真的能解决问题,或者只是掩盖了问题,但似乎可行...

I've looked through various answers on this website (as well as others) and from those I've gotten solutions which didn't help (tried and didn't work or just didn't differ from my program). 我浏览了本网站(以及其他网站)上的各种答案,并从中得到了无济于事的解决方案(尝试过,行不通或与我的程序没有不同)。 The insert function (I've isolated it and think this is the problematic code) has some bug somewhere that causes my program to crash. 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 is a Node Pointer, TP is a Tree Pointer NP是节点指针,TP是树指针

The Node variable is the initialized node sent through InsertNode() Node变量是通过InsertNode()发送的初始化节点

void InitializeTree(TP Tree)
{

    Tree->Root=NULL;
    Tree->Size=0;
}

void InitializeNode(NP Node)
{

    Node->Input=0;
    Node->Height=0;
}

The above are my Initialize functions just in case you need to see them. 以上是我的Initialize函数,以防万一您需要查看它们。

The memory for the Tree is allocated in the main class before any of the functions are called. 在调用任何函数之前,在主类中分配了Tree的内存。

The main problem from what I saw through testing is that once Root is made equal to Node it remains null. 从测试中看到的主要问题是,一旦使Root等于Node,它将保持为空。

Any ideas how I can get past the problem? 有什么想法可以解决这个问题吗?

void InsertNode(int I, TP Tree) allocates mem for a new node, but when you call NP InTree(NP Node,NP Root) you only modify the local pointer address. void InsertNode(int I, TP Tree)为新节点分配mem,但是当您调用NP InTree(NP Node,NP Root)您仅修改了本地指针地址。 You need to either use a pointer to a pointer (ie NP InTree(NP Node, NP *ppRoot) ) or the following example: 您需要使用指向指针的指针(即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;
}

Ps. 附言 I notice you allocate struct AVLNode...is NP a typedef of AVLNode ( typedef struct AVLNode* NP )? 我注意到您分配了struct AVLNode ... NP是AVLNode的typedef( typedef struct AVLNode* NP )吗? I don't know what your structures are so I can't say. 我不知道您的结构是什么,所以我不能说。 Technically AVLs are different from B-trees in that they are self balancing... http://en.wikipedia.org/wiki/AVL_tree 从技术上讲,AVL与B树的不同之处在于它们具有自我平衡功能。http: //en.wikipedia.org/wiki/AVL_tree

In the InTree function, where the Root is made equal to Node , it only changes the memory locally. InTree函数中,使Root等于Node ,它仅在本地更改内存。

Instead, you may need to use a pointer to a pointer to achieve what you're trying. 相反,您可能需要使用一个指向该指针的指针来实现您要尝试的功能。

I'd do it something like this. 我会做这样的事情。 Your insert function doesn't differentiate between the special case (an empty tree) and the general case (a non-empty tree). 您的插入函数不会区分特殊情况(空树)和一般情况(非空树)。

#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM