Solved

count internal nodes in a tree

Posted on 2004-04-07
11
643 Views
Last Modified: 2008-03-10
write a program in 'C' language to count the number of internal nodes  of a tree.
0
Comment
Question by:jessu2607
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
sorry we cannot do your homework for you

make some efforts ... post your code and if you are stuck on something specific, we will help
0
 
LVL 4

Expert Comment

by:skypalae
Comment Utility
Is it binary tree? If yes, then number of internal nodes is allways one less then number of leaves.

Cheers! S.
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 125 total points
Comment Utility
The simplest solution would be a recursive function.
jessu2607,all this requires is one recursive fn. to be written.

Try it urself.As sunny said,if you are stuck,we'll be glad to help.
0
 
LVL 5

Expert Comment

by:twobitadder
Comment Utility
>>Is it binary tree? If yes, then number of internal nodes is allways one less then number of leaves.

Only if it is a complete and balanced tree.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 4

Expert Comment

by:skypalae
Comment Utility
twobitadder,

I don't get what you mean by complete, but definitely it doesn't need to be balanced tree

/\
 /\
  /\
   /\
    /\

is not balanced at all and it still has 5 internals and 6 leaves

and if complete means that there is not

/\
  \
  /\

that tree is (almost) for nothing and you can allways get complete by deleting the uncomplete internal

S.
0
 
LVL 5

Expert Comment

by:twobitadder
Comment Utility
>/\
> /\
>  /\
>   /\
>    /\
>
>is not balanced at all and it still has 5 internals and 6 leaves

    /\
  /\ /\
/

has 4 internals and 4 leaves

whilst
   /\
  /\/\
 /\
has 4 internals and 5 leaves
0
 
LVL 4

Expert Comment

by:skypalae
Comment Utility
>     /\
>   /\ /\
> /

is incomplete (as far as i understand your term 'completeness'). and it is balanced (since there are no 2 paths from root to leaf that would differ by 2). i still insist on that (complete) binary tree has allways one less internals than leaves. and on that incomplete trees are for nothing (but studying purposes).

S.
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
Full Binary Tree Theorem

Theorem:  The number of leaves in a non-empty full binary  tree is one more than the number of internal nodes.  

Full binary tree:  each node either is a leaf or is an internal node with exactly two non-empty children

For proof,see:http://www.cz3.nus.edu.sg/~chenk/cz2102/notes5_1.htm

BTW, a complete binary tree is:

Complete binary tree:  If the height of the tree is d, then all levels except possibly level d are completely full.  The bottom level has all nodes to the left side.
0
 
LVL 4

Expert Comment

by:Fippy_Darkpaw
Comment Utility
Here's an easy (not necessarily optimal) way:

- search every node in the tree
- if it has children then InternalNodeCount++
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now