Question: Java Use the LinkedBinaryTree (presented in class) as a super class to implement class BinarySearchTree ONLY for sorting purposes, such as sorting integers, double numbers

Java
Use the LinkedBinaryTree (presented in class) as a super class to implement class BinarySearchTree ONLY for sorting purposes, such as sorting integers, double numbers and arrays of strings.
Input: A set of random integers generated by a random generator. Display all unsorted numbers
Output: All the sorted integers.
 Java Use the LinkedBinaryTree (presented in class) as a super class
to implement class BinarySearchTree ONLY for sorting purposes, such as sorting integers,
double numbers and arrays of strings. Input: A set of random integers
generated by a random generator. Display all unsorted numbers Output: All the
sorted integers. 7:41 1 LTE Done LinkedBinaryTree.java import java.util.ArrayList; import java.util.Random; public
class LinkedBinaryTreeimplements BinaryTree protected static class BTNode implements BTPosition private E element;l/
element stored at this node private BTPosition left, right, parent; II adjacent
nodes /* Main constructor / public BTNode00 public BTNode(E element, BTPosition parent,
BTPosition left, BTPosition right) setElement(element) setParent(parent); setLeft(left); setRight(right) *Returns the element stored

7:41 1 LTE Done LinkedBinaryTree.java import java.util.ArrayList; import java.util.Random; public class LinkedBinaryTreeimplements BinaryTree protected static class BTNode implements BTPosition private E element;l/ element stored at this node private BTPosition left, right, parent; II adjacent nodes /* Main constructor / public BTNode00 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) felement o;) *Returns the left child of this position public BTPositiongetLeft0 return left; ) Sets the left child of this position public void setLeft (BTPosition v) fleft-v;) *Returns the right child of this position / public BTPosition getRight0return right; h /** Sets the right child of this position */ public void setRight(BTPosition v) rightv:) * Returns the parent of this position / public BTPosition getParent0return parent; 7:41 LTE Done LinkedBinaryTree.java Sets the parent of this position public void setParent (BTPosition v) (parent-v;) protected BTPosition root; protected int size public LinkedBinaryTree0) root-null size 0; public int size0freturn size;) public boolean isEmpty0freturn size0 0) public boolean isinternal (Position) checkPosition(v); return (hasLeft(v)II hasRight(v)) public boolean isExternal(Positionv) checkPosition(v) return(!isinternal(v) public boolean isRoot(Positionv) checkPosition(v) return(v-root)) public boolean hasLeft (Positionv) BTPositionvv checkPosition(v) return(vv.getLeftnull) public boolean hasRight(PositionE-v) BTPosition vv= checkPosition(); return (vw.getRight0!-null) 7:43 LTE Done LinkedBinaryTree.java public Position root0 if(root-null)throw new IllegalArgumentException("The tree is empty"); return root public Position left (Positionv) BTPositionvvcheckPosition(v): PositionleftPos-vv.getLeft0 if(leftPosnull)throw nevw llegalArgumentException("Has no Left child"); return leftPos; public Position right(Positionv) BTPositionE-vv= checkPosition(v); PositionrightPos vv.getRight0 if (rightPosnull)throw new IllegalArgumentException("Has no right child"); return rightPos; public Positionparent (Positionv) BTPositionvvcheckPosition(v) PositionparentPos- vv.getParent0: if (parentPos -null) return null return parentPos; public Iterable>children(Positionv) ArrayListPosition> Children= new ArrayList> if (hasLeft(v)) children.add(left V)) if (hasRight(v)) children.add(right(v); return children public Iterablepositions(0 public Iterable>positions( ArrayList>positions new ArrayList> if (size!-0) inorderPositions (root),positions); return positions; public Iterable iterator0 Iterable>positions-positions0; ArrayListelements new ArrayList for(Positionpos:positions) elements.add(pos.element)); return elements; public Position sibling(Positionv) Positionparent validate(v) parent-parent(v); if (parentnull) return null; if v-left(parent)) return right(parent); else return left(parent) BTPositionv checkPosition(v) PositionparentPos-vv.getParent0 if (parentPos!-null) BTPositionsiblingPos; BTPositionleftPos- parentPos.getLeft /BTPositionrightPos-parentPos.getRight); if (leftPosvv) siblingPos-parentPos.getRight0 else siblingPos-parentPos.getLeft0; if (siblingPos!-null) return siblingPos; throw new IllegalArgumentException("No sibling") PI public E replace(Positionv, E o) BTPositionE-vys validate() public E replace(Positionv, E o) BTPositionE-vv= validate(); E temps velement(); vv.setElement(o); return temp; protected BTPositioncheckPosition(Positionv) if(v== null ll! (v instanceof BTPosition)) throw new IllegalArgumentException("invalid position"); return (BTPosition)V; protected BTPosition createNode(E element, BTPositionparent,BTPositionleft,BTPosition right) return new BTNode(element, parent, left, right); protected void inorderPositions (Positionv, ArrayList>pos) if (hasLeft(v)) inorderPositions(left (v),pos); pos.add(V); if hasRight(v)) inorderPositions(right(v).pos); public int depth(TreeT, Position) if (T.isRoot(v)) return 0; else return 1+ depth(T,Tparent(v)) public int height1 (Tree T) int h=0 for(Position: T.positions0) if(T.isExternal(v) h Math.max(h,depth(Tv)); return h; public PositionaddRoot (E e)throws llegalStateException if(lisEmpty0) throw new IllegalStateException("Tree has got a root already"); size=1 root-createNode(e,null,null,null); return root; public PositioninsertLeft (Positionv, Ee)throws llegalArgumentException BTPositionE> vv= validate(v); if (vv.getLeft() !=null) throw new IllegalArgumentException V has already a left child "; BTPositionchild-createNode(e vv,null,null) vv.setLeft(child); Size+ return child public Position insertRight(PositionV,E e)throws llegalArgumentException BTPositionE-vv= validate(); if (vv.getRight ( )!=null) throw new IllegalArgumentException(V has got a child already"); BTPosition child-createNode(evv, null,null); vv.setRight(child); size+4 return child; public E remove(Positionv)throws IllegalStateException BT Position E>vv=validate (v) BTPosition leftPos-vv.getLeft0 BTPosition rightPos vv.getRight0 if (leftPos! null&&rightPos!-null) throw new IllegalStateException("Cannot remove a node that has 2 children") BTPositionc if (leftPos! =null) c leftPos; else c= null if (vv== root) if(c!=null) c.setParent (null); root c else BTPositionE>p=vv.getParent(); if(vv-p.getLeft0) p.setLeft(c); else p.setRight(c); if(c!-null) c.setParent (p); size- return v.element(O public void attach(Positionv, BinaryTreeT1,BinaryTreeT2) throws llegalArgumentException BTPosition t1-validate(T1.root0) vv.setLeft(t1); t1.setParent(v) if (T2.isEmpty)) BTPositiont2-checkPosition(T2.root)) vv.setRight(t2); t2.setParent(vv) public BTPosition validate(Positionp)throws IllegalArgumentException if ((p instanceof BTPosition)) throw new IllegalArgumentException("Not valid public BTPosition validate(Positionp)throws IllegalArgumentException if (!(p instanceof BTPosition) throw new IllegalArgumentException( Not valid Position Type."); BTPosition v (BTPosition)p; if (v.getParent)v) throw new IllegalArgumentException( p is no longer in the tree"); return v; public static void main(String args) LinkedBinaryTreecInteger> T new LinkedBinaryTreecInteger 0 Random rand-new Random); int n=10 int j if (T.isEmpty0) T.addRoot (rand.nextint (1000)); System.out.printin("the root of the tree is:"+T.root().element)) Position IntegerpzTroot(); for(int i-0;i n:i++) j-rand.nextint (100); if (j%2-:0) T.insertLeft(p,j); p= T.left(p); else T.insertRight(pj): p=T.right (p) System.out.printin("the size of the tree isT.size)); System.out.println("heightl of the tree is +T.height1(T)) System.out.println("depth of the tree is +T.depth(T,T.parent (p))); System.out.printin("Print all the elements in the tree:"); int count-Q 7:44 Done LinkedBinaryTree.java Positionimplements BinaryTree protected static class BTNode implements BTPosition private E element;l/ element stored at this node private BTPosition left, right, parent; II adjacent nodes /* Main constructor / public BTNode00 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) felement o;) *Returns the left child of this position public BTPositiongetLeft0 return left; ) Sets the left child of this position public void setLeft (BTPosition v) fleft-v;) *Returns the right child of this position / public BTPosition getRight0return right; h /** Sets the right child of this position */ public void setRight(BTPosition v) rightv:) * Returns the parent of this position / public BTPosition getParent0return parent; 7:41 LTE Done LinkedBinaryTree.java Sets the parent of this position public void setParent (BTPosition v) (parent-v;) protected BTPosition root; protected int size public LinkedBinaryTree0) root-null size 0; public int size0freturn size;) public boolean isEmpty0freturn size0 0) public boolean isinternal (Position) checkPosition(v); return (hasLeft(v)II hasRight(v)) public boolean isExternal(Positionv) checkPosition(v) return(!isinternal(v) public boolean isRoot(Positionv) checkPosition(v) return(v-root)) public boolean hasLeft (Positionv) BTPositionvv checkPosition(v) return(vv.getLeftnull) public boolean hasRight(PositionE-v) BTPosition vv= checkPosition(); return (vw.getRight0!-null) 7:43 LTE Done LinkedBinaryTree.java public Position root0 if(root-null)throw new IllegalArgumentException("The tree is empty"); return root public Position left (Positionv) BTPositionvvcheckPosition(v): PositionleftPos-vv.getLeft0 if(leftPosnull)throw nevw llegalArgumentException("Has no Left child"); return leftPos; public Position right(Positionv) BTPositionE-vv= checkPosition(v); PositionrightPos vv.getRight0 if (rightPosnull)throw new IllegalArgumentException("Has no right child"); return rightPos; public Positionparent (Positionv) BTPositionvvcheckPosition(v) PositionparentPos- vv.getParent0: if (parentPos -null) return null return parentPos; public Iterable>children(Positionv) ArrayListPosition> Children= new ArrayList> if (hasLeft(v)) children.add(left V)) if (hasRight(v)) children.add(right(v); return children public Iterablepositions(0 public Iterable>positions( ArrayList>positions new ArrayList> if (size!-0) inorderPositions (root),positions); return positions; public Iterable iterator0 Iterable>positions-positions0; ArrayListelements new ArrayList for(Positionpos:positions) elements.add(pos.element)); return elements; public Position sibling(Positionv) Positionparent validate(v) parent-parent(v); if (parentnull) return null; if v-left(parent)) return right(parent); else return left(parent) BTPositionv checkPosition(v) PositionparentPos-vv.getParent0 if (parentPos!-null) BTPositionsiblingPos; BTPositionleftPos- parentPos.getLeft /BTPositionrightPos-parentPos.getRight); if (leftPosvv) siblingPos-parentPos.getRight0 else siblingPos-parentPos.getLeft0; if (siblingPos!-null) return siblingPos; throw new IllegalArgumentException("No sibling") PI public E replace(Positionv, E o) BTPositionE-vys validate() public E replace(Positionv, E o) BTPositionE-vv= validate(); E temps velement(); vv.setElement(o); return temp; protected BTPositioncheckPosition(Positionv) if(v== null ll! (v instanceof BTPosition)) throw new IllegalArgumentException("invalid position"); return (BTPosition)V; protected BTPosition createNode(E element, BTPositionparent,BTPositionleft,BTPosition right) return new BTNode(element, parent, left, right); protected void inorderPositions (Positionv, ArrayList>pos) if (hasLeft(v)) inorderPositions(left (v),pos); pos.add(V); if hasRight(v)) inorderPositions(right(v).pos); public int depth(TreeT, Position) if (T.isRoot(v)) return 0; else return 1+ depth(T,Tparent(v)) public int height1 (Tree T) int h=0 for(Position: T.positions0) if(T.isExternal(v) h Math.max(h,depth(Tv)); return h; public PositionaddRoot (E e)throws llegalStateException if(lisEmpty0) throw new IllegalStateException("Tree has got a root already"); size=1 root-createNode(e,null,null,null); return root; public PositioninsertLeft (Positionv, Ee)throws llegalArgumentException BTPositionE> vv= validate(v); if (vv.getLeft() !=null) throw new IllegalArgumentException V has already a left child "; BTPositionchild-createNode(e vv,null,null) vv.setLeft(child); Size+ return child public Position insertRight(PositionV,E e)throws llegalArgumentException BTPositionE-vv= validate(); if (vv.getRight ( )!=null) throw new IllegalArgumentException(V has got a child already"); BTPosition child-createNode(evv, null,null); vv.setRight(child); size+4 return child; public E remove(Positionv)throws IllegalStateException BT Position E>vv=validate (v) BTPosition leftPos-vv.getLeft0 BTPosition rightPos vv.getRight0 if (leftPos! null&&rightPos!-null) throw new IllegalStateException("Cannot remove a node that has 2 children") BTPositionc if (leftPos! =null) c leftPos; else c= null if (vv== root) if(c!=null) c.setParent (null); root c else BTPositionE>p=vv.getParent(); if(vv-p.getLeft0) p.setLeft(c); else p.setRight(c); if(c!-null) c.setParent (p); size- return v.element(O public void attach(Positionv, BinaryTreeT1,BinaryTreeT2) throws llegalArgumentException BTPosition t1-validate(T1.root0) vv.setLeft(t1); t1.setParent(v) if (T2.isEmpty)) BTPositiont2-checkPosition(T2.root)) vv.setRight(t2); t2.setParent(vv) public BTPosition validate(Positionp)throws IllegalArgumentException if ((p instanceof BTPosition)) throw new IllegalArgumentException("Not valid public BTPosition validate(Positionp)throws IllegalArgumentException if (!(p instanceof BTPosition) throw new IllegalArgumentException( Not valid Position Type."); BTPosition v (BTPosition)p; if (v.getParent)v) throw new IllegalArgumentException( p is no longer in the tree"); return v; public static void main(String args) LinkedBinaryTreecInteger> T new LinkedBinaryTreecInteger 0 Random rand-new Random); int n=10 int j if (T.isEmpty0) T.addRoot (rand.nextint (1000)); System.out.printin("the root of the tree is:"+T.root().element)) Position IntegerpzTroot(); for(int i-0;i n:i++) j-rand.nextint (100); if (j%2-:0) T.insertLeft(p,j); p= T.left(p); else T.insertRight(pj): p=T.right (p) System.out.printin("the size of the tree isT.size)); System.out.println("heightl of the tree is +T.height1(T)) System.out.println("depth of the tree is +T.depth(T,T.parent (p))); System.out.printin("Print all the elements in the tree:"); int count-Q 7:44 Done LinkedBinaryTree.java Position

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!