Metoda powinna wykonywać tylko jedną rzecz naraz. Również sposób, w jaki robisz rzeczy, jest ogólnie dziwny. Dam ci trochę pseudokodowania prawie-Java. Przepraszam za to, ale przez jakiś czas nie dotykałem Javy. Mam nadzieję, że to pomoże. Zobacz komentarze, które również zrobiłem w sprawie pytania i mam nadzieję, że to załatwisz!
Zadzwoń do isBST tak:
public boolean isBst(BNode node)
{
return isBinarySearchTree(node , Integer.MIN_VALUE , Integer.MIN_VALUE);
}
Wewnętrznie:
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);
}
Teraz: Jeśli chcesz znaleźć Min
i Max
wartości każdego poddrzewo należy użyć pojemnika (użyłem ArrayList
) i przechowaj triplet z Node, Min, Max
, który reprezentuje węzeł główny i wartości (oczywiście).
np.
/*
* 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;
}
}
I:
/*
* Use this as your container to store Min and Max of the whole
*/
ArrayList<TreeValues> myValues = new ArrayList<TreeValues>;
Teraz jest to metoda, która wyszukuje wartości Min
i Max
danego węzła:
/*
* 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);
}
Ale ta zwraca wartości tylko dla jednego węzła, Więc użyjemy tego, aby znaleźć dla całego drzewa:
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;
}
Używając tej metody, połączenie powinno wyglądać
if(isBinarySearchTree(root))
GetTreeValues(root);
//else ... Do Something
To prawie Java. Powinien działać z pewnymi modyfikacjami i poprawkami. Znajdź dobrą książkę OO, to ci pomoże. Zauważ, że to rozwiązanie może być zepsute na więcej metod.
@TimeToCodeTheRoad - w jakim języku się to pisze? Byłoby niezwykle pomocne, jeśli edytujesz swoje pytanie, aby odpowiednio oznaczyć pytanie tagiem. Powinieneś również użyć przycisków znaczników kodu ('{}'), aby sformatować swój kod w celu łatwiejszej czytelności. Na koniec, co jest nie tak z twoim kodem? Do czego dokładnie "sprawdzamy", czy otrzymujesz błąd? – LittleBobbyTables
to jest kod java, i sprawdzam, czy BinaryNode v spełnione właściwości binarnego drzewa wyszukiwania – TimeToCodeTheRoad
@TimeToCode, dlaczego zwracasz 'Pair()' ?? – Muggen