這是我在BST中使用兩種不同方式查找最小搜索關鍵字的實現,我想確保我是否正確地做到了這一點:查找二叉搜索樹中的最小元素(迭代和遞歸)
迭代
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(); }
遞歸
public T findSmallest (BinaryNode Node)
{ if (Node == null) return null;
if(Node.getLeftChild()==null) return Node.getData();
else
return findSmallest ((Node.getLeftChild()) ; }
一個測試? – thegrinner
除了語法錯誤,命名約定和代碼格式,一切都應該沒問題 – jlordo
你的單元測試說什麼?他們通過了嗎?請尊重Java命名約定。變量以小寫字母開頭。 –