Finding Depth of Perfect & Complete Binary Trees

Click For Summary
SUMMARY

The discussion focuses on determining the depth of perfect and complete binary trees using a specific algorithm. The proposed algorithm iterates through the leftmost nodes of the tree to calculate depth, which is valid for perfect binary trees where all leaves are at the same depth. For complete binary trees, the algorithm may not suffice if the leaves are not uniformly distributed. The necessity of a full traversal for depth calculation in such cases is highlighted, along with a correction regarding the traversal method.

PREREQUISITES
  • Understanding of binary tree structures, specifically perfect and complete binary trees.
  • Familiarity with tree traversal algorithms, particularly depth-first traversal.
  • Knowledge of pointer manipulation in programming languages such as C or C++.
  • Basic algorithm analysis to evaluate time complexity of tree operations.
NEXT STEPS
  • Study the properties of perfect binary trees and their depth calculation methods.
  • Learn about complete binary trees and the implications of leaf distribution on depth calculation.
  • Explore depth-first search (DFS) algorithms for full tree traversal.
  • Implement and analyze the time complexity of various tree traversal algorithms.
USEFUL FOR

Computer science students, software developers, and algorithm enthusiasts looking to deepen their understanding of binary tree structures and traversal techniques.

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?
 

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
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K