简体   繁体   English

递归搜索并插入二叉树

[英]Search and Inserting in a binary tree recursively

first of all happy new year. 首先新年快乐。 I was trying to fix a piece of code I've been stumped on for hours. 我试图修复一段我已经困扰了几个小时的代码。 (Note: I am not a solid coder.) (注意:我不是一个可靠的编码器。)

What I am trying to do is to write a function "searchInsert", which will take in a binary tree and some integer i. 我想做的是编写一个函数“ searchInsert”,该函数将包含一个二叉树和一些整数i。 It will then try to find the integer i in the tree. 然后它将尝试在树中找到整数i。 If it's not there it is inserted into the tree. 如果不存在,则将其插入树中。

Other information: If we do in fact find the integer in the tree, return a pointer pointing to the node of to it. 其他信息:如果实际上在树中找到了整数,则返回一个指向其节点的指针。 If we do not find it as we said before, insert it in BUT return a pointer pointing to the root of the tree. 如果找不到我们之前说过的方法,请将其插入但返回指向树根的指针。

I also must do this recursively. 我还必须递归执行此操作。

Now I have tested it using an arbitrary tree along with i = 98, as follows: 现在,我已经使用任意树和i = 98对其进行了测试,如下所示:

Before what it looks like. 在它看起来像什么之前。

     4
    / \
   2   6
  / \ / \
 1  3 5  7

After, what it should look like: 之后,它应该是什么样的:

     4
    / \
   2   6
  / \ / \
 1  3 5  7
          \
          98

But my code doesn't seem to be working. 但是我的代码似乎不起作用。

treelink searchInsert(treelink t, TreeItem i){

    treelink keyNode = NULL;
    if (t == NULL) {
        t = insertTreeNode(t, i);  
    } else if(i < t->item){
        keyNode = searchInsert(t->left,i); 
    } else if(i > t->item){
        keyNode = searchInsert(t->right,i);   
    } else {
        keyNode = t;
        return keyNode;
    }    

    return t;
 }

Other important notes: treelink is a pointer to a binary tree. 其他重要说明:treelink是指向二叉树的指针。 Assume insertTreeNode works as it was a function given to us. 假设insertTreeNode可以正常工作,因为它是给我们的一个函数。

Any help would be appreciated thanks. 任何帮助,将不胜感激谢谢。

Among other problems, you have lost all context when you realize that you have not found the item that you are looking for: 除其他问题外,当您意识到找不到所需的项目时,您已经失去了所有上下文:

if ( t == NULL ) {
  t = insertTreeNode(t, i) ;
}

So you are always calling insertTreeNode with NULL as the first argument. 因此,您始终以NULL作为第一个参数调用insertTreeNode

While recursion is a great way to step through a tree, you might instead want to create a pointer and iterate through the tree, so that you have the original t around when you decide to call insert. 尽管递归是遍历树的好方法,但您可能想创建一个指针并遍历树,以便在决定调用insert时具有原始的t

{
  treelink ptr= t ;
  while ( ptr )
  {
    if ( ptr-> item == i ) return ptr ;
    ptr= ( ptr-> item > i ) ? ptr-> left : ptr-> right ;
  }

  return insertTreeNode( t, i ) ;
}

A new node is created, but it is not linked to the tree. 创建了一个新节点,但未将其链接到树。 You never change your left and right pointers. 你永远不改变你的leftright指针。

what you need is to update the link after recursive call, eg: 您需要在递归调用后更新链接,例如:

else if (i < t->item) {
    t->left = searchInsert(t->left, i);
} ...

But then of course you cannot simply return a pointer to a found item if it's found, otherwise it would break the tree. 但是,当然,您不能简单地将指针返回到找到的项目(如果找到),否则会破坏树。 That's because the statement of your task is NOT recursive: you have to return either root or an existing (inner) node. 这是因为任务的语句不是递归的:您必须返回root或现有(内部)节点。 So you might want to write a recursive function which eg always returns a pointer to the root, but also returns a pointer to a found item (through an additional treelink* argument). 因此,您可能想编写一个递归函数,例如,它总是返回指向根的指针,但也返回指向找到的项的指针(通过附加的treelink*参数)。

Or maybe it would be simpler to split the function into two: search which returns a pointer to an existing node, and insert which returns a pointer to the root. 或者将函数分为两个部分可能更简单: search将返回指向现有节点的指针, insert将返回指向根节点的指针。 Both of them would be recursive and quite simple. 它们都将是递归的并且非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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