COMS W3134 - Spring 2024 - Theory 3 - Answer Key

.pdf

School

Columbia University *

*We aren’t endorsed by this school

Course

3134

Subject

Computer Science

Date

May 8, 2024

Type

pdf

Pages

11

Uploaded by DeanRamMaster2400 on coursehero.com

COMS W3134, Theory Homework 3, Spring 2024 .Name & UNI: _______________________________ Date: __________________ Point values are assigned for each question. Points earned: ____ / 100, = ____ % 1. Assume you have a binary search tree with no duplicate values. The method below, findDifference, will find the greatest difference between any two nodes on the tree. You may not add any additional lines to the functions, since everything you need is given. A TreeNode is defined as follows: class TreeNode { public int value ; public TreeNode left, right; } Fill in the blanks for findDifference , findMin , and findMax to calculate the greatest difference between nodes in a binary search tree. (12 points) public static int findMin(TreeNode root) { if (__A__) { return root.value; } return __B__; } public static int findMax(TreeNode root) { if (__C__) { return root.value; } return __D__; } /** * Finds the greatest difference between nodes in a * binary search tree. */ public static int findDifference(TreeNode root) { if (__E__) { return 0; } return __F__; } A. root.left == null B. findMin(root.left) C. root.right == null D. findMax(root.right) E. root == null
COMS W3134, Theory Homework 3, Spring 2024 F. findMax(root) - findMin(root) 2. Give an iterative algorithm in Java to visit all the nodes in a binary tree with a postorder traversal. Same as above, nodes have left and right references as well as a value field. Each time a Node is visited, your solution will print its value. Write your solution below. (10 points) public static void iterativePostorder(Node root) { if (root == null) return; MyStack<Node> stack = new MyArrayList<>(); Node current = root; Node lastVisited = null; while (current != null || !stack.__A__) { if (current != null) { stack.push(__B__); current = current.left; } else { Node peekNode = stack.peek().right; if (peekNode == null || peekNode == lastVisited) { Node poppedNode = __C__; System.out.println(__D__); lastVisited = __E__; } else { current = peekNode; } } } } A. isEmpty()or empty() B. current C. stack.pop() D. poppedNode.value E. poppedNode
COMS W3134, Theory Homework 3, Spring 2024 full method: public static void iterativePostorder(Node root) { if (root == null) return; MyStack<Node> stack = new MyArrayList<>(); Node current = root; Node lastVisited = null; while (current != null || !stack.isEmpty()) { if (current != null) { stack.push(current); current = current.left; } else { Node peekNode = stack.peek().right; if (peekNode == null || peekNode == lastVisited) { Node poppedNode = stack.pop(); System.out.println(poppedNode.value); lastVisited = poppedNode; } else { current = peekNode; } } } } 3. You are given the preorder and inorder traversals of a binary tree of integers that contains no duplicates. Using this information, reconstruct the binary tree and draw it. (10 points) Inorder: [6, 2, 9, 4, 3, 8, 1, 5, 7, 0] Preorder: [3, 9, 6, 2, 4, 1, 8, 7, 5, 0] Solution 3 / \ / \ / \ 9 1 / \ / \ 6 4 8 7 \ / \ 2 5 0
COMS W3134, Theory Homework 3, Spring 2024 4. One of the properties for red-black trees is that for any given node, the path to its descendant leaves contains the same amount of black nodes. This property is known as the black-heigh of the tree. The recursive function below, checkBlackHeight, takes in a node in a red-black tree and true if the node satisfies the black-height property, and false if it does not. Fill in the blanks to the function given here. (10 points) // Helper function that returns the black height for any node public static int blackHeight(RBNode<K, V> node) { if (node == null) { return 0; } int leftHeight = blackHeight(node.left); int rightHeight = blackHeight(node.right); // If the current node is black, add 1 to the height return ((node.color == RBNode.BLACK) ? 1 : 0) + Math.max(__A__); } // Function to check if a node satisfies the black-height property public static boolean blackHeightEquality(RBNode<K, V> node) { // Base case: a leaf node satisfies the black-height property if (__B__) { return true; } // Get the black height of the left and right subtrees int leftHeight = blackHeight(node.left); int rightHeight = blackHeight(node.right); // Check if the black heights of left and right subtrees are equal if (__C__) { // Recursively check if both left and right subtrees satisfy // the black-height property return ___D___ && ___E___; } return false; } Solution A. leftHeight, rightHeight B. node == null C. leftHeight == rightHeight D. blackHeightEquality(node.right) OR blackHeightEquality(node.left) E. blackHeightEquality(node.right) OR blackHeightEquality(node.left) (depending on which was called first) 5. Using structural induction, prove that the number of leaves in a non-empty full binary tree is one more than the number of internal nodes. (10 points)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help