Domanda

I am trying to build my own implementation of a Binary Search Tree. I have pretty much everything working, I wanted to write a recursive method that prints the values in the tree in order. I used the following:

void innerPrint(Node * node) {
    if (node->getLeft() != NULL) innerPrint(node->getLeft());
    cout << node->getValue() << " ";
    if (node->getRight() != NULL) innerPrint(node->getRight());
}

This is a private function, then the actual print function just calls this with my RootNode as the initial input. Below is my output. My question is, what causes this percent sign to appear? I've had a similar problem in the past, where everything that needs to be printed is done, but also this % appears at the end.

but I can't figure out why this is sometimes printed to the screen.

The error I have

EDIT:

class BinarySearchTree {
    private:
        // Inner class Node, node contains a comprable value and pointers to other Node
        class Node {
            private:
                int value;
                Node * left = NULL;
                Node * right = NULL;
                Node * parent = NULL;

            public:
                // Getter, setter methods for Node
        };

        Node * root; // This is the top node of the tree

public:
    BinarySearchTree (int value) {
        root = new Node(value);
    }
};
È stato utile?

Soluzione

what @dabhaid said. this is most surely your prompt getting printed on the same line because your output does not end with \n. this is my zsh, with the verbiage in prompt edited out:

crap > printf "hey\n"
hey
crap > printf "hey"  
hey%
crap > 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top