checking the order of binarytree

I get stuck while I write codes about checking the order of binarytree.
here is what i got so far,

bool BinaryTree<int>::isOrdered(const BinaryTree& root)
{
bool order= false;

if(root->left != NULL)
{
isOrdered(root->left);
order = root > root-> left;
if(!order) return false;
}

else if(root->right !=NULL)
{
isOrdered(root->right);
order = root < root->right;
if(!order) return false;
}

else
return true;

return order;
}

There can be several errors but what I think the most problematic one is that it only compares the values of parent and child.

Actually I though about creating couple other helper functions to hold values.
but I wonder that there can be more efficient way.

Can you give me some ideas doing this? and also please let me know if there are other crucial errors

Thanks
Last edited on
"Ordered" as in BST? http://en.wikipedia.org/wiki/Binary_search_tree

Note: use of code tags makes commenting easier.

1
2
if (root->left != NULL) ...
else if (root->right !=NULL) ...

You do consider the right subtree only if the left subtree is empty.

1
2
3
4
5
bool BinaryTree<int>::isOrdered(const BinaryTree& root)
...
(root->left != NULL)
...
isOrdered(root->left);

Pointers are usually compared against value of NULL (whatever that is here), but const references are not.

Calling isOrdered(root->left); does nothing obvious, because its return value is not used.


Back to BST. There are three values of interest in a subtree of a potential BST:
* Is the subtree a valid BST?
* What are the smallest and largest keys of the subtree?

There are more than one way to traverse a tree.
Topic archived. No new replies allowed.