Question: Implementation for the method: Findpathbetween The node class: public class BSTNode> { private T data; private BSTNode left; private BSTNode right; /** * Create a
Implementation for the method: Findpathbetween The node class:
public class BSTNode> { private T data; private BSTNode left; private BSTNode right; /** * Create a BST node with the given data. * * @param data the data to be stored in this node. */ public BSTNode(T data) { this.data = data; } /** * Get the data in this node. * * @return data in this node. */ public T getData() { return data; } /** * Set the data in this node. * * @param data data to be placed into the node. */ public void setData(T data) { this.data = data; } /** * Get the node to the left of this node. * * @return node to the left. */ public BSTNode getLeft() { return left; } /** * Set the node to the left of this node. * * @param left new node to the left. */ public void setLeft(BSTNode left) { this.left = left; } /** * Get the node to the right of this node. * * @return node to the right. */ public BSTNode getRight() { return right; } /** * Set the node to the right of this node. * * @param right new node to the right. */ public void setRight(BSTNode right) { this.right = right; } } The interface for the method: /** * Finds a path between two elements in the tree, specifically the path * from data1 to data2, inclusive of both. * * To do this, you must first find the deepest common ancestor and add it * to the list, then traverse to data1 while adding its ancestors to the * front of the list then traverse to data2 while adding its ancestors to * the back of the list. Please note that there is no relationship * between the data parameters in that they may not belong to the same * branch. You will most likely have to split off and traverse the tree * for each piece of data. * * You may only use 1 list instance to complete this method. This method * should only need to traverse to the deepest common ancestor once, and * from that node to each data in one traversal each. Failure to * do so will result in a penalty. * * Should run in O(n). * * @param data1 The data to start the path from * @param data2 The data to end the path on * @return the unique path between two elements * @throws IllegalArgumentException if either data1 or data2 is null * @throws java.util.NoSuchElementException if data1 or data2 is not in * the tree */ List findPathBetween(T data1, T data2);
I'm supposed to implement the method from the BST class :
import java.util.Collection; import java.util.List;
public class BST> implements BSTInterface { // DO NOT ADD OR MODIFY INSTANCE VARIABLES. private BSTNode root; private int size; /** * A no argument constructor that should initialize an empty BST. * YOU DO NOT NEED TO IMPLEMENT THIS CONSTRUCTOR! */ public BST() { } /** * Initializes the BST with the data in the Collection. The data in the BST * should be added in the same order it is in the Collection. * * @param data the data to add to the tree * @throws IllegalArgumentException if data or any element in data is null */ public BST(Collection data) { if (data == null) { throw new IllegalArgumentException(""); } for (T item : data) { add(item); } } @Override public List findPathBetween(T data1, T data2) { if (data1 == null || data2 == null) { throw new IllegalArgumentException(""); } // } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
