Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For thi last problem, you will find two elements, and return an iterator that can traverse the elements along the shortest possible path (following the

For thi last problem, you will find two elements, and return an iterator that can traverse the elements along the shortest possible path (following the connections that exist in the tree between parent and child nodes) to get from one given tree node to another given tree node. You can use both the path to root and lowest common ancestor in your solution. This method will be added to the LinkedBinaryTree.java file, and has the following header: public Iterator shortestPath(T targetOne, T targetTwo) throws ElementNotFoundException{

LowestCommonAnscestor.java

public T lowestCommonAncestor( T targetOne, T targetTwo) throws ElementNotFoundException{
// Create two iterators that refer to the
// Path to root for two elements in a tree
// These may throw the ElementNotFoundException
Iterator one = pathToRoot(targetOne);
Iterator two = pathToRoot(targetTwo);
// Create a new unordered list of generic type T
ArrayUnorderedList onPathOne = new ArrayUnorderedList();
// While iterator one has more elements
while(one.hasNext()){
// Add the next element from iterator one to the
// Rear of the unordered list
onPathOne.addToRear(one.next());
}
// While the second iterator has more elements
while(two.hasNext()){
// Create a generic element temp that
// Refers to the next element from iterator two
T temp = two.next();
// If the unordered list contains temp
if(onPathOne.contains(temp)){
// Return the temp element
return temp;
}
}
// In the worst case scenario
// Return the element at the root of the tree
return root.element;
}

}

pathToRoot

public Iterator pathToRoot(T targetElement) throws ElementNotFoundException{
// Create a new unordered list of generic type T
ArrayUnorderedList pathToRoot = new ArrayUnorderedList();
// Use the recursive method pathToRootAgain taking variables
// targetElement (the element to be found)
// root (the root of the tree)
// pathToRoot (the unordered list we created at the start
pathToRootAgain(targetElement, root, pathToRoot);
// If the unordered list is empty
if (pathToRoot.isEmpty() == true){
// Throw a new Element Not found Exception
throw new ElementNotFoundException("Binary Tree");
}
// Returns the list after being iterated
return pathToRoot.iterator();
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions