1)
Given a binary tree, find the maximum path product. The path may start and end at any node in the tree.
Here I have taken simple Example:
Input: Root of below tree 1 / \ 2 3 Output: 6 (1*2*3) See below diagram for Given example.
For each node there can be four ways
that the max path goes through the node:
1. Node only
2. Max path through Left Child * Node
3. Max path through Right Child * Node
4. Max path through Left Child * Node * Max path through Right
Child
The idea is to keep trace of four paths and pick up the max one in the end. An important thing to note is, root of every subtree need to return maximum path product such that at most one child of root is involved. This is needed for parent function call. In below code, this product is stored in ‘max_single’ and returned by the recursive function.
CODE:- (I provided you the code with comments for better understanding. Please follow below code..)
// This function returns overall maximum path product in 'res'
// And returns max path product going through root.
int findMaxUtil(Node* root, int &res)
{
//Base Case
if (root == NULL)
return 0;
// l and r store maximum path product going through left and
// right child of root respectively
int l = findMaxUtil(root->left,res);
int r = findMaxUtil(root->right,res);
// Max path for parent call of root. This path must
// include at-most one child of root
int max_single = max(max(l, r) * root->data, root->data);
// Max Top represents the product when the Node under
// consideration is the root of the maxproduct path and no
// ancestors of root are there in max product path
int max_top = max(max_single, l * r * root->data);
res = max(res, max_top); // Store the Maximum Result.
return max_single;
}
// Returns maximum path product in tree with given root
int findMaxProduct(Node *root)
{
// Initialize result
int res = INT_MIN;
// Compute and return result
findMaxUtil(root, res);
return res;
}
(b)
After I tracing the output using binary tree shown above, it becomes like
Output:
Max path product is 240.
Here I have taken one more tree let's see the output
Output:
Max path product is 4000.
3. [5 marks] Suppose T is a binary tree that stores an integer key value in...
just need to answer the second question 3 AVL Trees Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes: . The key T.key is the root node's key. The left child T.left is T's left subtree, which is an AVL tree (possibly E). The right child T.right is T's right subtree, which is an AVL tree (possibly E) [3 marks] Describe an alternative version of the RANGECOUNT(T,...
Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes The key T.key is the root node's key. The left child T.left is Ts left subtree, which is an AVL tree (possibly E). The right child T.right is T's right subtree, which is an AVL tree (possibly E). (a) 5 marsl Write a function RANGECOUNT(T, lo, hi) to count the number of nodes in an AVL tree with...
Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes: • The key T.key is the root node’s key. • The left child T.left is T’s left subtree, which is an AVL tree (possibly E). • The right child T.right is T’s right subtree, which is an AVL tree (possibly E). (a) [5 marks] Write a function RangeCount(T, lo, hi) to count the number of nodes in an...
Hello. I have written the following function to remove a value from a Binary Search Tree using resources my professors gave and stuff I found online. The problem is I don't know how it works and I need to know how it works to finish the rest of the project. public boolean remove(T value){ if(value == null) return false; class RemoveBSTObj{ private boolean found = false; private Node removeBST(Node root, T value){ if(root == null) return root; //IF there is...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
Draw the tree resulting from inserting the following values into a binary search tree in order without re-balancing: 40, 10, 60, 30, 20, 90, 70, 50 Null pointers can be omitted as long as it is clear whether a single child is a left or right child. THEN For every node in the tree, the values that can be in the subtree rooted at that node are constrained by ancestors to be in some range of integers. The root (the...
I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list //constructor - create an empty binary tree public BinaryTree() { root = null; } //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); } //deleteTree() - remove all items from tree public void deleteList() { root =...
I need help to write this code in C++, I am struggling to find max node's parent's right child and max node's left child. Node* maxValueNode(Node* node) { Node* current = node; while (current && current->right != NULL) current = current->right; return current; } void deleteNode(BST* tree, Node* node, Node* parent) { //TODO - follow the lecture pseudocode to write the deleteNode function // - returns true if the value was deleted, false if not // - don't forget to...
In a binary tree, the balance ratio of node v, bal(v), is the number of nodes in the left subtree of node v divided by the sum of the number of nodes in the right and left subtrees of node v. bal(a leaf node) = ½. A tree T is said to be ε-balanced if, for all nodes v in T, ½ - ε < bal(v) < ½ + ε. Design an efficient recursive algorithm that determines whether a binary...
Let T be a proper binary tree. Given a node v ∈ T, the imbalance of v, denoted imbalance(v), is defined as the difference, in absolute value, between the number of leaves of the left subtree of v and the number of leaves of the right subtree of v. (If v is a leaf, then imbalance(v) is defined to be 0.) Define imbalance(T) = maxv∈T imbalance(v). (a) Provide an upper bound to the imbalance of a proper binary tree with...