• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1553
  • Last Modified:

Check Complete Binary Tree in java

How would i check in a recursive way if a given java tree where nodes are called p is binary complete??
0
axtur
Asked:
axtur
  • 2
1 Solution
 
Christopher KileCommented:
The recursion is:

Function BinaryComplete(p) As Boolean

    if Not(HasRightChild(p) or HasLeftChild(p)) then
        return True
    else if HasRightChild(p) And HasLeftChild(p)
        return BinaryComplete(LeftChild(p)) And BinaryComplete(RightChild(p))
    else 'only one child exists, the tree is not binary-complete
        return False
    end if
 
End Function

I used functional form, but LeftChild(p) is most likely to be p.leftChild in the actual implmentation, and HasRightChild(p) is likely to be implemented as the Boolean expression p.rightChild is Nothing.
0
 
Christopher KileCommented:
I just realized that this sounds like a homework assignment.  If it is, the Moderators may feel free to delete this.
0
 
axturAuthor Commented:
It isn't homework assignment, its the answer to a question in an exam from the last year.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now