Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- //
- // CONSTRUCTION: with no initializer
- //
- // ******************PUBLIC OPERATIONS*********************
- // void insert( x ) --> Insert x
- // void remove( x ) --> Remove x
- // Comparable find( x ) --> Return item that matches x
- // Comparable findMin( ) --> Return smallest item
- // Comparable findMax( ) --> Return largest item
- // boolean isEmpty( ) --> Return true if empty; else false
- // void makeEmpty( ) --> Remove all items
- // void printTree( ) --> Print tree in sorted order
- /**
- * Implements an unbalanced binary search tree.
- * Note that all "matching" is based on the compareTo method.
- * @author Mark Allen Weiss
- */
- class BinarySearchTree<E extends Comparable<E>> {
- /** The tree root. */
- private BNode<E> root;
- /**
- * Construct the tree.
- */
- public BinarySearchTree() {
- root = null;
- }
- /**
- * Insert into the tree; duplicates are ignored.
- * @param x the item to insert.
- */
- public void insert(E x) {
- root = insert(x, root);
- }
- /**
- * Remove from the tree. Nothing is done if x is not found.
- * @param x the item to remove.
- */
- public void remove(E x) {
- root = remove(x, root);
- }
- /**
- * Find the smallest item in the tree.
- * @return smallest item or null if empty.
- */
- public E findMin() {
- return elementAt(findMin(root));
- }
- /**
- * Find the largest item in the tree.
- * @return the largest item of null if empty.
- */
- public E findMax() {
- return elementAt(findMax(root));
- }
- /**
- * Find an item in the tree.
- * @param x the item to search for.
- * @return the matching item or null if not found.
- */
- public BNode<E> find(E x) {
- return find(x, root);
- }
- /**
- * Make the tree logically empty.
- */
- public void makeEmpty() {
- root = null;
- }
- /**
- * Test if the tree is logically empty.
- * @return true if empty, false otherwise.
- */
- public boolean isEmpty() {
- return root == null;
- }
- /**
- * Print the tree contents in sorted order.
- */
- public void printTree() {
- if (isEmpty()) {
- System.out.println("Empty tree");
- } else {
- printTree(root);
- }
- }
- /**
- * Internal method to get element field.
- * @param t the node.
- * @return the element field or null if t is null.
- */
- private E elementAt(BNode<E> t) {
- if (t == null)
- return null;
- return t.info;
- }
- /**
- * Internal method to insert into a subtree.
- * @param x the item to insert.
- * @param t the node that roots the tree.
- * @return the new root.
- */
- private BNode<E> insert(E x, BNode<E> t) {
- if (t == null) {
- t = new BNode<E>(x, null, null);
- } else if (x.compareTo(t.info) < 0) {
- t.left = insert(x, t.left);
- } else if (x.compareTo(t.info) > 0) {
- t.right = insert(x, t.right);
- } else; // Duplicate; do nothing
- return t;
- }
- /**
- * Internal method to remove from a subtree.
- * @param x the item to remove.
- * @param t the node that roots the tree.
- * @return the new root.
- */
- @SuppressWarnings({"raw", "unchecked"})
- private BNode<E> remove(Comparable x, BNode<E> t) {
- if (t == null)
- return t; // Item not found; do nothing
- if (x.compareTo(t.info) < 0) {
- t.left = remove(x, t.left);
- } else if (x.compareTo(t.info) > 0) {
- t.right = remove(x, t.right);
- } else if (t.left != null && t.right != null) { // Two children
- t.info = findMin(t.right).info;
- t.right = remove(t.info, t.right);
- } else {
- if (t.left != null)
- return t.left;
- else
- return t.right;
- }
- return t;
- }
- /**
- * Internal method to find the smallest item in a subtree.
- * @param t the node that roots the tree.
- * @return node containing the smallest item.
- */
- private BNode<E> findMin(BNode<E> t) {
- /*
- if (t == null) {
- return null;
- } else if (t.left == null) {
- return t;
- }
- return findMin(t.left);
- */
- if(t==null) return null;
- if(t.left!= null) return findMin(t.left);
- return t;
- }
- /**
- * Internal method to find the largest item in a subtree.
- * @param t the node that roots the tree.
- * @return node containing the largest item.
- */
- private BNode<E> findMax(BNode<E> t) {
- if (t == null) {
- return null;
- } else if (t.right == null) {
- return t;
- }
- return findMax(t.right);
- }
- /**
- * Internal method to find an item in a subtree.
- * @param x is item to search for.
- * @param t the node that roots the tree.
- * @return node containing the matched item.
- */
- private BNode<E> find(E x, BNode<E> t) {
- if (t == null)
- return null;
- if (x.compareTo(t.info) < 0) {
- return find(x, t.left);
- } else if (x.compareTo(t.info) > 0) {
- return find(x, t.right);
- } else {
- return t; // Match
- }
- }
- /**
- * Internal method to print a subtree in sorted order.
- * @param t the node that roots the tree.
- */
- private void printTree(BNode<E> t) {
- if (t != null) {
- printTree(t.left);
- System.out.println(t.info);
- printTree(t.right);
- }
- }
- public void printTreeWithIndent() {
- printTreeWithIndent(root, 0);
- }
- private void printTreeWithIndent(BNode<E> t, int indent) {
- if (t != null) {
- printTreeWithIndent(t.left, indent+1);
- for (int i=0;i<indent;i++)System.out.print(" ");
- System.out.println(t.info);
- printTreeWithIndent(t.right, indent+1);
- }
- }
- public BNode<E> getRoot() {
- return root;
- }
- public int size() {
- return size(root);
- }
- private int size(BNode<E> node) {
- if(node == null) {
- return 0;
- }
- return 1 + size(node.left) + size(node.right);
- }
- public E findKthSmallest(int k) {
- if(k <= 0 || k > size()) {
- return null;
- }
- BNode<E> res = new BNode<E>(null);
- int[] cnt = {0};
- inorderSearch(root, k, cnt, res);
- return res.info;
- }
- private void inorderSearch(BNode<E> node, int k, int[] cnt, BNode<E> result) {
- if(node == null || result.info != null) {
- return;
- }
- inorderSearch(node.left, k, cnt, result);
- cnt[0]++;
- if(cnt[0] == k) {
- result.info = node.info;
- return;
- }
- if(result.info == null) {
- inorderSearch(node.right, k, cnt, result);
- }
- }
- public void invert() {
- invert(root);
- }
- private void invert(BNode<E> node) {
- if(node == null) {
- return;
- }
- BNode<E> tmp = node.left;
- node.left = node.right;
- node.right = tmp;
- invert(node.left);
- invert(node.right);
- }
- public void bfs() {
- if(root == null) {
- return;
- }
- Queue<BNode<E>> queue = new LinkedList<>();
- queue.add(root);
- while(!queue.isEmpty()) {
- BNode<E> currentNode = queue.poll();
- System.out.println(currentNode.info + " ");
- if(currentNode.left != null) {
- queue.add(currentNode.left);
- }
- if(currentNode.right != null) {
- queue.add(currentNode.right);
- }
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment