Question: 1. Rewrite code from C++ to pseudo-code. Very important must be in pseudo-code .. #include using namespace std; typedef struct tree{ int key; struct tree

1. Rewrite code from C++ to pseudo-code. Very important must be in pseudo-code..

#include

using namespace std;

typedef struct tree{

int key;

struct tree *left;

struct tree *right;

}Tree;

struct Mini_max{

int mini;

int maxi;

Mini_max(int mn,int mx)

{

mini = mn;

maxi = mx;

}

};

Tree *root = NULL;

int maximum = -999999;

Tree *add_node(int d)

{

Tree *node = new Tree;

node->left = NULL;

node->right = NULL;

node->key = d;

return node;

}

Mini_max *max_prod(Tree *root)

{

if(root == NULL)

return new Mini_max(0,0);

//cout<<"root :"

Mini_max l = max_prod(root->left);

Mini_max r = max_prod(root->right);

if(root->key == 0)

{

maximum = max(maximum,0);

}

else

{

maximum = max(maximum, root->key);

maximum = max(maximum, root->key * l.mini);

maximum = max(maximum, root->key * l.maxi);

maximum = max(maximum, root->key * r.mini);

maximum = max(maximum, root->key * r.maxi);

maximum = max(maximum, root->key * l.mini * r.mini);

maximum = max(maximum, root->key * l.mini * r.maxi);

maximum = max(maximum, root->key * l.maxi * r.mini);

maximum = max(maximum, root->key * l.maxi * r.maxi);

}

int min1 = 0, max1 = 0;

min1 = min(min1, root->key);

min1 = min(min1, root->key * l.mini);

min1 = min(min1, root->key * l.maxi);

min1 = min(min1, root->key * r.mini);

min1 = min(min1, root->key * r.maxi);

max1 = max(max1, root->key);

max1 = max(max1, root->key * l.mini);

max1 = max(max1, root->key * l.maxi);

max1 = max(max1, root->key * r.mini);

max1 = max(max1, root->key * r.maxi);

return new Mini_max(min1, max1);

}

int main()

{

root = add_node(3);

root->left = add_node(-2);

root->right = add_node(4);

root->left->left = add_node(5);

root->left->right = add_node(0);

root->right->left = add_node(-2);

root->right->right = add_node(2);

root->left->right->left = add_node(1);

max_prod(root);

cout<

return 0;

}

The original question was:

Suppose T is a binary tree that stores an integer key value in each node. Assume the following notation/operations on a binary tree.

the key T.key is the root nodes integer key value

the left child T.lef t is Ts left subtree, which is possibly an empty tree (or null)

the right child T.right is Ts right subtree, which is possibly an empty tree (or null)

(a) Write an efficient algorithm FindMaxProduct(T) in pseudocode that returns the maximum product of the key values on all possible paths in the tree T, which is passed as the input to the function.

For the tree below, your algorithm should return a value of 240 (from the path with key values 5 ?2 3 4 ?2 ).

The above code was given by a chegg expert but did not give the pseudo-code.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!