Question: this is in java import java.util.Comparator; public class BinarySearchTree { private BinaryNode root = null; private Comparator comparator; public BinarySearchTree(Comparator comparator) { this.comparator = comparator;

this is in java

this is in java import java.util.Comparator; public class BinarySearchTree { private BinaryNode

import java.util.Comparator;

public class BinarySearchTree { private BinaryNode root = null; private Comparator comparator; public BinarySearchTree(Comparator comparator) { this.comparator = comparator; } public int size() { return root == null ? 0 : root.size(); } private void add(T item, BinaryNode root) { if (this.comparator.compare(item, root.getData()) 0) { // Item is less than root: // add to right subtree if (root.getRight() == null) { // Base case: opening for a right child BinaryNode newNode = new BinaryNode(); newNode.setData(item); root.setRight(newNode); } else { // Recursively add to right subtree add(item, root.getRight()); } } // If elements are equal (duplicate), the item is already in the tree }

public void add(T item) { if (this.root == null) { this.root = new BinaryNode(); this.root.setData(item); } else { add(item, this.root); } }

private boolean contains(T item, BinaryNode root) { if (this.comparator.compare(item, root.getData()) 0) { // Item is less than root: // search in right subtree if (root.getRight() == null) { // Base case: item not found return false; } else { // Recursively search in right subtree return contains(item, root.getRight()); } } else { // Base case: item found return true; } }

public boolean contains(T item) { return this.root == null ? false : contains(item, this.root); } public String toString() { return "[" + (root == null ? "" : root.inOrder(", ")) + "]"; } public static void main(String[] args) { BinarySearchTree tree = new BinarySearchTree(Comparator.naturalOrder()); for (int i = 0; i

this is binary node class

import java.util.Iterator;

public class BinaryNode implements Iterable { private T data; private BinaryNode left; private BinaryNode right;

public BinaryNode getLeft() { return left; } public BinaryNode getRight() { return right; } public T getData() { return data; } public void setLeft(BinaryNode left) { this.left = left; } public void setRight(BinaryNode right) { this.right = right; } public void setData(T data) { this.data = data; } public String preOrder(String separator) { return data + (left == null ? "" : separator + left.preOrder(separator)) + (right == null ? "" : separator + right.preOrder(separator)); }

public String postOrder(String separator) { return (left == null ? "" : left.postOrder(separator) + separator) + (right == null ? "" : right.postOrder(separator) + separator) + data; }

public String inOrder(String separator) { return (left == null ? "" : left.inOrder(separator) + separator) + data + (right == null ? "" : separator + right.inOrder(separator)); } public Iterator iterator() { return new LevelOrderIterator(this); } public int height() { return 1 + Math.max( left == null ? 0 : left.height(), right == null ? 0 : right.height()); } public int size() { return 1 + (left == null ? 0 : left.size()) + (right == null ? 0 : right.size()); } public T first() { return left == null ? data : left.first(); }

public T last() { return right == null ? data : right.first(); } public static void main(String[] args) { BinaryNode root = new BinaryNode(); root.setData(2); root.setLeft(new BinaryNode()); root.getLeft().setData(7); root.setRight(new BinaryNode()); root.getRight().setData(5); root.getLeft().setLeft(new BinaryNode()); root.getLeft().getLeft().setData(2); root.getLeft().setRight(new BinaryNode()); root.getLeft().getRight().setData(6); root.getRight().setRight(new BinaryNode()); root.getRight().getRight().setData(9); root.getLeft().getRight().setLeft(new BinaryNode()); root.getLeft().getRight().getLeft().setData(5); root.getLeft().getRight().setRight(new BinaryNode()); root.getLeft().getRight().getRight().setData(11); root.getRight().getRight().setLeft(new BinaryNode()); root.getRight().getRight().getLeft().setData(4); System.out.println("Height: " + root.height()); System.out.println("Pre-order: " + root.preOrder(", ")); System.out.println("Post-order: " + root.postOrder(", ")); System.out.println("In-order: " + root.inOrder(", ")); System.out.print("Level-order:"); for (int i : root) { System.out.print(" " + i); } System.out.println(); } }

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!