0

This is my implementation of finding the smallest search key in a BST using two different ways and I'd like to make sure if I'm doing it correctly:

Iterative

public T findSmallest ( BinarySearchTree<T> tree )

{ BinaryNode Node = new BinaryNode (tree.getDataRoot);

  if ( Node == null ) return null;

  while(Node.hasLeftChild()) Node = Node.getLeftChild;

return Node.getData(); } 

Recursively

public T findSmallest ( BinaryNode Node ) 

{ if (Node == null) return null;

  if(Node.getLeftChild()==null) return Node.getData(); 

  else 

  return findSmallest ( (Node.getLeftChild()) ; } 
GeekFlow
  • 13
  • 1
  • 5
  • If you want to know if you're right, why don't you run a test? – thegrinner Apr 12 '13 at 12:53
  • 1
    Aside from syntax errors, naming conventions and code formatting, everything should be ok – jlordo Apr 12 '13 at 12:53
  • 2
    What do your unit tests say? Do they pass? Please respect the Java naming conventions. Variables start with a lower-case letter. – JB Nizet Apr 12 '13 at 12:53
  • 2
    You may prefer to try http://codereview.stackexchange.com. Stack Overflow is a site for answering specific programming questions, but not for reviewing implementations or discussing code quality. I have voted to close this question. – Duncan Jones Apr 12 '13 at 13:08
  • thank you Duncan will ask there too now .. – GeekFlow Apr 12 '13 at 13:10

1 Answers1

0

It looks good, i reformatted the code a bit, hope i did not miss anything.

Iterative

public T FindSmallest(BinarySearchTree<T> tree){ 
  BinaryNode Node = new BinaryNode(tree.getDataRoot());
  if (Node == null) 
    return null;

  while(Node.hasLeftChild()) 
    Node = Node.getLeftChild();

  return Node.getData(); 
}

Recursive

public T FindSmallest(BinaryNode Node){ 
  if (Node == null) 
    return null;

  if(Node.getLeftChild()==null) 
    return Node.getData(); 

  return findSmallest(Node.getLeftChild()); 
} 

This may interest you: Find kth smallest element in a binary search tree in Optimum way

Community
  • 1
  • 1
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • @Sara: I hope this is not your actual code, because (due to syntax errors) it won't compile. – jlordo Apr 12 '13 at 13:24
  • @TwoMore Both methods won't compile as is. In the first, it has the wrong return type, the second one also, and the second one is missing a pair of brackets in the last line... – jlordo Apr 12 '13 at 13:31
  • Thank You to you too @jlordo i will modify if right now – GeekFlow Apr 12 '13 at 13:38