Solved

Check Complete Binary Tree in java

Posted on 2008-06-17
3
1,532 Views
Last Modified: 2010-10-05
How would i check in a recursive way if a given java tree where nodes are called p is binary complete??
0
Comment
Question by:axtur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Christopher Kile earned 500 total points
ID: 21806819
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
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 21806852
I just realized that this sounds like a homework assignment.  If it is, the Moderators may feel free to delete this.
0
 

Author Comment

by:axtur
ID: 21807991
It isn't homework assignment, its the answer to a question in an exam from the last year.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question