MHB Finding Depth of Perfect & Complete Binary Trees

AI Thread Summary
To find the depth of a perfect binary tree, the proposed algorithm suggests traversing the leftmost nodes until a null pointer is reached, incrementing a depth counter. This approach is valid for perfect binary trees, where all leaves are at the same depth. For complete binary trees, which may not have all leaves at the same depth, the same algorithm can be applied, but it may not yield accurate results if the tree structure varies. A full traversal is recommended for trees that do not guarantee uniform leaf depth. There is a clarification needed regarding the traversal method, specifically whether to use the left child pointer instead of the next pointer.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hi! (Wave)

How can we find the depth of a perfect binary tree and how of a complete one? (Thinking)

Since a perfect binary tree is a full binary tree, at which the leaves have the same depth, I thought that we can find the depth, by just looking at the leftmost nodes, like that:

Code:
Algorithm(NODE *tree){
pointer p=tree;
int depth=0;
while (p!=NULL){
         p=p->next;
         depth++;
} 
return depth;
}
A complete binary tree of height $h$ consists of a perfect binary tree of height $h-1$, at which, one or more leaves with height $h$, have been added.
The leaves have been placed at the leftmost positions of the tree.

I thought that we could use again the above algorithm.

Am I right or have I done someething wrong? (Thinking)
 
Technology news on Phys.org
evinda said:
Hi! (Wave)

How can we find the depth of a perfect binary tree and how of a complete one? (Thinking)

Since a perfect binary tree is a full binary tree, at which the leaves have the same depth, I thought that we can find the depth, by just looking at the leftmost nodes, like that:

Code:
Algorithm(NODE *tree){
pointer p=tree;
int depth=0;
while (p!=NULL){
         p=p->next;
         depth++;
} 
return depth;
}
A complete binary tree of height $h$ consists of a perfect binary tree of height $h-1$, at which, one or more leaves with height $h$, have been added.
The leaves have been placed at the leftmost positions of the tree.

I thought that we could use again the above algorithm.

Am I right or have I done someething wrong? (Thinking)

If the tree truly does have all its leaves at the same depth, this algorithm should work fine.

On the other hand, if you're not guaranteed a tree where all leaves are at the same depth, I don't see any way of computing the depth of the tree other than a full traversal.

[EDIT] You did mean
Code:
p = p->lc
, right?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top