MHB Finding Depth of Perfect & Complete Binary Trees

Click For 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?
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 65 ·
3
Replies
65
Views
10K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 40 ·
2
Replies
40
Views
7K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
14
Views
3K
  • · Replies 13 ·
Replies
13
Views
4K
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K