!!! JAVA ONLY!! Create a class Tree to model a binary tree . This class should contain a single Node which is the root node of the tree, and should have appropriate getters/setters and toString()....




!!! JAVA ONLY!!



Create a class Tree to model a binary tree. This class should contain a single Node which is the
root node of the tree, and should have appropriate getters/setters and toString().
Again, test out your class by creating a tree and nodes objects; and by calling various methods
on the them in the main method.



Create the following tree using instances of your classes:





Hoare
Jones
Fitzgerald Astarte
Roscoe
Broadfoot Creese
This tree captures PhD supervision hierarchy: Jones was supervised by Hoare, etc.



Write a method traceAndPrint for your Tree class.
It should take as a parameter a string
of L and R characters, and, starting at the root, trace a path through the tree, taking a left
child when encountering L and a right child when encountering R. Each node passed, starting
with the root, should be printed to the console. If directions given are wrong, i.e. a null is
encountered, or any other character is passed to the method, an exception should be thrown.
The exception should indicate what the error is.
For example: using the tree from “LR” should print “Hoare, Jones, Astarte”. “RRR”
should throw an exception. “LZ” should throw an exception.

Hint:
consider using the toCharArray method of the String class to help you parse the input


*****************************************************************************************************************



NODE.java


public class Node {


    // attributes: an element, and two children


    private T element;
    private Node left;
    private Node right;


    // simple constructor which wraps an element and sets no children


    public Node(T el) {
        this.element = el;
        this.left = null;
        this.right = null;
    }


    // constructor for creating Nodes with children right away


    public Node(T el, Node l, Node r) {
        this.element = el;
        this.left = l;
        this.right = r;
    }


    // getters & setters


    public T getElement() {
        return element;
    }


    public void setElement(T el) {
        this.element = el;
    }


    public Node getLeft() {
        return left;
    }


    public Node getRight(){
        return right;
    }


    public void setLeft(Node l) {
        this.left = l;
    }


    public void setRight(Node r) {
        this.right = r;
    }


    // generates a string for printing, wrapping each Node in () for moderately easy viewing
    public String toString() {
        String out = "(";


        // handle an empty node (though these should not be constructable!)
        if (this.element == null) {
            out += " ";
        } else {
            out += "E: " + element.toString() + " ";
        }


        // stringify the left child and any of its children
        out += "L: ";
        if (this.left == null) {
            out += " ";
        } else {
            out += left.toString() + " ";
        }


        // stringify the right node and any of its children
        out += "R: ";
        if (this.right == null) {
            out += "";
        } else {
            out += right.toString() + " ";
        }


        out += ")";


        return out;
    }
}




Create the following tree using instances of your classes:<br>Houare<br>Jones<br>RosCoe<br>Fitzgerald<br>Astarte<br>Broadfoot<br>Crese<br>

Extracted text: Create the following tree using instances of your classes: Houare Jones RosCoe Fitzgerald Astarte Broadfoot Crese
Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here