Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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) ; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started