Question

Design a class called TreeNode (the class java file should be called TreeNode.java) that encodes a binary tree node with inte

show output as well

0 0
Add a comment Improve this question Transcribed image text
Answer #1
        
        class TreeNode {
                private int data;
                private TreeNode left, right;
                
                public TreeNode() {
                        data = 0;
                        left = null;
                        right = null;
                }
                public int getData() {
                        return data;
                }
                public void setData(int data) {
                        this.data = data;
                }
                public TreeNode getLeft() {
                        return left;
                }
                public void setLeft(TreeNode left) {
                        this.left = left;
                }
                public TreeNode getRight() {
                        return right;
                }
                public void setRight(TreeNode right) {
                        this.right = right;
                }
        }
        
        class TreeNodeRef {
                private TreeNode treeNodeRef;
                private TreeNodeRef left, right;

                public TreeNode get() {
                        return treeNodeRef;
                }
                public void set(TreeNode t) {
                        treeNodeRef = t;
                }
                
        }

the question asks just to code the classes, there is no as such main method given or asked to code, which could show the output.. I am not sure for what purpose you are using these classes.. If you provide some info about the project in whcih the classes are getting used, then probably some main class can be written. Thanks!

Add a comment
Know the answer?
Add Answer to:
show output as well Design a class called TreeNode (the class java file should be called...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • Java class TreeNode is defined as below: public class TreeNode {     public int val;     public TreeNode...

    Java class TreeNode is defined as below: public class TreeNode {     public int val;     public TreeNode left, right;     public TreeNode(int x) { val = x; }     public TreeNode(int x, TreeNode lChild, TreeNode rChild) {         val = x;         left = lChild;         right = rChild;     } } Suppose you only have the reference of the root, please write a method that returns a deep copy of the binary tree. Your method returns the root of the deep copy. Your algorithm should...

  • In java, create class BNode(T), which implements the interface TreeNode(T) interface methods: void setRight(TreeNode(T) right) -...

    In java, create class BNode(T), which implements the interface TreeNode(T) interface methods: void setRight(TreeNode(T) right) - sets right side of node, where right is the node to the right of this node void setRight(TreeNode(T) left) - sets left side of node, where left is the node to the left of this node TreeNode getRight() - gets the right node TreeNode getLeft() - gets the left node T getData() - gets the data within the node THEN, create class BT(E), which...

  • java Description You are to create a class named BinaryTree. This will simulate a balanced and...

    java Description You are to create a class named BinaryTree. This will simulate a balanced and ordered binary tree. You also need to create a TreeNode class, which defines the data contained in each node of the tree. In this case, it is an integer. Remember that the node also needs a way to track back to its parent, as well as its left and right children. For the purpose of this assignment, each node should also contain an indication...

  • BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E>...

    BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E> overallRoot;    public BST() {        overallRoot = null;    }    // ************ ADD ************ //    public void add(E addThis) {        if (overallRoot == null) {            overallRoot = new TreeNode<>(addThis);        } else {            add(overallRoot, addThis);        }    }    private TreeNode<E> add(TreeNode<E> node, E addThis) {        if...

  • Write an implementation of a generic java BinaryTree class that fits any primitive data type. your...

    Write an implementation of a generic java BinaryTree class that fits any primitive data type. your class at minimum should have the following. Node class with a minimum of three fields and 3 constructor. BinaryTree class should include the following at least 3 Fields, at least 2 constructors, add, remove, find, getSize method (which returns the amount nods in the tree), getHeight (should return the height), countLeftNode (Should return the mount of left node found in the tree), countRightNode (Should...

  • In Java: Given the following binary tree class, write a recursive method called size() which will...

    In Java: Given the following binary tree class, write a recursive method called size() which will find the number of nodes in the subtree rooted at the current node: class myBinaryTree{ int myValue; myBinaryTree left; myBinaryTree right; myBinaryTree(int inValue) {myValue = inValue;} public int size(){ <CODE WRITTEN HERE> } }

  • I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\...

    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 =...

  • Design a class called Building. The class should keep the following information in the fields: String...

    Design a class called Building. The class should keep the following information in the fields: String address double property_value create several constructors including copy constructor , create accessor and mutator methods        Part 2 (50 points) Define interface Taxable which has one method getPropertyTax() Design a class called Private_Property that extends Building and implements Taxable It should have the following field : double value_of_improvements Create a constructor for this class to include newly added fields. The tax for the private property...

  • 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...

    in java ..write all complete program from a- e 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT