简体   繁体   中英

Binary Search Tree Printing

I am working on a Binary Tree program for school and I have everything working perfectly. 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,

Needs to be: 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. 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! 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

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. When you're about to print something, check the flag. If false, set flag to true and print your element. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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