Question

Please help me with me. I did the first part to write the operations but in...

Please help me with me. I did the first part to write the operations but in the driver its shows me errors, can't fix it. Help me.
Need help with this to run the application.
/**
 * BinaryTreeNode represents a node in a binary tree with a left and 
 * right child.
 * 
 * @author Java Foundations
 * @version 4.0
 */
public class BinaryTreeNode
{
 protected T element;
 protected BinaryTreeNode left, right;

 /**
  * Creates a new tree node with the specified data.
  */
 public BinaryTreeNode(T obj) 
 {
  // students complete this method
 }

 /**
  * Returns the number of non-null children of this node.
  */
 public int numChildren() 
 {
  // students complete this method
   return -1;
 }

 /**
  * Return the element at this node.
  */
 public T getElement() 
 {
  // students complete this method
   T temp = null;
   return temp;
 }

 /**
  * Return the right child of this node.
  */
 public BinaryTreeNode getRight() 
 {
  // students complete this method
   BinaryTreeNode temp = null;
   return temp;
 }

 /**
  * Sets the right child of this node.
  */
 public void setRight(BinaryTreeNode node) 
 {
  // students complete this method
 }

 /**
  * Return the left child of this node.
  */
 public BinaryTreeNode getLeft() 
 {
  // students complete this method
   BinaryTreeNode temp = null;
   return temp;
 }

 /**
  * Sets the left child of this node.
  */
 public void setLeft(BinaryTreeNode node) 
 {
  // students complete this method
 }
 
 
}
/**
 * Auto Generated Java Class.
 */
public class TestBinaryTreeNode {
  
  public static void main(String[] args)
  {
   
    // create the tree in Figure 1
  
    
    System.out.println("Preorder: ");
    //preOrder(root);
    System.out.println();
    
    System.out.println("Inorder: ");
    //inOrder(root);
    System.out.println();
  
    System.out.println("Postorder: ");
    //postOrder(root);
    System.out.println();
    
    }



 /**
  * Performs a recursive inorder traversal.
  */
public static void inOrder(BinaryTreeNode node) 
 {
  // students complete this method
}

 /**
  * Performs a recursive preorder traversal.
  *
  * @param node the node to be used as the root for this traversal
  * @param tempList the temporary list for use in this traversal
  */
public static void preOrder(BinaryTreeNode node) 
 {
  // students complete this method
}

 /**
  * Performs a recursive postorder traversal.
  *
  * @param node the node to be used as the root for this traversal
  * @param tempList the temporary list for use in this traversal
  */
public static void postOrder(BinaryTreeNode node) 
 {
  // students complete this method
}

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the solution below:

I have created code for both the class, as input tree was not provided simulation is not possible. Please use this code, it would generate the appropriate output.

I have mentioned details in comments.

BinaryTreeNode.java

//added generic operator to full fill the requirement
public class BinaryTreeNode<T> {
    protected T element;
    protected BinaryTreeNode<T> left, right;

    /**
     * Creates a new tree node with the specified data.
     */
    public BinaryTreeNode(T obj)
    {
        // students complete this method
        //intialize value and set left and right to null
        this.element = obj;
        this.left = null;
        this.right = null;
    }

    /**
     * Returns the number of non-null children of this node.
     */
    public int numChildren()
    {
        // students complete this method
        //intitalize the children with 0 and then call the method on left and right child
        int childern = 0;
        if(this.left!=null)
            childern = 1 + left.numChildren();
        if(this.right!=null)
            childern = 1 + right.numChildren();
        return childern;
    }

    /**
     * Return the element at this node.
     */
    public T getElement()
    {
        // students complete this method
        return this.element;
    }

    /**
     * Return the right child of this node.
     */
    public BinaryTreeNode<T> getRight()
    {
        // students complete this method
        return this.right;
    }

    /**
     * Sets the right child of this node.
     */
    public void setRight(BinaryTreeNode<T> node)
    {
        // students complete this method
        this.right = node;
    }

    /**
     * Return the left child of this node.
     */
    public BinaryTreeNode<T> getLeft()
    {
        // students complete this method
        return this.left;
    }

