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> implements TreePrinter.PrintableNode{ private E data; private BinaryNode left; private BinaryNode right; private int height; private int size; private BinaryNode parent;

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 left, BinaryNode right, BinaryNode parent){ }

// Access fields E data() { return this.data; }; BinaryNode left() { return this.left; } BinaryNode right() { return this.right; } BinaryNode parent() { return this.parent; }

// Setter fields void setLeft(BinaryNode left) { this.left = left; if(left != null) left.setParent(this); } void setRight(BinaryNode right) { this.right = right; if(right != null) right.setParent(this); } void setParent(BinaryNode parent) { this.parent = parent; } void setData(E data) { this.data = data; } void setHeight(int h){ this.height = h; }

// 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 other){ if(other == null) return false; return other.data.equals(this.data); }

// 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> implements Tree {

private int height; private int size; private BinaryNode root;

public BST(){ this.root = null; this.height = 0; this.size = 0; }

// TODO: BST public BST(BinaryNode root){ }

// Access field public BinaryNode root() { return this.root; }

// 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 preOrderList() { return new ArrayList<>(); }

// TODO: Inorder traversal public List inOrderList() { return new ArrayList<>(); }

// TODO: Postorder traversal public List postOrderList() { return new ArrayList<>(); }

// Helpers for BST/AVL methods // TODO: extractRightMost // This will be called on the left subtree and will get the maximum value. public BinaryNode extractRightMost(BinaryNode curNode) { return null; }

// AVL & BST Search & insert same // TODO: search public BinaryNode search(E elem) { return null; }

// TODO: insert public void insert(E elem) {

}

// TODO: delete public BinaryNode delete(E elem) { return null; }

// Stuff to help you debug if you want // Can ignore or use to see if it works. static > Tree mkBST (Collection elems) { Tree result = new BST<>(); for (E e : elems) result.insert(e); return result; } public TreePrinter.PrintableNode getLeft() { return this.root.hasLeft() ? this.root.left() : null; } public TreePrinter.PrintableNode getRight() { return this.root.hasRight() ? this.root.right() : null; } public String getText() { return (this.root != null) ? this.root.getText() : ""; } }

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!