Change these methods below into recursive ones. protected BinaryNode findMin( Bi
ID: 3689639 • Letter: C
Question
Change these methods below into recursive ones.
protected BinaryNode findMin( BinaryNode t )
{
if( t != null )
while( t.left != null )
t = t.left;
return t;
}
private BinaryNode findMax( BinaryNode t )
{
if( t != null )
while( t.right != null )
t = t.right;
return t;
}
private BinaryNode find( AnyType x, BinaryNode t )
{
while( t != null )
{
if( x.compareTo( t.element ) < 0 )
t = t.left;
else if( x.compareTo( t.element ) > 0 )
t = t.right;
else
return t; // Match
}
return null; // Not found
}
Explanation / Answer
protected BinaryNode findMin( BinaryNode t ) { if( t != null ) return findMin(t.left); else return t; } private BinaryNode findMax( BinaryNode t ) { if( t != null ) return findMax(t.right); else return t; } private BinaryNode find( AnyType x, BinaryNode t ) { if( t != null ) { if( x.compareTo( t.element ) < 0 ) find(x,t.left); else if( x.compareTo( t.element ) > 0 ) find(x,t.right); else return t; // Match } return null; // Not found }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.