    /**
     * Sets the left child of this node.
     */
    public void setLeft(BinaryTreeNode<T> node)
    {
        // students complete this method
        this.left = node;
    }

}
TestBinaryTreeNode.java

package com.company;

public class TestBinaryTreeNode {

    public static void main(String[] args)
    {

        // create the tree in Figure 1
        BinaryTreeNode<Integer> root;
        BinaryTreeNode<Integer> node_4 = new BinaryTreeNode<>(4);
        BinaryTreeNode<Integer> node_2 = new BinaryTreeNode<>(2);
        BinaryTreeNode<Integer> node_7 = new BinaryTreeNode<>(7);
        BinaryTreeNode<Integer> node_1 = new BinaryTreeNode<>(1);
        BinaryTreeNode<Integer> node_3 = new BinaryTreeNode<>(3);
        BinaryTreeNode<Integer> node_6 = new BinaryTreeNode<>(6);
        BinaryTreeNode<Integer> node_9 = new BinaryTreeNode<>(9);
        BinaryTreeNode<Integer> node_8 = new BinaryTreeNode<>(8);
        BinaryTreeNode<Integer> node_10 = new BinaryTreeNode<>(10);

        root = node_4;
        root.setLeft(node_2);
        root.setRight(node_7);

        node_2.setLeft(node_1);
        node_2.setRight(node_3);

        node_7.setLeft(node_6);
        node_7.setRight(node_9);

        node_9.setLeft(node_8);
        node_9.setRight(node_10);


        System.out.println("Preorder: ");
        preOrder(root);
        System.out.println();

        System.out.println("Inorder: ");
        inOrder(root);
        System.out.println();

        System.out.println("Postorder: ");
        postOrder(root);
        System.out.println();

    }
    /**
     * Performs a recursive inorder traversal.
     */
    public static void inOrder(BinaryTreeNode node)
    {
        // students complete this method
        if(node==null)
            return;

        //follow the sequence left.data,root.data,right.data
        inOrder(node.getLeft());
        System.out.print(node.getElement() + " ");
        inOrder(node.getRight());
    }

    /**
     * Performs a recursive preorder traversal.
     *
     * @param node the node to be used as the root for this traversal
     * @param //tempList the temporary list for use in this traversal
     */
    public static void preOrder(BinaryTreeNode node)
    {
        // students complete this method
        if(node==null)
            return;
        //follow the sequence root.data,left.data,right.data
        System.out.print(node.getElement() + " ");
        preOrder(node.getLeft());
        preOrder(node.getRight());
    }

    /**
     * Performs a recursive postorder traversal.
     *
     * @param node the node to be used as the root for this traversal
     * @param //tempList the temporary list for use in this traversal
     */
    public static void postOrder(BinaryTreeNode node)
    {
        // students complete this method
        if(node == null)
            return;
        //follow the sequence left.data,right.data,root.data
        postOrder(node.getLeft());
        postOrder(node.getRight());
        System.out.print(node.getElement() + " ");
    }
}

Output:

Add a comment
Know the answer?
Add Answer to:
Please help me with me. I did the first part to write the operations but in...
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
  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective:...

    JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java  (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • Hello, I've been working on this for a while and I can't figure the rest out....

    Hello, I've been working on this for a while and I can't figure the rest out. Need asap if anyone can help. maxBSt,minBST,isBST, and inOrder package lab7; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class TreeExercise {    /*    * Construct BST from preorder traversal    */    public static Node<Integer> consBSTfromPreOrder(int[] arr, int start, int end)    {                       if(start > end) return null;               Node<Integer> root = new Node<Integer>(arr[start],...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • QUESTION 8   In the _____ traversal, the root is processed first, before its subtrees. breadth first...

    QUESTION 8   In the _____ traversal, the root is processed first, before its subtrees. breadth first preorder postorder inorder 0.10000 points    QUESTION 9 What kind of traversal does the following algorithm (in pseudo-code) describe? Algorithm traversal (root) if (root is not null)      traversal (leftSubTree)      process (root)      traversal (rightSubTree) end if end traversal breadth first preorder inorder postorder 0.10000 points    QUESTION 10 What kind of traversal does the following algorithm (in pseudo-code)  describe? Algorithm traversal (root) if...

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