Click to See Complete Forum and Search --> : how do i know binary tree is complete?


kidu
December 26th, 2005, 03:13 PM
Do You know any algo for checking a binary tree is complete or not?

Hnefi
December 26th, 2005, 03:48 PM
There are several ways with varying efficiency. One way is to do a levelorder traversal (from left to right), and if the traversal ends without two criteria being met, the tree is complete. The criteria are:
1: a node is found with a right child but no left child.
2: a node has been found with less than two children. At some point after that, a node with more than zero children is found.

If any of the above criteria is met, the algorithm should end with the conclusion that the tree is incomplete.

mehdi62b
December 27th, 2005, 08:11 AM
2: a node has been found with less than two children. At some point after that, a node with more than zero children is found.How do you write a recursivefuncon for this?

there is also another way which the structure of the tree could be converted to an array as it would be easy to determine its completeness in that case (this post (http://www.codeguru.com/forum/showpost.php?p=1244064&postcount=6))

Hnefi
December 27th, 2005, 11:24 AM
How do you write a recursivefuncon for this?
You don't, and I don't see why you'd want to. It wasn't part of the assignment. Levelorder is usually implemented with a queue, which means no recursion is needed. As for keeping track of the first part of criterium 2, a simple boolean variable will do.