Csc 115, Fall 2002 Final Exam 9. Coding Trees (12 marks) The program on the foll
ID: 3709689 • Letter: C
Question
Csc 115, Fall 2002 Final Exam 9. Coding Trees (12 marks) The program on the following page imple make two additions to this code: You need to For (a) Add a new meth example, the tree on the right shows a mirror image of createMirrorTree method does not change the tree it is associated with, it simply creates a new tree and returns a reference to the root of that tree. (Hint: you may find this question easier if you use recursion.) (8 marks) od to the TreeNode class to create a mirror image of the tree. The the tree on the left. public TreeNode createMirrorTree() f (b) The iterator supplied with this Binary Tree class provides a preorder traversal of the binary tree. Change the code in this iterator so that it returns the preorder traversal of the tree, but in reverse. For example, the preorder traversal of the tree above on the left is: 4, 6, 6, 7, 7, 8. In reverse this should be: 8,7, the next page by marking up the existing code. (4 marks) 7, 6,6, 4. Show the required changes on 16Explanation / Answer
If you post more than 1 question, as per chegg guidelines i ahve to solve only first question.
(A)
public TreeNode createMirrorTree()
{
// root oints to the root node of the tree
return createMirrorTree_util(root);
}
public TreeNode createMirrorTree_util(TreeNode x)
{
// if tree is empty
if( x == null )
retur null;
// recursively traverse the left tree
TreeNode l = createMirrorTree_util(x.getLeft());
// recursively traverse the right tree
TreeNode r = createMirrorTree_util(x.getLeft());
// make l as the new right child
x.setRight( l );
// make r as the new left child
x.setLeft( r );
return x;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.