Question: Please finish everything marked TODO BinaryNode.java public class BinaryNode implements TreePrinter.PrintableNode{ private E data; private BinaryNode left; private BinaryNode right; private int height; private int
Please finish everything marked TODO
BinaryNode.java
public class BinaryNode
public BinaryNode(E data){ this.data = data; this.left = null; this.right = null; this.parent = null; this.height = 1; this.size = 1;
}
// TODO: Set up the BinaryNode public BinaryNode(E data, BinaryNode
// Access fields E data() { return this.data; }; BinaryNode
// Setter fields void setLeft(BinaryNode
// Basic properties int height() { return this.height; } int size() { return this.size; } boolean isBalanced() { int leftHeight = (hasLeft()) ? left.height() : 0; int rightHeight = (hasRight()) ? right().height() : 0; return Math.abs(leftHeight - rightHeight) < 2; } boolean hasLeft(){ return left == null ? false : true; } boolean hasRight(){ return right == null ? false :true; } boolean hasParent(){ return parent == null ? false :true; }
public boolean equals(BinaryNode
// Can use these to help debug public TreePrinter.PrintableNode getLeft() { return left == null ? null : left; } public TreePrinter.PrintableNode getRight() { return right == null ? null : right; } public String getText() { return String.valueOf(data); } public String toString(){ String ret = ""; return "root " + this.data + " Left: " +(hasLeft() ? left.data : null) + " Right: " +(hasRight() ? right.data : null) + " parent: " + (hasParent() ? parent.data : null) ; }
}
BST.java
import java.util.ArrayList; import java.util.Collection; import java.util.List;
public class BST
private int height; private int size; private BinaryNode
public BST(){ this.root = null; this.height = 0; this.size = 0; }
// TODO: BST public BST(BinaryNode
// Access field public BinaryNode
// Basic properties public int height() { return this.height; } public int size() { return this.size; } public boolean isBalanced() { return root.isBalanced(); }
// TODO: updateHeight - Update the root height to reflect any changes public void updateHeight() { }
// Traversals that return lists // TODO: Preorder traversal public List
// TODO: Inorder traversal public List
// TODO: Postorder traversal public List
// Helpers for BST/AVL methods // TODO: extractRightMost // This will be called on the left subtree and will get the maximum value. public BinaryNode
// AVL & BST Search & insert same // TODO: search public BinaryNode
// TODO: insert public void insert(E elem) {
}
// TODO: delete public BinaryNode
// Stuff to help you debug if you want // Can ignore or use to see if it works. static
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
