DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Binary Search Tree help  (Read 10,240 times)

Josh

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 3,411
    • Gathering Badge
    • Official Forum Clown
    • Coding Snacks
    • Plugin Coding Medal
    • Volunteers/Staff
    • Military Service Badge - US Army
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Binary Search Tree help
« on: February 13, 2014, 04:35 AM »
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.

I.E,
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
etc.

The function looks like such

Code: (cpp)
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 :)
Strength in Knowledge
« Last Edit: February 13, 2014, 04:43 AM by Josh »

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
Re: Binary Search Tree help
« Reply #1 on: February 13, 2014, 08:24 AM »
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:

Spoiler (click to show/hide)
Code: (cpp)
    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);	
    }
 

Jibz

  • Cold Warrior
  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,187
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Participated in NANY Challenge Four Times
Re: Binary Search Tree help
« Reply #2 on: February 13, 2014, 09:40 AM »
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.

Spoiler (click to show/hide)
Disclaimer: this was written rather quickly, might not work as intended ;D

Code: (cpp)
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 convenience
NumberPrinter &operator<<(NumberPrinter &np, int i)
{
	np.print_number(i);
	return np;
}

// your traversal function
void 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 cout
NumberPrinter np(std::cout, 10);
inOrderDisplay(root, np);
np.finish();
"A problem, properly stated, is a problem on it's way to being solved" -Buckminster Fuller
"Multithreading is just one damn thing after, before, or simultaneous with another" -Andrei Alexandrescu

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Binary Search Tree help
« Reply #3 on: February 13, 2014, 10:30 AM »
What mwb suggested is what I would do (except that i would use a better name for the variable!).

Josh

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 3,411
    • Gathering Badge
    • Official Forum Clown
    • Coding Snacks
    • Plugin Coding Medal
    • Volunteers/Staff
    • Military Service Badge - US Army
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Binary Search Tree help
« Reply #4 on: February 13, 2014, 03:23 PM »
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!
Strength in Knowledge

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
Re: Binary Search Tree help
« Reply #5 on: February 13, 2014, 06:42 PM »
i would use a better name for the variable!

Quote from: http://martinfowler.com/bliki/TwoHardThings.html
There are only two hard things in Computer Science: cache invalidation and naming things.

-- Phil Karlton

Original address: https://www.donationcoder.com/forum/index.php?topic=37233.0