請在下面找到一個簡單的二叉搜索樹檢查代碼:我應該多長時間在二叉樹中添加條目?
class Tree {
int value;
Tree left;
Tree right;
public Tree (int a){
value = a;
left = right = null;
}
}
public class VerifyBST {
public static boolean ifBST(Tree myTree, int small , int large){
if(myTree == null)
return true;
if(myTree.value > small && myTree.value < large){
boolean leftBST = ifBST(myTree.left, small,myTree.value);
boolean rightBST = ifBST(myTree.right,myTree.value,large);
return leftBST&&rightBST;
}
else{
return false;
}
}
public static void main(String[] args) {
/*
4
/\
2 6
/\ /\
1 3 5 7 */
Tree myTree = new Tree(4);
myTree.left = new Tree(2);
myTree.right = new Tree(6);
myTree.left.left = new Tree(1);
myTree.left.right = new Tree(3);
myTree.right.left = new Tree(5);
myTree.right.right = new Tree(7);
System.out.println("BST or NOT?" + ifBST(myTree,Integer.MIN_VALUE,Integer.MAX_VALUE));
}
}
我的問題:
從代碼作爲清楚,我手動輸入我的二叉樹的所有條目,所以如果有一種情況我需要檢查那些手動輸入不是好主意的大樹,那麼應該遵循什麼樣的最佳方法呢?
由於我在主要方法中通過了
ifBST(myTree,Integer.MIN_VALUE,Integer.MAX_VALUE)
,這是否意味着Integer.MIN_VALUE = 1
和Integer.MAX_VALUE = 7
被傳遞給方法體呢?
感謝