void BST(tree T) { if (T == null) return if ( T.left and T.right) { if (T.left.data < T.data or T.right.data > T.data) { count = count + 1 BST(T.left) BST(T.right) } } }
But I can’t really figure this one out. I know that this algorithm won’t solve the problem because the count will be zero if the second if statement isn’t true. Could anyone help me out on this one?
Asked By : OghmaOsiris
Answered By : Gilles
if (T.left and T.right) { count = count + 1
Furthermore, you need to explore the left subtree even if the right subtree is empty, and you need to explore the right subtree even if the left subtree is empty. So watch where you put the recursive calls. To test whether the tree is a search tree, you need to inspect the data values. You’ve already got something close to the right comparison; not quite right. Write a few example trees with various shapes (not very big, 2 to 5 nodes) and run your algorithm on them to see what happens. You still need to find some place to put the result of the validity check. Again, watch where you put the recursive calls (if you only do this part, there are several solutions, but at this stage don’t worry if you only see one). Finally, once you’ve managed to write both functions separately, and you’ve tested them on a few examples, put them together carefully (if required by the assignment).
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/105