Phương pháp chỉ nên thực hiện một việc tại một thời điểm. Ngoài ra cách bạn làm mọi thứ thường là Weird. Tôi sẽ cung cấp cho bạn một số mã giả gần như Java. Xin lỗi vì điều đó, nhưng tôi đã không chạm vào Java trong một thời gian. Tôi hy vọng nó sẽ giúp. Nhìn vào những bình luận tôi cũng đã làm trong câu hỏi và tôi hy vọng bạn sắp xếp nó ra!
Gọi isBST của bạn như thế:
public boolean isBst(BNode node)
{
return isBinarySearchTree(node , Integer.MIN_VALUE , Integer.MIN_VALUE);
}
Bên trong:
public boolean isBinarySearchTree(BNode node , int min , int max)
{
if(node.data < min || node.data > max)
return false;
//Check this node!
//This algorithm doesn't recurse with null Arguments.
//When a null is found the method returns true;
//Look and you will find out.
/*
* Checking for Left SubTree
*/
boolean leftIsBst = false;
//If the Left Node Exists
if(node.left != null)
{
//and the Left Data are Smaller than the Node Data
if(node.left.data < node.data)
{
//Check if the subtree is Valid as well
leftIsBst = isBinarySearchTree(node.left , min , node.data);
}else
{
//Else if the Left data are Bigger return false;
leftIsBst = false;
}
}else //if the Left Node Doesn't Exist return true;
{
leftIsBst = true;
}
/*
* Checking for Right SubTree - Similar Logic
*/
boolean rightIsBst = false;
//If the Right Node Exists
if(node.right != null)
{
//and the Right Data are Bigger (or Equal) than the Node Data
if(node.right.data >= node.data)
{
//Check if the subtree is Valid as well
rightIsBst = isBinarySearchTree(node.right , node.data+1 , max);
}else
{
//Else if the Right data are Smaller return false;
rightIsBst = false;
}
}else //if the Right Node Doesn't Exist return true;
{
rightIsBst = true;
}
//if both are true then this means that subtrees are BST too
return (leftIsBst && rightIsBst);
}
Bây giờ là: Nếu bạn muốn tìm Min
và Max
Giá trị của mỗi Subtree bạn nên sử dụng một container (Tôi đã sử dụng một ArrayList
) và lưu trữ một bộ ba của Node, Min, Max
đại diện cho nút gốc và các giá trị (rõ ràng).
ví dụ:
/*
* A Class which is used when getting subTrees Values
*/
class TreeValues
{
BNode root; //Which node those values apply for
int Min;
int Max;
TreeValues(BNode _node , _min , _max)
{
root = _node;
Min = _min;
Max = _max;
}
}
Và một:
/*
* Use this as your container to store Min and Max of the whole
*/
ArrayList<TreeValues> myValues = new ArrayList<TreeValues>;
Bây giờ đây là một phương pháp mà tìm Min
và Max
giá trị của một nút cho trước:
/*
* Method Used to get Values for one Subtree
* Returns a TreeValues Object containing that (sub-)trees values
*/
public TreeValues GetSubTreeValues(BNode node)
{
//Keep information on the data of the Subtree's Startnode
//We gonna need it later
BNode SubtreeRoot = node;
//The Min value of a BST Tree exists in the leftmost child
//and the Max in the RightMost child
int MinValue = 0;
//If there is not a Left Child
if(node.left == null)
{
//The Min Value is this node's data
MinValue = node.data;
}else
{
//Get me the Leftmost Child
while(node.left != null)
{
node = node.left;
}
MinValue = node.data;
}
//Reset the node to original value
node = SubtreeRoot; //Edit - fix
//Similarly for the Right Child.
if(node.right == null)
{
MaxValue = node.data;
}else
{
int MaxValue = 0;
//Similarly
while(node.right != null)
{
node = node.right;
}
MaxValue = node.data;
}
//Return the info.
return new TreeValues(SubtreeRoot , MinValue , MaxValue);
}
Nhưng điều này trả về giá trị cho một Node chỉ, Vì vậy, chúng tôi sẽ sử dụng điều này để tìm Cây nguyên thủy:
public void GetTreeValues(BNode node)
{
//Add this node to the Container with Tree Data
myValues.add(GetSubTreeValues(node));
//Get Left Child Values, if it exists ...
if(node.left != null)
GetTreeValues(node.left);
//Similarly.
if(node.right != null)
GetTreeValues(node.right);
//Nothing is returned, we put everything to the myValues container
return;
}
Sử dụng phương pháp này, cuộc gọi của bạn sẽ trông như thế
if(isBinarySearchTree(root))
GetTreeValues(root);
//else ... Do Something
này là gần như Java. Nó sẽ làm việc với một số sửa đổi và sửa chữa. Tìm một cuốn sách OO tốt, nó sẽ giúp bạn. Lưu ý rằng giải pháp này có thể được chia nhỏ thành nhiều phương pháp hơn.
@TimeToCodeTheRoad - ngôn ngữ được viết bằng ngôn ngữ nào? Sẽ rất hữu ích nếu bạn chỉnh sửa câu hỏi để gắn thẻ câu hỏi của mình một cách thích hợp. Ngoài ra, bạn nên sử dụng các nút mã thẻ ('{}') để định dạng mã của bạn để dễ đọc hơn. Cuối cùng, điều gì sai với mã của bạn? Chính xác thì chúng tôi đang "kiểm tra" là gì, bạn có gặp lỗi không? – LittleBobbyTables
đây là mã java và tôi đang kiểm tra xem BinaryNode v có đáp ứng các thuộc tính của cây tìm kiếm nhị phân – TimeToCodeTheRoad
@TimeToCode không, tại sao bạn trả về một cặp 'Pair()' ?? – Muggen