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

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

  • 1. the path length of a binary tree BT is the sum of the depths of...

    1. the path length of a binary tree BT is the sum of the depths of all position in BT. write and test a linear-time java method for computing the path length of a binary tree BT. add the method to linkedbinarytree class and write testing code in main method. hint: add method to linkedbinarytree class and write testing in main method public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary...

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

  • i need help to modify this Tree class to include a method leavesCount that returns the...

    i need help to modify this Tree class to include a method leavesCount that returns the number of leaves in a binary tree. (NOTE: Complete leavesCount and leavesCountHelper methods in the Tree class). I already have the test class. java class TreeNode< T extends Comparable< T > > { TreeNode< T > leftNode; T data; TreeNode< T > rightNode; public TreeNode( T nodeData ) { data = nodeData; leftNode = rightNode = null; } public void insert( T insertValue )...

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