简体   繁体   English

二进制搜索树打印

[英]Binary Search Tree Printing

I am working on a Binary Tree program for school and I have everything working perfectly. 我正在为学校编写Binary Tree程序,并且一切正常。 All I am working on now is the proper output. 我现在要做的就是正确的输出。 My teacher wants the output to be all the numbers in sorted order with commas after them. 我的老师希望输出的结果是所有数字,其后跟逗号。

My code that I have sorts the numbers perfectly and prints them, I am just not sure how to remove the comma after the last number. 我的代码完美地将数字排序并打印出来,但我不确定如何删除最后一个数字后的逗号。

Current Output: 1, 2, 3, 4, 电流输出:1,2,3,4,

Needs to be: 1, 2, 3, 4 需要为:1、2、3、4

Here is my code: 这是我的代码:

void BinaryTree::inorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) 
            inorder(p->left);

        cout << p->data << ", ";

        if(p->right)
            inorder(p->right);
    }
    else
        return;
}

I have tried a few things to make it right but I just can't figure it out. 我已经尝试了一些方法来使其正确,但是我无法弄清楚。

Any help would be great. 任何帮助都会很棒。

Thanks. 谢谢。

One easy way is to print the separator character before the data, like this 一种简单的方法是像这样在数据之前打印分隔符

cout << ", " << p->data;

This way we have changed your problem into skip printing the first comma. 这样,我们将您的问题更改为跳过打印第一个逗号。 This is much easier. 这要容易得多。 Hint: In order to keep track of whether to skip the comma you might need to introduce another argument to the function, since it is a recursive function. 提示:为了跟踪是否跳过逗号,您可能需要在函数中引入另一个参数,因为它是递归函数。

As xmoex points out there is a more elegant way to print this tree resulting in very readable and logic code. 正如xmoex所指出的那样,有一种更优雅的方式来打印此树,从而产生非常易读和逻辑的代码。 Try to find this way for an extra challenge. 尝试找到这种方式来应对其他挑战。

An unrelated hint: You can drop the return statement since its redundant - The function will return anyway! 一个不相关的提示:您可以删除return语句,因为它是多余的-该函数仍然会返回! Like this: 像这样:

void BinaryTree::inorder(tree_node* p)
{
  if (p != NULL)
  {
    // stuff goes inside here!
  }
  // no return here - the function will return anyway
}

This will yield less uneccessary code and will help you read your own code if you need to, for example, debug it quickly before a assignment due date. 这样可以减少不必要的代码,并在需要(例如)在作业到期日之前快速对其进行调试时,可以帮助您阅读自己的代码。

maybe you need another perspective on the problem. 也许您需要对问题有另一种看法。 think of a node as 认为一个节点是
<left subtree> p->data <right subtree>

At the moment your nodes are printed like 目前,您的节点打印像
<left subtree> p->data ", " <right subtree> wich leads to a trailing ", " everytime <left subtree> p->data ", " <right subtree>每次都会导致尾随", "

But you don't want to print ", " on every element. 但是您不想在每个元素上都打印", "
-> You just want to print ", " (at the right place) when ( and only when! ) you decend into a subtree, as otherwise there's no need for a seperator... ->您只想在下降到子树时( 且仅当! )时在正确的位置打印", " (在正确的位置),否则不需要分隔符...

There's a very simple, very elegant way of achieving this without any additional data needed to take along... Feel free to ask if you need further help... 有一个非常简单,非常优雅的方法来实现此目标,而无需携带任何其他数据……随时询问您是否需要进一步的帮助……

Update: as i think your homework is over by now i want to display my solution: 更新:由于我认为您的作业已经结束,所以我想展示我的解决方案:

void BinaryTree::inorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) 
        {
            inorder(p->left);
            cout << ", "; // print ", " everytime after you descended to the left
        }

        cout << p->data;

        if(p->right)
        {
            cout << ", "; // print ", " everytime before you descend to the right
            inorder(p->right);
        }
   }
}

it should look like 它应该看起来像
<left subtree ", "> p->data <", " rightsubtree>

I'd say you setup a boolean flag that you pass by reference to your printing routine (and the printing routine passes the flag around as it recurses). 我想说您设置了一个布尔标志,该标志通过引用您的打印例程传递(并且打印例程在递归时传递该标志)。

Initially the flag is set to false. 最初,该标志设置为false。 When you're about to print something, check the flag. 当您要打印某些东西时,请检查标志。 If false, set flag to true and print your element. 如果为false,则将flag设置为true并打印您的元素。 If it was already true, print a comma, a space, and your element. 如果已经存在,请打印一个逗号,一个空格和您的元素。

You can introduce a max() method which returns the pointer to the right-most node, then just compare if the pointer to your current node is equal to that of the maximum node. 您可以引入max()方法,该方法将指针返回最右边的节点,然后仅比较当前节点的指针是否等于最大节点的指针。

tree_node* BinaryTree::max(tree_node *p) {
    if(p != NULL && p->right != NULL) return max(p->right);
    return p;
 }

You would need a wrapper method that calls inorder() and passes the maximum, as you don't want to compute the max in each recursive call. 您将需要一个包装器方法来调用inorder()并传递最大值,因为您不想在每次递归调用中都计算最大值。

void BinaryTree::print() {
    inorder(root, max(root));
}

void BinaryTree::inorder(tree_node *p, tree_node *max) {
    if(p == NULL) return;        

    if(p->left) inorder(p->left, max);

    cout << p->data;
    if(p != max) cout << ", ";

    if(p->right) inorder(p->right, max);
}

Change 更改

cout << p->data << ", ";
if(p->right)
{
    inorder(p->right);
}

into 进入

cout << p->data;
if(p->right)
{
    cout << ", ";
    inorder(p->right);
}

ie, only print the comma if you're sure there're something in your right child. 即,只有在确定正确的孩子中有东西的情况下,才打印逗号。

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

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