Java Data Structures Binary Search Tree

Description

Java Data Structures Binary Search Tree


class TreeNode {
   public TreeNode(Object o) {
      left = right = null;//  w ww  .j  a v  a2  s .  c  o m
      data = o;
   }

   public Object getData() {
      return data;
   }

   public TreeNode getLeft() {
      return left;
   }

   public TreeNode getRight() {
      return right;
   }

   public void setData(Object o) {
      data = o;
   }

   public void setLeft(TreeNode l) {
      left = l;
   }

   public void setRight(TreeNode r) {
      right = r;
   }

   public String toString() {
      return "TreeNode " + data;
   }

   TreeNode left;
   TreeNode right;
   Object data;
}

interface Comparable {
   public int compare(Object a, Object b);
}

interface Traversal {
   public void process(Object o);
}

class Tree {
   public Tree(Comparable o) {
      c = o;
   }

   public void add(Object o) {
      add(root, new TreeNode(o));
   }

   protected void add(TreeNode root, TreeNode newNode) {
      if (root == null) {
         this.root = newNode;
         return;
      }

      int val = c.compare(newNode.getData(), root.getData());

      if (val == 0) {
         root.setData(newNode.getData());
         return;
      } else if (val < 0) {
         if (root.getLeft() == null)
            root.setLeft(newNode);
         else
            add(root.getLeft(), newNode);
      } else if (val > 0) {
         if (root.getRight() == null)
            root.setRight(newNode);
         else
            add(root.getRight(), newNode);
      }
   }

   public Object search(Object o) {
      return search(root, o);
   }

   protected Object search(TreeNode root, Object o) {
      if (root == null) {
         return null;
      }

      int val = c.compare(o, root.getData());

      if (val == 0) {
         return root.getData();
      } else if (val < 0) {
         return search(root.getLeft(), o);
      } else if (val > 0) {
         return search(root.getRight(), o);
      }
      return null;
   }

   public void traverse(Traversal t) {
      traverse(INORDER, t);
   }

   public void traverse(int type, Traversal t) {
      traverse(root, type, t);
   }

   protected void traverse(TreeNode root, int type, Traversal t) {
      TreeNode tmp;

      if (type == PREORDER)
         t.process(root.getData());

      if ((tmp = root.getLeft()) != null)
         traverse(tmp, type, t);

      if (type == INORDER)
         t.process(root.getData());

      if ((tmp = root.getRight()) != null)
         traverse(tmp, type, t);
   }

   protected TreeNode root;

   protected Comparable c;

   public final static int INORDER = 1;
   public final static int PREORDER = 2;

   protected final static int RIGHT = 1;
   protected final static int LEFT = 2;
}

public class Main {
   public static void main(String args[]) {
      Tree t = new Tree(new Comparable() {
         public int compare(Object a, Object b) {
            return ((String) a).compareTo((String) b);
         }
      });

      t.add("CSS");
      t.add("HTML");
      t.add("Java");
      t.add("Javascript");
      t.add("SQL");
      t.add("C++");
      t.traverse(new Traversal()
      {
         public void process(Object o) {
            System.out.println(o);
         }
      });

      System.out.println("SEARCH=" + t.search("C++"));
      System.out.println("SEARCH=" + t.search("C"));

   }
}



PreviousNext

Related