Write a method: public Iterator shortestPath(T targetOne, T targetTwo) throws ElementNotFoundException{
which 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.
Other methods you may use:
pathToRoot
public Iterator<T> pathToRoot(T targetElement) throws ElementNotFoundException{
ArrayUnorderedList<T> rootList = new ArrayUnorderedList<T>();
pathToRootAgain(targetElement, root, rootList);
if (rootList.isEmpty()==true){
throw new ElementNotFoundException("Binary Tree");
}
return rootList.iterator();
}
Lowest Common Ancestor
public T lowestCommonAncestor( T targetOne, T targetTwo) throws ElementNotFoundException{
Iterator<T> iterator1 = pathToRoot(targetOne);
Iterator<T> iterator2 = pathToRoot(targetTwo);
ArrayUnorderedList<T> lowestPath = new ArrayUnorderedList<T>();
while(iterator1.hasNext()){
lowestPath.addToRear(iterator1.next());
}
while(iterator2.hasNext()){
T temporaryholder = iterator2.next();
//if 'lowest Path' contains the temporary element, return that element
if(lowestPath.contains(temporaryholder)){
return temporaryholder;
}
}
//return the element at the root of the tree
return root.element;
}
SOLUTION:
According to the given data the following code illustrates;
The below is the code with the nodes having the shortest paths and it is as follows;
CODE:
#include <bits/stdc++.h>
using namespace std;
struct Node {
struct Node* left, *right;
int val;
};
struct Node* newNode(int val)
{
struct Node* ptr = new Node;
ptr->val = val;
ptr->left = ptr->right = NULL;
return ptr;
}
struct Node* insert(struct Node* root, int val)
{
if (root==null)
root = newNode(val);
else if (root->val > val)
root->left = insert(root->left, val);
else if (root->val < val)
root->right = insert(root->right, val);
return root;
}
int distanceFromRoot(struct Node* root, int x)
{
if (root->val == x)
return 0;
else if (root->val > x)
return 1 + distanceFromRoot(root->left, x);
return 1 + distanceFromRoot(root->right, x);
}
int distanceBetween2(struct Node* root, int a, int b)
{
if (!root)
return 0;
if (root->val > a && root->val > b)
return distanceBetween2(root->left, a, b);
if (root->val < a && root->val < b) // same
path
return distanceBetween2(root->right, a, b);
if (root->val >= a && root->val <= b)
return distanceFromRoot(root, a) +
distanceFromRoot(root, b);
}
int findDistMethod(Node *root, int a, int b)
{
if (a > b)
swap(a, b);
return distanceBetween2(root, a, b);
}
int main()
{
struct Node* root = NULL;
root = insert(root, 21);
insert(root, 16);
insert(root, 4);
insert(root, 13);
int a = 4, b = 32;
cout << findDistMethod(root, a,b);
return 0;
}
therefore by using the above code that is with respect to the C++ we can get our required output
Write a method: public Iterator shortestPath(T targetOne, T targetTwo) throws ElementNotFoundException{ which will find two elements,...
In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT { private BinaryTreeNode root; /** * Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; } /** * Creates a binary tree from an existing root. */ public LinkedBinaryTree(BinaryTreeNode root) { this.root = root; } /** * Creates a binary tree with the specified element...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...
In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying nodes referenced by positions p and q to be exchanged for each other. Relink the existing nodes, do not create any new nodes. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- package lists; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedPositionalList implements PositionalList { //---------------- nested Node class ---------------- /** * Node of a doubly linked list, which stores a reference to its * element and to both the previous and next...
Consider the following class definition class FibSequence implements Iterablexnteger public Iterator<Integer> iterator) return new FibIterator); private class FibIterator implements Iterator<Integer> f //complete this You are required to create an inner class in the FibSequence class. The inner class should be called FibIterator. It implements the Iterator<Integer> interface and implements the hasNext() and the next() methods. The iterator iterates through the Fibonacci sequence. The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers....
Create a nested inner class inside a file called LinkedList.java called the ListIterator. ListIterator: This public class is nested within an LinkedList data structure and allows us to traverse the elements in any collection, access any data element and remove any data elements of the collection. It also adds the functionality to move forward and backward, which is new functionality. We are going to build this data structure from scratch. Instance variables (fields) are: mPrevious (Node) – the previous node...
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...
When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> { Node<T> itsFirstNode; Node<T> itsLastNode; private int size; public LinkedList() { itsFirstNode = null; itsLastNode = null; size = 0; } public Iterator<T> getIterator() { return new Iterator(this); } // THIS WILL NEED...
Since we do not want to have to rewrite this code when the element type changes, this classes uses a Comparator to assist in ordering the elements. You will need to complete the siftUpComparator() and siftDownComparator() methods in the LinkedHeap Class. siftUp §Added element may violate heap-order property §siftUp() restores order starting at added node §Processing will continue working up the tree until: úFinds properly ordered node & parent úReaches the root (reaches node without parent) siftDown §Restores heap’s order...