Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective : - To create “most” of BTNode, and to create “most” of Tree all but r

ID: 3689761 • Letter: O

Question

Objective:
-To create “most” of BTNode, and to create “most” of Tree all but remove( )

UML
BTNode<E> for Lab 06
BTNode<E>
-data:E
-left: BTNode<E>
-right: BTNode<E>
+BTNode ( newData:E, newLeft: BTNode<E>, newRight: BTNode<E>)
+getData():E
+getLeft():BTNode<E>
+getRight():BTNode<E>
+inOrderPrint():void <- code is on slides
+setData(newData:E):void
+setLeft(newLeft: BTNode<E>):void
+setRight(newRight: BTNode<E>):void

Tree<E> for Lab 6
Tree<E>
-root: BTNode<E>
-numItems:int
+Tree()
+add(element:E):void
+size():int
+inOrderPrint():void <- calls root. inOrderPrint() if root is not null
NOTE: you will need to extend Comparable for the .compareTo() to work
public class Tree<E extends Comparable<E>>

Explanation / Answer

public class BTNode { // Invariant of the BTNode class: // 1. Each node has one reference to an E Object, stored in the instance // variable data. // 2. The instance variables left and right are references to the node's // left and right children. private E data; private BTNode left, right; /** * Initialize a BTNode with a specified initial data and links * children. Note that a child link may be the null reference, * which indicates that the new node does not have that child. * @param initialData * the initial data of this new node * @param initialLeft * a reference to the left child of this new node--this reference may be null * to indicate that there is no node after this new node. * @param initialRight * a reference to the right child of this new node--this reference may be null * to indicate that there is no node after this new node. * Postcondition: * This node contains the specified data and links to its children. **/ public BTNode(E initialData, BTNode initialLeft, BTNode initialRight) { data = initialData; left = initialLeft; right = initialRight; } /** * Accessor method to get the data from this node. * @return * the data from this node **/ public E getData( ) { return data; } /** * Accessor method to get a reference to the left child of this node. * @return * a reference to the left child of this node (or the null reference if there * is no left child) **/ public BTNode getLeft( ) { return left; } /** * Accessor method to get the data from the leftmost node of the tree below * this node. * @return * the data from the deepest node that can be reached from this node by * following left links. **/ public E getLeftmostData( ) { if (left == null) return data; else return left.getLeftmostData( ); } /** * Accessor method to get a reference to the right child of this node. * @return * a reference to the right child of this node (or the null reference if there * is no right child) **/ public BTNode getRight( ) { return right; } /** * Accessor method to get the data from the rightmost node of the tree below * this node. * @return * the data from the deepest node that can be reached from this node by * following right links. **/ public E getRightmostData( ) { if (left == null) return data; else return left.getRightmostData( ); } /** * Uses an inorder traversal to print the data from each node at or below * this node of the binary tree. * Postcondition: * The data of this node and all its descendants have been writeen by * System.out.println( ) using an inorder traversal. **/ public void inorderPrint( ) { if (left != null) left.inorderPrint( ); System.out.println(data); if (right != null) right.inorderPrint( ); } /** * Accessor method to determine whether a node is a leaf. * @return * true (if this node is a leaf) or * false (if this node is not a leaf. **/ public boolean isLeaf( ) { return (left == null) && (right == null); } /** * Uses a preorder traversal to print the data from each node at or below * this node of the binary tree. * Postcondition: * The data of this node and all its descendants have been writeen by * System.out.println( ) using a preorder traversal. **/ public void preorderPrint( ) { System.out.println(data); if (left != null) left.preorderPrint( ); if (right != null) right.preorderPrint( ); } /** * Uses a postorder traversal to print the data from each node at or below * this node of the binary tree. * Postcondition: * The data of this node and all its descendants have been writeen by * System.out.println( ) using a postorder traversal. **/ public void postorderPrint( ) { if (left != null) left.postorderPrint( ); if (right != null) right.postorderPrint( ); System.out.println(data); } /** * Uses an inorder traversal to print the data from each node at or below * this node of the binary tree, with indentations to indicate the depth * of each node. * @param depth * the depth of this node (with 0 for root, 1 for the root's * children, and so on)( * Precondition: * depth is the depth of this node. * Postcondition: * The data of this node and all its descendants have been writeen by * System.out.println( ) using an inorder traversal. * The indentation of each line of data is four times its depth in the * tree. A dash "--" is printed at any place where a child has no * sibling. **/ public void print(int depth) { int i; // Print the indentation and the data from the current node: for (i = 1; i