Question
anyone helps for comments? like // this method is doing ~~ import java.util.Comparator; import java.util.Random; public class BinarySearchTree> { protected BinaryNode root; public BinarySearchTree() {
anyone helps for comments? like // this method is doing ~~
import java.util.Comparator; import java.util.Random; public class BinarySearchTree> { protected BinaryNode root; public BinarySearchTree() { root = null; } public void empty() { root = null; } public boolean isEmpty(){ return root == null; } public BinaryNode getRoot(){ return root; } public int height() { return height(root); } protected int height(BinaryNode root) { if(root == null) { return -1; } else { return Math.max(height(root.getLeft()), height(root.getRight())) + 1; } } public boolean contains(T data){ return contains(data, root); } protected boolean contains(T data, BinaryNode root){ if(root == null){ return false; } int compareResult = root.getData().compareTo(data); if(compareResult > 0){ return contains(data, root.getLeft()); } else if(compareResult < 0){ return contains(data, root.getRight()); } else{ return true; } } public T findMin() { BinaryNode node = findMin(root); return node == null ? null : node.getData(); } protected BinaryNode findMin(BinaryNode root) { if(root == null) { return null; } else if(root.getLeft() == null) { return root; } else { return findMin(root.getLeft()); } } public T findMax() { BinaryNode node = findMax(root); return node == null ? null : node.getData(); } protected BinaryNode findMax(BinaryNode root) { if(root == null) { return null; } else if(root.getRight() == null) { return root; } else { return findMin(root.getRight()); } } public void insert(T data) { root = insert(data, root); } protected BinaryNode insert(T data, BinaryNode root) { if(root == null) { return new BinaryNode<>(data); } int compareResult = root.getData().compareTo(data); if(compareResult > 0) { root.setLeft(insert(data, root.getLeft())); } else if(compareResult < 0) { root.setRight(insert(data, root.getRight())); } return root; } public void remove(T data) { root = remove(data, root); } protected BinaryNode remove(T data, BinaryNode root) { if(root == null) { return null; } int compareResult = root.getData().compareTo(data); if(compareResult > 0) { root.setLeft(remove(data, root.getLeft())); } else if(compareResult < 0) { root.setRight(remove(data, root.getRight())); } else if(root.getLeft() != null && root.getRight() != null) { root.setData(findMin(root.getRight()).getData()); root.setRight(remove(root.getData(), root.getRight())); } else { root = root.getLeft() != null ? root.getLeft() : root.getRight(); } return root; } public void printTree() { printTree(root); } protected void printTree(BinaryNode root) { if(root != null) { printTree(root.getLeft()); System.out.println(root.getData()); printTree(root.getRight()); } } }
---------------------------------------------------------------
public class BinaryNode> { private T data; private int height; private BinaryNode left; private BinaryNode right; public BinaryNode(T data) { this(data, null, null); } public BinaryNode(T data, BinaryNode left, BinaryNode right) { this.data = data; this.left = left; this.right = right; this.height = 0; } public T getData() { return data; } public void setData(T data) { this.data = data; } public BinaryNode getLeft() { return left; } public void setLeft(BinaryNode left) { this.left = left; } public BinaryNode getRight() { return right; } public void setRight(BinaryNode right) { this.right = right; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; }
}
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