Print only 20 values from BSTree

closed account (NCRLwA7f)
Hello, I'm wondering how I can only print 20 values from a Binary Search Tree, recursively. I would like to print the 1st 20 values in the tree. I have used variations of the example below but with no luck.

Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  void BST::BST::print_InOrder_private(node* Ptr, int n){
	
	if(Ptr == nullptr && Ptr == root){
		cout <<"Tree is Empty Yo!";
		return;
		}
	if(Ptr == nullptr){
		return;
		}

	print_InOrder_private(Ptr->left, n-1);
	if(n != 0){
		cout << Ptr->key <<" ";
		n--;
		}
	print_InOrder_private(Ptr->right, n-1);
}
Lets say that you call that with n=20.

You will go left with 19.

Whatever happens there, the n does not change. Therefore, you will go right with 19 18 too.

The only nodes that do not print are the ones that are in very deep branches.
In fact, you could rename 'n' into 'depth', for that is what your current code does:
prints all nodes, where depth is <20 (or is it <=20 ?).

What you do need is to update the 'n' before line 12 to deduct the already printed nodes. You could return int from the function.

(With that the outside caller could even figure out how many were actually printed.)
Last edited on
closed account (NCRLwA7f)
the above function still prints all values in the binary tree
Not all, your tree is just too small to show it.


You have to change the function. I did say what you should/could do. Was that unclear?
closed account (NCRLwA7f)

Not all, your tree is just too small to show it.

I tried it with a smaller number like 4 and you are correct, it prints levels not a particular number of nodes, my mistake.

I am a visual learner, so sometimes things may not be very clear to me if I am unfamiliar with the subject. :(

What you do need is to update the 'n' before line 12 to deduct the already printed nodes.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void BST::BST::print_InOrder_private(node* Ptr, int n){
	
	if(Ptr == nullptr && Ptr == root){
		cout <<"Tree is Empty Yo!";
		return;
		}
	if(Ptr == nullptr){
//*** Maybe here, update n?
		return;
		}

	print_InOrder_private(Ptr->left, n-1);
   // *** Update n somewhere around here?
	if(n != 0){
		cout << Ptr->key <<" ";
		n--;
		}
	print_InOrder_private(Ptr->right, n-1);
}


I'm not very sure if that implies updating n right before I get to my If statement on line 12 or just in general.

You could return int from the function.

you mean like return int instead of void on the function?

Also, should I be doing n-1, when calling the function again or should I do something else?

*not trying to be unappreciative but this has stumped me for about 5 days now, and I am also currently working on an Assembly Language program for my class too and I am confused on using stacks to call functions on MASMS, so I'm researching that too. I have not been able to find anything about printing a particular amount of nodes on google or my book and my professor has not responded to my email, that was 2 days ago.
you mean like return int instead of void on the function?

Yes. That is one way to return information from a function. What is the other?
closed account (NCRLwA7f)
keskiverto Thank you for your input.


Anyone one else willing to help, please I want to take this function (if it is possible) and make it print the 1st 20 nodes recursively. I don't want to change the function if I don't have to. I want to leave it as void since it is just a print function. The only way I will change the function is if there is no absolute way to make it work with void.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

void BST::BST::print_InOrder_private(node* Ptr, int n){
	
	if(Ptr == nullptr && Ptr == root){
		cout <<"Tree is Empty Yo!";
		return;
		}
	if(Ptr == nullptr){
		return;
		}

	print_InOrder_private(Ptr->left, n-1);
	if(n != 0){
		cout << Ptr->key <<" ";
		n--;
		}
	print_InOrder_private(Ptr->right, n-1);
}



* I'm not trying to be rude, but I really need help with this and It has been days.
you don't have to write a function out for me, but please at least tell me what I need to do just straightforward not by dropping hints, it just doesn't work with me.

* ways to help:
you can say "on line n, you cannot do that. Try something like this"

or " You need to pass more arguments, in line n."

or "what you are wanting to do, is called xyz and you can find it in xyz location"

or give an example.
The only way I will change the function is if there is no absolute way to make it work
That is one way to return information from a function. What is the other?

Function arguments can be by value or by reference. You do pass the 'n' by value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

void foo( int bar, int & gaz ) {
  --bar;
  --gaz;
}

int feedback( int bar ) {
  --bar;
  return bar;
}

int main() {
  int val = 5;
  int ref = 5;
  foo( val, ref );
  std::cout << "val=" << val << '\n';
  std::cout << "ref=" << ref << "\n\n";

  val = feedback( val );
  std::cout << "val=" << val << '\n';
  return 0;
}

The foo() demostrates by reference in action. The feedback() uses return value.


I want to leave it as void since it is just a print function.

If you want.

Is the print_InOrder_private() a class member? Does it have to be?
It is "just" a print function.

You cannot print const trees. Why not
void BST::BST::print_InOrder_private(const node* Ptr, int & n)


1
2
3
4
5
6
7
	if(Ptr == nullptr && Ptr == root){
		cout <<"Tree is Empty Yo!";
		return;
		}
	if(Ptr == nullptr){
		return;
		}

Repetition?
1
2
3
4
if ( Ptr == nullptr ) {
  if ( Ptr == root) cout <<"Tree is Empty Yo!";
  return;
}


Line 13. Which is more foolproof, n != 0 or 0 < n ?

If 'n' means "this many can still be printed", and you have not printed any before line 12, how many elements can the line 12 print, n or n-1 ?
One way to do it is to have a helper function that defines an int and then passes the address of the int to the main function (print_InOrder_private). In the recursive function you only print the value if *num is > 0, in which case you also decrement it --*num (or (*num)--). You probably also want to bail on the entire function at the start if *num <= 0.
closed account (NCRLwA7f)
n != 0. will work in this function, but I can now see that 0 < n is far more foolproof because you can land on the range of 0 - n and still satisfy the condition, where with the previous way I was having problems with negative numbers causing an infinite loop.

const would prevent any direct or indirect modifications of the data.

print_InOrder_private(node* Ptr) is part of a class, I have a public function called

print_InOrder(){
print_InOrder_private(root);
}
It's a little verbose but I didn't want to have to pass node* root = nullptr; to it every time I called it.

I also had not realized that I could consolidate the two if statements, so thanks for the tip.


this was extremely helpful, this made me realize why my n value was bouncing all over the place.

Was it because n was getting copied multiple times when I was passing by value and passing by reference it was just pointing back to the original value?

Thank you, this also helped me realize something on my assembly language project too.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 

void BST::BST::print_InOrder_private(node* Ptr, int &n){
	
	if(Ptr == nullptr){
              if(Ptr == root)
		     cout <<"Tree is Empty Yo!";
	      return;
          }

	print_InOrder_private(Ptr->left, n);
	if(0 < n){
		cout << Ptr->key <<" ";
		n--;
		}
	print_InOrder_private(Ptr->right, n);
}


so here it is, and it works great.


*Edit << added &n pass by reference
Last edited on
I must be missing something because I have no idea how that function could possibly work. Are you sure you've tested it enough?
closed account (NCRLwA7f)
sorry, forgot to add the &n to pass by referrence
Topic archived. No new replies allowed.