Question
Write a recursive method(Code )
static void mirrorTree(node root)
This will take a tree as input and then change the tree such that it becomes a mirror of itself.
We have the following methods implemented(you can use them and assume we coded somewhere else): getLeft(), getRight(), setLeft(), setRight(), getValue(). All the values in a node are integers. [Hint: Use them, and think how you would write when the tree only has 2 children ]
Example:
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 3 steps with 1 images
Knowledge Booster
Similar questions
- Please answer the question in the screenshot. The language used here is Java. Please use the starting code.arrow_forwardIn python. Write a LinkedList class that has recursive implementations of the add and remove methods. It should also have recursive implementations of the contains, insert, and reverse methods. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds). It should have a recursive method named to_plain_list that takes no parameters (unless they have default arguments) and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list. The head data member of the LinkedList class must be private and have a get method defined (named get_head). It should return the first Node in the list (not the value inside it). As in the iterative LinkedList in the exploration, the data members of the Node class don't have to be private. The reason for that is because Node is a trivial class…arrow_forwardC. package Final; import java.util.HashSet; public class LLCycle_FE { public static void main(String[] args) { Node head = buildLL(); // Given the above linked list write the 2 methods below (removeDuplicates and showLL) System.out.printf("\n --------- "); // This method will remove any duplicate LL nodes (that is, with the same color) head = removeDuplicates( head ); showLL( head ); } **public static Node removeDuplicates(Node head) { return head; } ** private static void showLL(Node head) { // ToDo: Output the entire linked list } private static Node buildLL() { // Use this code to create your LL Node head = new Node("Red", null); Node n2 = new Node("Blue", null); head.next = n2; Node n3 = new Node("Green", null); n2.next = n3; Node n4 = new Node("Yellow", null); n3.next = n4; Node n5 = new…arrow_forward
- Please fill in the code gaps if possible. This problem has been giving me trouble. Any help is appreciated. public class Node { private String element; // we assume elements are character strings private Node next; /* Creates a node with the given element and next node. */ public Node(String s, Node n) { //fill in body } /* Returns the element of this node. */ public String getElement() { //fill in body } /* Returns the next node of this node. */ public Node getNext() { //fill in body } // Modifier methods: /* Sets the element of this node. */ public void setElement(String newElem) { //fill in body } /* Sets the next node of this node. */ public void setNext(Node newNext) { //fill in body } }arrow_forward24. Add the following new method in the BST class. /** Returns the number of nodes in this binary tree */ public int getNumberofNodes () Requirements: a. Don't use return size; b.write a recursive method that returns the number of nodes starting from www the root.arrow_forwardImplement LeafNode and InteriorNode classes for the expression tree as discussed on this page Use this template: Please don't change any function namesAdd any methods if necesssaryTODO: Remove the pass statements and implement the methods. '''class LeafNode:def __init__(self, data):self.data = datadef postfix(self):return str(self)def __str__(self):return str(self.data)def prefix(self):passdef infix(self):passdef value(self):return self.dataclass InteriorNode:def __init__(self, op, left_op, right_op):self.op = opself.left_op = left_opself.right_op = right_opdef postfix(self):return self.left_op.postfix() + " " + self.right_op.postfix() + " " + self.opdef prefix(self):passdef infix(self):passdef value(self):passif __name__ == "__main__":# TODO: (Optional) your test code here.a = LeafNode(4)b = InteriorNode('+', LeafNode(2), LeafNode(3))c = InteriorNode('*', a, b)c = InteriorNode('-', c, b)arrow_forward
- Question v .Full explain this question and text typing work only We should answer our question within 2 hours takes more time then we will reduce Rating Dont ignore this linearrow_forward8. Write down the insertBefore method which inserts a new element in the list before the node containing the given element. The method takes as parameters a dummy headed doubly linked circular list, the element existing in the list and new element to be added. public void insertBefore (Node head, Object elem, Object newElement) { //to do OR def insertBefore (head, elem, newElement): pass insertBefore (head, 3, 50) Sample Input Sample Output Ox21 2 223 240 O x21 2 22 50 ² 3 2 4 0arrow_forwardTree.java: This is a simple binary tree class that should be used to represent the data in your Huffman tree. You only need to implement two of the methods (_printTree and compareTo). Huffman.java: This file contains all of the logic required to implement a Huffman tree. The code in this file relies on the Tree.java file. You must provide the implementation for all of the methods in this class. public class Main { /* * tree.txt should produce this tree: * 16 * / \ * 6 Z * / \ * A D * * A (1), D (5), Z (10) * * Codes: * A: 00 * D: 01 * Z: 1 * * Preorder Traversal: * 16, 6, 1 (A), 5 (D), 10 (Z) */ public static void main(String[] args) throws Exception { Huffman huff = new Huffman(); huff.buildTreeFromFile("tree.txt"); // Print the tree: System.out.println("printTree tests:"); huff.printTree(); // Expected output: [16: , 6: , 1:A, 5:D, 10:Z] // Get some codes:…arrow_forward
arrow_back_ios
arrow_forward_ios