Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.69 KB | None | 0 0
  1.  
  2.  class BinarySearchtree {
  3.      class BSTNode
  4.      {
  5.          BSTNode left, right;
  6.          int data;
  7.      
  8.          /* Constructor */
  9.          public BSTNode()
  10.          {
  11.              left = null;
  12.              right = null;
  13.              data = 0;
  14.          }
  15.          /* Constructor */
  16.          public BSTNode(int n)
  17.          {
  18.              left = null;
  19.              right = null;
  20.              data = n;
  21.          }
  22.          /* Function to set left node */
  23.          public void setLeft(BSTNode n)
  24.          {
  25.              left = n;
  26.          }
  27.          /* Function to set right node */
  28.          public void setRight(BSTNode n)
  29.          {
  30.              right = n;
  31.          }
  32.          /* Function to get left node */
  33.          public BSTNode getLeft()
  34.          {
  35.              return left;
  36.          }
  37.          /* Function to get right node */
  38.          public BSTNode getRight()
  39.          {
  40.              return right;
  41.          }
  42.          /* Function to set data to node */
  43.          public void setData(int d)
  44.          {
  45.              data = d;
  46.          }
  47.          /* Function to get data from node */
  48.          public int getData()
  49.          {
  50.              return data;
  51.          }    
  52.      }
  53.      
  54.      /* Class BST */
  55.      class BST
  56.      {
  57.          private BSTNode root;
  58.      
  59.          /* Constructor */
  60.          public BST()
  61.          {
  62.              root = null;
  63.          }
  64.          /* Function to check if tree is empty */
  65.          public boolean isEmpty()
  66.          {
  67.              return root == null;
  68.          }
  69.          /* Functions to insert data */
  70.          public void insert(int data)
  71.          {
  72.              root = insert(root, data);
  73.          }
  74.          /* Function to insert data recursively */
  75.          private BSTNode insert(BSTNode node, int data)
  76.          {
  77.              if (node == null)
  78.                 node = new BSTNode(data);
  79.              else
  80.              {
  81.                  if (data <= node.getData())
  82.                      node.left = insert(node.left, data);
  83.                  else
  84.                      node.right = insert(node.right, data);
  85.              }
  86.              return node;
  87.          }
  88.          /* Functions to delete data */
  89.          public void delete(int k)
  90.          {
  91.              if (isEmpty())
  92.                  System.out.println("Tree Empty");
  93.              else if (search(k) == false)
  94.                  System.out.println("Sorry "+ k +" is not present");
  95.              else
  96.              {
  97.                  root = delete(root, k);
  98.                  System.out.println(k+ " deleted from the tree");
  99.              }
  100.          }
  101.          private BSTNode delete(BSTNode root, int k)
  102.          {
  103.              BSTNode p, p2, n;
  104.              if (root.getData() == k)
  105.              {
  106.                  BSTNode lt, rt;
  107.                  lt = root.getLeft();
  108.                  rt = root.getRight();
  109.                  if (lt == null && rt == null)
  110.                      return null;
  111.                  else if (lt == null)
  112.                  {
  113.                      p = rt;
  114.                      return p;
  115.                  }
  116.                  else if (rt == null)
  117.                  {
  118.                      p = lt;
  119.                      return p;
  120.                  }
  121.                  else
  122.                  {
  123.                      p2 = rt;
  124.                      p = rt;
  125.                      while (p.getLeft() != null)
  126.                          p = p.getLeft();
  127.                      p.setLeft(lt);
  128.                      return p2;
  129.                  }
  130.              }
  131.              if (k < root.getData())
  132.              {
  133.                  n = delete(root.getLeft(), k);
  134.                  root.setLeft(n);
  135.              }
  136.              else
  137.              {
  138.                  n = delete(root.getRight(), k);
  139.                  root.setRight(n);            
  140.              }
  141.              return root;
  142.          }
  143.          /* Functions to count number of nodes */
  144.          public int countNodes()
  145.          {
  146.              return countNodes(root);
  147.          }
  148.          /* Function to count number of nodes recursively */
  149.          private int countNodes(BSTNode r)
  150.          {
  151.              if (r == null)
  152.                  return 0;
  153.              else
  154.              {
  155.                  int l = 1;
  156.                  l += countNodes(r.getLeft());
  157.                  l += countNodes(r.getRight());
  158.                  return l;
  159.              }
  160.          }
  161.          /* Functions to search for an element */
  162.          public boolean search(int val)
  163.          {
  164.              return search(root, val);
  165.          }
  166.          /* Function to search for an element recursively */
  167.          private boolean search(BSTNode r, int val)
  168.          {
  169.              boolean found = false;
  170.              while ((r != null) && !found)
  171.              {
  172.                  int rval = r.getData();
  173.                  if (val < rval)
  174.                      r = r.getLeft();
  175.                  else if (val > rval)
  176.                      r = r.getRight();
  177.                  else
  178.                  {
  179.                      found = true;
  180.                      break;
  181.                  }
  182.                  found = search(r, val);
  183.              }
  184.              return found;
  185.          }
  186.          /* Function for inorder traversal */
  187.          public void inorder()
  188.          {
  189.              inorder(root);
  190.          }
  191.          private void inorder(BSTNode r)
  192.          {
  193.              if (r != null)
  194.              {
  195.                  inorder(r.getLeft());
  196.                  System.out.print(r.getData() +" ");
  197.                  inorder(r.getRight());
  198.              }
  199.          }
  200.          /* Function for preorder traversal */
  201.          public void preorder()
  202.          {
  203.              preorder(root);
  204.          }
  205.          private void preorder(BSTNode r)
  206.          {
  207.              if (r != null)
  208.              {
  209.                  System.out.print(r.getData() +" ");
  210.                  preorder(r.getLeft());            
  211.                  preorder(r.getRight());
  212.              }
  213.          }
  214.          /* Function for postorder traversal */
  215.          public void postorder()
  216.          {
  217.              postorder(root);
  218.          }
  219.          private void postorder(BSTNode r)
  220.          {
  221.              if (r != null)
  222.              {
  223.                  postorder(r.getLeft());            
  224.                  postorder(r.getRight());
  225.                  System.out.print(r.getData() +" ");
  226.              }
  227.          }    
  228.      } 
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement