Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1567
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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