Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

| 36| parent 36 L|7| R |47| parent 7 L|4| R |9| parent 4 L |2| parent 2 R |3| Analyze the execution of the

| 36|

parent 36 L|7| R |47|

parent 7 L|4| R |9|

parent 4 L |2|

parent 2 R |3|

Analyze the execution of the algorithm foo() when it is applied to the above tree with the root node 36 as initial input. What value does foo return? What attribute of the tree does foo measure?

Node class used by algorithm

class TreeNode {

int data;

TreeNode left;

TreeNode right;

}

Method to return the maximum of two ints

public static int max (int a, int b) {

if (a < b) {

return b;

}

else {

return a;

}

}

Mystery algorithm

public static int foo(TreeNode node) {

if (node == null) {

return 0;

}

else {

return 1 + max(foo(node.left), foo(node.right));

}

}

Initial call in main assume above tree and root is pointing to node 36

int answer = foo(root);

----------------------------------------------------------------------------------------------------------------------------------

foo(root) returns ___________

What attribute of the tree does foo() measure? ___________________________________

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Structure Of The Relational Database Model

Authors: Jan Paredaens ,Paul De Bra ,Marc Gyssens ,Dirk Van Gucht

1st Edition

3642699588, 978-3642699580

More Books

Students also viewed these Databases questions

Question

Do you currently have a team agreement?

Answered: 1 week ago

Question

c. How is trust demonstrated?

Answered: 1 week ago

Question

c. Will leaders rotate periodically?

Answered: 1 week ago