Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Distance between two nodes of binary search tree. public class BST
Distance between two nodes of binary search tree.
public class BSTextends Comparable super T>> 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. * DO NOT IMPLEMENT THIS CONSTRUCTOR! */ public BST() { size = 0; root = null; }
....
...
...
...
/** * Calculates the shortest distance between two elements in the tree. * * To do this, you must first find the deepest common ancestor of both data. * From there, find {@code data1} and {@code data2} in the BST, counting up * the number of nodes it took to get there. 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. * * 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 an efficiency penalty. * * EXAMPLE: Given the BST below composed of Integers: * * 50 * / \ * 25 75 * / \ * 12 37 * / \ \ * 10 15 40 * / * 13 * * distanceBetween(40, 13) should return 5. * distanceBetween(37, 37) should return 0. * distanceBetween(25, 37) should return 1. * * Should have a running time of O(log n) for a balanced tree, and a worst * case of O(n). * * @param data1 the data to start the path from * @param data2 the data to end the path on * @return the shortest distance between the two data in the tree * @throws IllegalArgumentException if either data1 or data2 is null * @throws java.util.NoSuchElementException if data1 or data2 is not in * the tree */ @Override public int distance(T data1, T data2) { }
...
...
.
...
..
public class BSTNodeextends Comparable super T>> { 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; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started