ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE. Software > Coding Snacks

Binary Search Tree help

(1/2) > >>

Hello all, let me preface this by saying that YES, this is for a homework assignment. That said, I have already completed 99% of the program and the only thing left is what I am about to ask for help on.

I have a function to display a binary search tree using in-order traversal. The function works fine (as does the rest of the program, but I am having some trouble in limiting the output to 10 numbers per line.

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30

The function looks like such

--- Code: C++ ---void inOrderDisplay(bstNode *nodeToDisplay){        if (nodeToDisplay == NULL) return;        inOrderDisplay(nodeToDisplay->left);        cout << setw(6) << right << nodeToDisplay->integer;        inOrderDisplay(nodeToDisplay->right);   }
Now, what I am trying to achieve, as said above, is 10 numbers per line followed by a newline, then the next 10 numbers, etc.

I have tried various things like having a static int counter inside of the function, I have tried using a standard local variable, I have tried passing in the counter as a parameter (reference). Once inside, I have attempted to use (if counter == 0), if (counter % 10 == 0), or if (counter > 10).

I am really at a loss here and this is the only thing holding up my submission of this program. How do you go about doing something like this inside of a recursive function?

Thanks in advance :)

I'm not sure why passing in a counter by reference didn't work for you.  Maybe you can post that attempt.

If you're OK with me just posting what I believe to be a solution (though it's not tested, so maybe it's no help at all), here's a snippet that I think should work:

--- Code: C++ ---void inOrderDisplay(bstNode *nodeToDisplay)    {        int state = 0;  // will keep track of number of elements printed per line                inOrderDisplay( nodeToDisplay, state);    }      void inOrderDisplay(bstNode *nodeToDisplay, int& state)    {        if (nodeToDisplay == NULL) return;        inOrderDisplay(nodeToDisplay->left, state);        cout << setw(6) << right << nodeToDisplay->integer;                ++state;        if (state >= 10) {            cout << endl;            state = 0;        }                    inOrderDisplay(nodeToDisplay->right, state);        }   

A static variable should work as well, you just have to remember that it is initialized at program start, so you will need some way to reset it between uses.

You could also pass and return an int that contains the total number of integers printed so far.

But this being C++, perhaps a better solution would be to create a little helper class, which wraps an ostream, and then pass it to your function. This way, you have removed the details of how the numbers are printed from the traversal, and you can reuse it if you have to print the tree in some other order as well.

SpoilerDisclaimer: this was written rather quickly, might not work as intended ;D

--- Code: C++ ---class NumberPrinter {        std::ostream &to;        const int lim;        int num; public:        NumberPrinter(std::ostream &to, int lim) : to(to), lim(lim), num(0) { ; }         void reset() { num = 0; }         void print_number(int n)        {                to << std::setw(6) << std::right << n;                 if (++num == lim) {                        to << std::endl;                        num = 0;                }        }         void finish()        {                if (num > 0) {                        to << std::endl;                        num = 0;                }        }}; // for convenienceNumberPrinter &operator<<(NumberPrinter &np, int i){        np.print_number(i);        return np;} // your traversal functionvoid inOrderDisplay(bstNode *nodeToDisplay, NumberPrinter &np){        if (nodeToDisplay == NULL) return;        inOrderDisplay(nodeToDisplay->left, np);        np << nodeToDisplay->integer;        inOrderDisplay(nodeToDisplay->right, np);       } // print 10 numbers per line to coutNumberPrinter np(std::cout, 10);inOrderDisplay(root, np);np.finish();

What mwb suggested is what I would do (except that i would use a better name for the variable!).

mwb!!!!!! That is exactly what I had before with the exception of the fact of where I had the ++counter set at. I had it at the end of the function (made logical sense to me). Now it works beautifully!

Jibz, thanks for the suggestion as well. Unfortunately, we have to use structured code and not OOP (I love OOP and this is driving me nuts). Plus, each function I add I would have to calculate the Big-O for so I want to minimize my number of functions :)

Thanks again everyone! This is working like a champ!


[0] Message Index

[#] Next page

Go to full version