简体   繁体   English

C-二进制搜索树-按顺序打印不打印任何内容(请仔细检查我的指针)

[英]C - Binary Search Tree - Print in Order Doesn't Print Anything (Double check my pointers)

Ok, so the problem that I am trying to find is why when I call print_inOrder(), I don't get anything printed back. 好的,所以我要查找的问题是为什么当我调用print_inOrder()时,什么都没打印回来。 The assignment I am suppose to do is write a tree algorithm in descending order (meaning higher values on left and lower values on the right). 我想做的任务是按降序编写树算法(表示左侧的较高值和右侧的较低值)。 I had already created a function that created a tree a while back so I had just modified it and it works as it should; 我已经创建了一个函数,该函数在不久前创建了一棵树,所以我刚刚对其进行了修改,并且它应该可以正常工作。 however the signature for this assignment is different from my old assignment and when I tried changing the pointers around, I got it to compile, but nothing prints out. 但是,此分配的签名与我以前的分配不同,当我尝试更改指针时,可以编译它,但没有任何输出。 So if someone could double check my changes and explain where I went wrong and how I need to fix it, that would make my day! 因此,如果有人可以仔细检查我的更改并解释我出了什么问题以及如何解决它,那将使我度过愉快的一天! ^.^ ^。^

Working Original Function: 工作原始功能:

Tnode add_tnode(Tnode **current_tnode, char *value)
{
    if(!(*current_tnode))
    {
        *current_tnode = (Tnode*) malloc(sizeof(Tnode));
        (*current_tnode)->strValue = value;
         //initialize the children to null
        (*current_tnode)->left = NULL;
        (*current_tnode)->right = NULL;
    }

    //Greater values go to left
    else if(strcmp(value, (*current_tnode)->strValue) >= 0)
    {
      return add_tnode(&(*current_tnode)->left, value);
    }

    //Lesser values go to right
    else if(strcmp(value, (*current_tnode)->strValue) < 0)
    {
      return add_tnode(&(*current_tnode)->right, value);
    }
}

How it's called in main: main中的调用方式:

  Tnode *root;

  root = NULL;

  //Add some nodes with string values
  add_tnode(&root, "pie");
  add_tnode(&root, "hi");
  add_tnode(&root, "hi");
  add_tnode(&root, "l");
  add_tnode(&root, "leg");

  //Print nodes in descending order
  print_inOrder(root);

Signature Required: 要求签名:

Tnode *add_tnode(Tnode *current_tnode, char* value)

My Attempt to Fix: 我的修复尝试:

Tnode *add_tnode(Tnode *current_tnode, char* value)
{
    if(!(current_tnode))
    {
        current_tnode = (Tnode*) malloc(sizeof(Tnode));
        (current_tnode)->strValue = value;
        /* initialize the children to null */
        (current_tnode)->left = NULL;
        (current_tnode)->right = NULL;
    }

    // Greater values go to left
    else if(strcmp(value, (current_tnode)->strValue) >= 0)
    {
      return add_tnode((current_tnode)->left, value);
    }

    // Lesser values go to right
    else if(strcmp(value, (current_tnode)->strValue) < 0)
    {
      return add_tnode((current_tnode)->right, value);
    }
}

How it's called in Main: 在Main中的调用方式:

  Tnode *root;

  root = NULL;

  //Add some nodes with string values
  add_tnode(root, "pie");
  add_tnode(root, "hi");
  add_tnode(root, "hi");
  add_tnode(root, "l");
  add_tnode(root, "leg");

  //Print nodes in descending order
  print_inOrder(root);

Here's print_inOrder() just in case someone wants to look at it 这是print_inOrder(),以防万一有人想看它

void print_inOrder(Tnode *current_tnode)
{
    if (current_tnode)
    {
        print_inOrder(current_tnode->left);
        printf("%s\n",current_tnode->strValue);
        print_inOrder(current_tnode->right);
    }
}

When I run it through the gdb debugger and the print function is called, it only goes through the if statement and ends which my guess it means that the tree wasn't created at all or the pass in value is incorrect. 当我通过gdb调试器运行它并调用print函数时,它仅通过if语句并结束,这是我的猜测,这意味着根本没有创建树或传入的值不正确。 If someone could inform me on what the mistake is, I'd greatly appreciate it! 如果有人可以告诉我错误是什么,我将不胜感激!

Your problem is that your first function takes a Tnode ** , that is a pointer to a pointer, and modifies the TNode * it points to. 您的问题是您的第一个函数采用Tnode ** ,即指向指针的指针,并修改它指向的TNode * Your second function takes just the pointer, and modifies the passed-in argument; 您的第二个函数仅获取指针,并修改传入的参数; the caller can't see those changes, and so nothing is ever added to the tree. 调用者看不到那些更改,因此没有任何内容添加到树中。

You should allocate and assign the root node before doing anything, then change the function so that it modifies the TNode instead of the pointer thereto. 您应该在执行任何操作之前分配并分配root ,然后更改功能,以使其修改TNode而不是指向其的指针。

If your previous assignment worked, all you have to do is change the print function, exploring the right nodes before exploring the left nodes. 如果您之前的工作可行,则只需更改打印功能,先浏览右侧的节点,再浏览左侧的节点。

void print_inOrder(Tnode *current_tnode)
{
    if (current_tnode)
    {
        print_inOrder(current_tnode->right);
        printf("%s\n",current_tnode->strValue);
        print_inOrder(current_tnode->left);
    }
}

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

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