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

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

  • 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,...

  • Java - Data Structures The method removeLeftmost() in a BinaryNode class is given below. Write this...

    Java - Data Structures The method removeLeftmost() in a BinaryNode class is given below. Write this method in BinaryTree class. → *BinaryNode class private class BinaryNode<T> {    private T data;    private BinaryNode<T> leftChild;    private BinaryNode<T> rightChild;    ...    // constructors    // get and set methods    // other methods    /** removeLeftmost() - removes the leftmost node of the subtree rooted at this node */    public BinaryNode<T> removeLeftmost()    {       if(leftChild == null)          return rightChild       else          return leftChild.removeLeftmost();    } } ****************************************************************************************************************** --> BinaryTree class...

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

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
ADVERTISEMENT