Question: Please who can help with this question in Java thank you Here is the interface BTPosition public interface BTPosition extends Position { /** Returns the

Please who can help with this question in Java thank you

Here is the interface BTPosition

public interface BTPosition extends Position { /** Returns the element stored at this position */ // public E element(); /** Sets the element stored at this position */ public void setElement(E o); /** Returns the left child of this position */ public BTPosition getLeft(); /** Sets the left child of this position */ public void setLeft(BTPosition v); /** Returns the right child of this position */ public BTPosition getRight(); /** Sets the right child of this position */ public void setRight(BTPosition v); /** Returns the parent of this position */ public BTPosition getParent(); /** Sets the parent of this position */ public void setParent(BTPosition v); } 2. Class BTNode. /** * Class implementing a node of a binary tree by storing references to * an element, a parent node, a left node, and a right node. */ public class BTNode implements BTPosition { private E element; // element stored at this node private BTPosition left, right, parent; // adjacent nodes /** Main constructor */ public BTNode(E element, BTPosition parent, BTPosition left, BTPosition right) { setElement(element); setParent(parent); setLeft(left); setRight(right); } /** Returns the element stored at this position */ public E element() { return element; } /** Sets the element stored at this position */ public void setElement(E o) { element=o; } /** Returns the left child of this position */ public BTPosition getLeft() { return left; } /** Sets the left child of this position */ public void setLeft(BTPosition v) { left=v; } /** Returns the right child of this position */ public BTPosition getRight() { return right; } /** Sets the right child of this position */ public void setRight(BTPosition v) { right=v; } /** Returns the parent of this position */ public BTPosition getParent() { return parent; } /** Sets the parent of this position */ public void setParent(BTPosition v) { parent=v; } } 3. The interface BinaryTree /** * An interface for a binary tree, where each node can have zero, one, * or two children. */ public interface BinaryTree extends Tree { /** Returns the left child of a node. */ public Position left(Position v) throws InvalidPositionException, BoundaryViolationException; /** Returns the right child of a node. */ public Position right(Position v) throws InvalidPositionException, BoundaryViolationException; /** Returns whether a node has a left child. */ public boolean hasLeft(Position v) throws InvalidPositionException; /** Returns whether a node has a right child. */ public boolean hasRight(Position v) throws InvalidPositionException; } 4. The interface Tree public interface Tree { /** Returns the number of nodes in the tree. */ public int size(); /** Returns whether the tree is empty. */ public boolean isEmpty(); /** Returns an iterator of the elements stored in the tree. */ public Iterable iterator(); /** Returns an iterable collection of the the nodes. */ public Iterable> positions(); /** Replaces the element stored at a given node. */ public E replace(Position v, E e) throws InvalidPositionException; /** Returns the root of the tree. */ public Position root() throws EmptyTreeException; /** Returns the parent of a given node. */ public Position parent(Position v) throws InvalidPositionException, BoundaryViolationException; /** Returns an iterable collection of the children of a given node. */ public Iterable> children(Position v) throws InvalidPositionException; /** Returns whether a given node is internal. */ public boolean isInternal(Position v) throws InvalidPositionException; /** Returns whether a given node is external. */ public boolean isExternal(Position v) throws InvalidPositionException; /** Returns whether a given node is the root of the tree. */ public boolean isRoot(Position v) throws InvalidPositionException; } Complete the following codes: * An implementation of the BinaryTree interface by means of a linked structure. */ //import java.util.Iterator; import java.util.ArrayList; import java.util.Random; public class LinkedBinaryTree implements BinaryTree { protected BTPosition root; // reference to the root protected int size; // number of nodes /** Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; // start with an empty tree size = 0; } /** Returns the number of nodes in the tree. */ public int size() { return size; } public boolean isEmpty() { return size() == 0;} /** Returns whether a node is internal. */ public boolean isInternal(Position v) throws InvalidPositionException { // your work is here } public boolean isExternal(Position v) throws InvalidPositionException { // checkPosition(v); // auxiliary method return !isInternal(v); } /** Returns whether a node is the root. */ public boolean isRoot(Position v) throws InvalidPositionException { // your work is here. } /** Returns whether a node has a left child. */ public boolean hasLeft(Position v) throws InvalidPositionException { // Your work is here. public boolean hasRight(Position v) throws InvalidPositionException { BTPosition vv = checkPosition(v); return (vv.getRight() != null); } /** Returns the root of the tree. */ public Position root() throws EmptyTreeException { if (root == null) throw new EmptyTreeException("The tree is empty"); return root; } /** Returns the left child of a node. */ public Position left(Position v) throws InvalidPositionException, BoundaryViolationException { // Your work is here. } public Position right(Position v) throws InvalidPositionException, BoundaryViolationException { BTPosition vv = checkPosition(v); Position rightPos = vv.getRight(); if (rightPos == null) throw new BoundaryViolationException("No right child"); return rightPos; }

/** Returns the parent of a node. */ public Position parent(Position v) throws InvalidPositionException, BoundaryViolationException { BTPosition vv = checkPosition(v); Position parentPos = vv.getParent(); if (parentPos == null) throw new BoundaryViolationException("No parent"); return parentPos; } /** Returns an iterable collection of the children of a node. */ public Iterable> children(Position v) throws InvalidPositionException { ArrayList> children = new ArrayList>(); if (hasLeft(v)) children.add(left(v)); if (hasRight(v)) children.add(right(v)); return children; } /** Returns an iterable collection of the tree nodes. */ public Iterable> positions() { ArrayList> positions = new ArrayList>(); if(size != 0) inorderPositions(root(), positions); // assign positions in inorder return positions; } /** Returns an iterator of the elements stored at the nodes */ public Iterable iterator() { Iterable> positions = positions(); ArrayList elements = new ArrayList(); for (Position pos: positions) elements.add(pos.element()); return elements; // An iterator of elements } /** Replaces the element at a node. */ public E replace(Position v, E o) throws InvalidPositionException { BTPosition vv = checkPosition(v); E temp = v.element(); vv.setElement(o); return temp; } // Additional accessor method /** Return the sibling of a node */ public Position sibling(Position v) throws InvalidPositionException, BoundaryViolationException { BTPosition vv = checkPosition(v); BTPosition parentPos = vv.getParent(); if (parentPos != null) { BTPosition sibPos; BTPosition leftPos = parentPos.getLeft(); if (leftPos == vv) sibPos = parentPos.getRight(); else sibPos = parentPos.getLeft(); if (sibPos != null) return sibPos; } throw new BoundaryViolationException("No sibling"); } // Additional update methods /** Adds a root node to an empty tree */ public Position addRoot(E e) throws NonEmptyTreeException { if(!isEmpty()) throw new NonEmptyTreeException("Tree already has a root"); size = 1; root = createNode(e,null,null,null); return root; } /** Inserts a left child at a given node. */ public Position insertLeft(Position v, E e) throws InvalidPositionException { BTPosition vv = checkPosition(v); Position leftPos = vv.getLeft(); if (leftPos != null) throw new InvalidPositionException("Node already has a left child"); BTPosition ww = createNode(e, vv, null, null); vv.setLeft(ww); size++; return ww; }

/** Inserts a right child at a given node. */ public Position insertRight(Position v, E e) throws InvalidPositionException { BTPosition vv = checkPosition(v); Position rightPos = vv.getRight(); if (rightPos != null) throw new InvalidPositionException("Node already has a right child"); BTPosition ww = createNode(e, vv, null, null); vv.setRight(ww); size++; return ww; } /** Removes a node with zero or one child. */ public E remove(Position v) throws InvalidPositionException { BTPosition vv = checkPosition(v); BTPosition leftPos = vv.getLeft(); BTPosition rightPos = vv.getRight(); if (leftPos != null && rightPos != null) throw new InvalidPositionException("Cannot remove node with two children"); BTPosition ww; // the only child of v, if any if (leftPos != null) ww = leftPos; else if (rightPos != null) ww = rightPos; else // v is a leaf ww = null; if (vv == root) { // v is the root if (ww != null) ww.setParent(null); root = ww; } else { // v is not the root BTPosition uu = vv.getParent(); if (vv == uu.getLeft()) uu.setLeft(ww); else uu.setRight(ww); if(ww != null) ww.setParent(uu); } size--; return v.element(); } /** Attaches two trees to be subtrees of an external node. */ public void attach(Position v, BinaryTree T1, BinaryTree T2) throws InvalidPositionException { BTPosition vv = checkPosition(v); if (isInternal(v)) throw new InvalidPositionException("Cannot attach from internal node"); if (!T1.isEmpty()) { BTPosition r1 = checkPosition(T1.root()); vv.setLeft(r1); r1.setParent(vv); // T1 should be invalidated } if (!T2.isEmpty()) { BTPosition r2 = checkPosition(T2.root()); vv.setRight(r2); r2.setParent(vv); // T2 should be invalidated } } /** If v is a good binary tree node, cast to BTPosition, else throw exception */ protected BTPosition checkPosition(Position v) throws InvalidPositionException { // Your work is here. } /** Creates a new binary tree node */ protected BTPosition createNode(E element, BTPosition parent, BTPosition left, BTPosition right) { return new BTNode(element,parent,left,right); } /** Creates a list storing the the nodes in the subtree of a node, * ordered according to the ineorder traversal of the subtree. */ protected void inorderPositions(Position v, ArrayList> pos) throws InvalidPositionException { // Your work is here. } public int depth (Tree T, Position v) { if (T.isRoot(v)) return 0; else return 1 + depth(T, T.parent(v)); } public int height1 (Tree T) { int h = 0; for (Position v : T.positions()) { if (T.isExternal(v)) h = Math.max(h, depth(T, v)); } return h; } public int height2 (Tree T, Position v) { if (T.isExternal(v)) return 0; int h = 0; for (Position w : T.children(v)) h = Math.max(h, height2(T, w)); return 1 + h; }

public static void main(String[] args) { LinkedBinaryTree T = new LinkedBinaryTree(); Random rand = new Random(); int n = 100; int j; if (T.isEmpty()) T.addRoot(rand.nextInt(1000)); System.out.println("The root element is " + T.root().element()); Position p = T.root(); for (int i = 0; i <= n; i++) { j = rand.nextInt(1000); if ( j % 2 == 0) { T.insertLeft(p, j); p = T.left(p); } else { T.insertRight(p, j); p = T.right(p); } } System.out.println("The size of tree is " + T.size()); System.out.println(); System.out.println("The height1 is " + T.height1(T)); System.out.println(); System.out.println("The height2 is " + T.height2(T, T.root())); System.out.println(); System.out.println("The depth of the root is " + T.depth(T, T.root())); System.out.println(); System.out.println("We print all elements in the tree"); int count = 0; for (Integer in: T.iterator()) { System.out.print(in + " "); ++count; if(count % 10==0) System.out.println(); } System.out.println(); System.out.println();

} }

you are to complete the following codes

thank you

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!