• Home
  • Blog
  • Binary Search Tree method question(s) C++!

Binary Search Tree method question(s) C++!

0 comments

These are not very difficult questions and i’ve already done most of the work I just want to know if my answers are correct or if you can fix them if they are not so please do not ask for more money..

I’m just trying to get a good grade here! Please help!

If anyone would be willing to help for free that would be greatly appreciated on my student budget! Hah 🙂

but if not it’s okay I will still pay!

So first I had to write a method for the BinarySearchTree class named sum that takes no parameters. The method should return the total of
all the values in the tree and think recursively. 

My answer:

int sum(node)
	if (node == NULL)

	{
		return 0;

	}

	else

		return node-->value + sum (node-->left) + sum (node-->right);

My next task was to write a method for the
BinarySearchTree class named
height
that takes no parameters. The method should return the height of the tree and think recursively. 


My answer:
int BinarySearchTree::height (tree_node* count) 
{	

	int l = 0;
	int r = 0;

if (count == NULL)	
{		
	return 0;	
}  
{	
	l = height(count-->left);
	r = height(count-->right);

if (l > r ||l == r)
	{	 
		return ( l +l );
	}
else
	 {
		return ( r + l );
	}
}

And my last question was to write a method for the BinarySearchTree class named penultimate that takes no parameters. The function should return
the value of the second-largest item in the tree.

My answer:
node* max = maximum(root);

if(max --> left){
	if (max-->parent-->date > max-->left-->date)
		cout << max-->parent-->data;
	else
		cout << max-->left-->data;
}
	else
		cout << max-->parent-->data;



ANY SUGGESTIONS WOULD BE GREATLY APPRECIATED :) 
THANKS!

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}