Implements a Binary Search Tree in symmetric order. - Java Data Structure

Java examples for Data Structure:Tree

Description

Implements a Binary Search Tree in symmetric order.

Demo Code

import java.util.LinkedList;
import java.util.Queue;

class BinarySearchTree<Key extends Comparable<Key>, Value> {
  private Node root;

  class Node {
    private Key key;
    private Value val;

    private Node left, right;// left & right subtrees
    private int count = 1;

    public Node(Key k, Value v) {
      this.key = k;
      this.val = v;
    }// w w  w .ja  v a2 s .co  m

    @Override
    public String toString() {
      return "" + key;
    }
  }

  public boolean isEmpty() {
    return size() == 0;
  }

  public int size() {
    return size(root);
  }

  private int size(Node x) {
    if (x == null)
      return 0;
    return x.count;
  }

  public boolean contains(Key k) {
    return get(k) != null;
  }

  public Value get(Key key) {
    Node x = root;

    while (x != null) {
      int cmp = key.compareTo(x.key);

      if (cmp < 0)
        x = x.left;
      else if (cmp > 0)
        x = x.right;
      else
        return x.val;
    }

    return null;
  }

  public void put(Key key, Value val) {
    root = put(root, key, val);
  }

  private Node put(Node x, Key key, Value val) {
    if (x == null)
      return new Node(key, val);

    int cmp = key.compareTo(x.key);

    if (cmp < 0)
      x.left = put(x.left, key, val);
    else if (cmp > 0)
      x.right = put(x.right, key, val);
    else
      x.val = val; // found node

    x.count = 1 + size(x.left) + size(x.right);
    return x;
  }

  public int height() {
    return height(root);
  }

  private int height(Node x) {
    if (x == null)
      return -1;
    return 1 + Math.max(height(x.left), height(x.right));
  }

  public Key min() {
    if (isEmpty())
      return null;
    return min(root).key;
  }

  private Node min(Node x) {
    if (x.left == null)
      return x;
    return min(x.left);
  }

  public Key max() {
    if (isEmpty())
      return null;
    return max(root).key;
  }

  private Node max(Node x) {
    if (x.right == null)
      return x;

    return max(x.right);
  }

  public Key select(int k) {
    if (k < 0 || k >= size())
      return null;

    Node x = select(root, k);
    return x.key;
  }

  private Node select(Node x, int k) {
    if (x == null)
      return null;

    int t = size(x.left);
    if (t > k)
      return select(x.left, k);
    else if (t < k)
      return select(x.right, k - t - 1);
    else
      return x;
  }

  public int rank(Key key) {
    return rank(key, root);
  }

  private int rank(Key key, Node x) {
    if (x == null)
      return 0;

    int cmp = key.compareTo(x.key);
    if (cmp < 0)
      return rank(key, x.left);
    else if (cmp > 0)
      return 1 + size(x.left) + rank(key, x.right);
    else
      return size(x.left);
  }

  public void deleteMin() {
    if (isEmpty())
      return;
    root = deleteMin(root);
  }

  private Node deleteMin(Node x) {
    if (x.left == null)
      return x.right;

    x.left = deleteMin(x.left);
    x.count = size(x.left) + size(x.right) + 1;
    return x;
  }

  public void deleteMax() {
    if (isEmpty())
      return;
    root = deleteMax(root);
  }

  private Node deleteMax(Node x) {
    if (x.right == null)
      return x.left;

    x.right = deleteMax(x.right);
    x.count = size(x.left) + size(x.right) + 1;
    return x;
  }

  public void delete(Key key) {
    root = delete(root, key);
  }

  private Node delete(Node x, Key key) {
    if (x == null)
      return null;

    int cmp = key.compareTo(x.key);
    if (cmp < 0)
      x.left = delete(x.left, key); // delete left
    else if (cmp > 0)
      x.right = delete(x.right, key); // delete right
    else {
      if (x.right == null)
        return x.left;

      if (x.left == null)
        return x.right;

      Node t = x;
      x = min(t.right);
      x.right = deleteMin(t.right);
      x.left = t.left;
    }
    x.count = size(x.left) + size(x.right) + 1;
    return x;
  }

  public Iterable<Key> keys() {
    Queue<Key> q = new LinkedList<Key>();
    inorder(root, q);
    return q;
  }

  private void inorder(Node x, Queue<Key> q) {
    if (x == null)
      return;

    inorder(x.left, q);
    q.add(x.key);
    inorder(x.right, q);
  }

  public static void main(String[] args) {
    BinarySearchTree<String, Integer> bst = new BinarySearchTree<String, Integer>();

    bst.put("a", 1);
    bst.put("d", 1);
    bst.put("q", 1);
    bst.put("w", 1);
    bst.put("e", 1);
    bst.put("r", 1);
    bst.put("t", 1);
    bst.put("y", 1);
    bst.put("u", 1);
    bst.put("i", 1);
    bst.put("o", 1);
    bst.put("p", 1);
    bst.put("d", 1);
    bst.put("f", 1);
    bst.put("g", 1);
    bst.put("j", 1);

    for (String k : bst.keys())
      System.out.printf("%s (key) %s (Value) %n", k, bst.get(k));

    System.out.printf("%nMax key: %s - Min key: %s%n", bst.max(), bst.min());
    System.out.printf("Height of tree is %s%n", bst.height());
  }
}

Related Tutorials