Question

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

public class BinaryTree<T>

{

   private BinaryNode<T> root;

   ...

   // Constructors

   // Get and Set methods

   // Other methods

   /** removeLeftmost()

     * use getLeftChild() or getRightChild() to access left/right child */

   private T removeLeftmost(BinaryNode<T> node)

   {

       // write code here

   }

}

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

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();

   }

}

//******************************************************************************************************************

public class BinaryTree<T>

{

   private BinaryNode<T> root;


   // Constructors

   // Get and Set methods

   // Other methods

   /** removeLeftmost()

     * use getLeftChild() or getRightChild() to access left/right child */

   private T removeLeftmost(BinaryNode<T> node)

   {
       if(node == null) return null;
       T val;
       if(node.getLeftChild() == null) {
           val = node.getData();
           root = node.getRightChild();
           return val;
       }
     
       while(node.getLeftChild().getLeftChild() != null) {
           node = node.getLeftChild();
       }
       val = node.getLeftChild().getData();
       node.setLeftChild(node.getLeftChild().getRightChild());
       return val;
   }

}

Add a comment
Know the answer?
Add Answer to:
Java - Data Structures The method removeLeftmost() in a BinaryNode class is given below. Write this...
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
  • Question B1 You are given the following Java classes: public class Queue { private static class...

    Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...

  • Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation...

    Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

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

  • show output as well Design a class called TreeNode (the class java file should be called...

    show output as well Design a class called TreeNode (the class java file should be called TreeNode.java) that encodes a binary tree node with integral values, with the following (exact) fields and methods/constructors: Description The integral data store in the node Link to the left subtree Link to the right subtree Fields and Methods Fields Data Left Right Methods TreeNode A constructor with no parameters that initialized the fields Design a class called TreeNodeWrapper (the class java file should be...

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

  • In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write...

    In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...

  • Language: Java Topic: Doubly Linked Lists Write a method that removes and returns the last copy...

    Language: Java Topic: Doubly Linked Lists Write a method that removes and returns the last copy of the given data from the list. Do not return the same data that was passed in. Return the data that was stored in the list. Must be O(1) if data is in the tail and O(n) for all other cases.    * @param data the data to be removed from the list * @return the data that was removed * @throws java.lang.IllegalArgumentException if...

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

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

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