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

Dec 10th, 2013
HelloWorld
Category:
Programming
Price: $5 USD

Question description

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!

Tutor Answer

(Top Tutor) Daniel C.
(997)
School: Carnegie Mellon University
PREMIUM TUTOR

Studypool has helped 1,244,100 students

8 Reviews


Summary
Quality
Communication
On Time
Value
Five Star Tutor
Dec 10th, 2016
" Outstanding Job!!!! "
kpcutie
Nov 27th, 2016
" Excellent job "
Joemoe
Nov 18th, 2016
" <3 it, thanks for saving me time. "
Hemapathy
Nov 13th, 2016
" all I can say is wow very fast work, great work thanks "
pmallory
Nov 4th, 2016
" Totally impressed with results!! :-) "
kevin12622
Oct 22nd, 2016
" Goes above and beyond expectations ! "
kiln82
Oct 13th, 2016
" awesome work thanks "
likeplum4
Sep 29th, 2016
" Excellent work as usual "
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1823 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors