Don't like ads? PRO users don't see any ads ;-)
Guest

searchCode

By: a guest on Sep 24th, 2012  |  syntax: Java  |  size: 1.04 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.         public ComparableKeyValuePair search(Comparable key){
  3.                 TreeNode root = getRoot();
  4.                 if (root == null)
  5.                         return null;
  6.                 else return recursiveSearch(root, key);
  7.         }
  8.  
  9.         public TreeNode recursiveSearch(TreeNode node, Comparable key){
  10.                 System.out.println("hi"+node.toString());
  11.                 int compare = key.compareTo(node.key);
  12.                 if (compare == 0)
  13.                         return node;
  14.                 else if (compare < 0){
  15.                         if (node.left == null)
  16.                                 return null;
  17.                         else
  18.                                 return recursiveSearch(node.left, key);
  19.  
  20.                 }
  21.                 else if (compare > 0){
  22.                         if (node.right == null)
  23.                                 return null;
  24.                         else
  25.                                 return recursiveSearch(node.right, key);
  26.                 }
  27.                 return null;
  28.         }