binary tree
vertex parent
tree traversal
data structures
algorithms

Getting parent of a vertex in a perfect binary tree

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In computer science, a binary tree is a fundamental data structure in which each node has at most two children, referred to as the left child and the right child. A perfect binary tree is a specialized version where each internal node has exactly two children, and all leaf nodes are at the same depth level. This article discusses the process of finding the parent of a given vertex in a perfect binary tree, providing both intuitive insights and technical explanations.

Understanding Perfect Binary Trees

A perfect binary tree of height `h` has the following properties:

Total Number of Nodes: 2h+112^{h+1} - 1Leaf Nodes: 2h2^hInternal Nodes: 2h12^h - 1

Node Labeling

For convenience in discussing binary trees, especially when representing them in arrays, nodes are typically labeled sequentially starting from 1 (the root) up to 2h+112^{h+1} - 1.

Properties of Node Indices

In a 1-indexed perfect binary tree, commonly used for array representation:

• An element at an index `i` has its left child at index `2i` (if it exists), • The right child is at index `2i+1` (if it exists), • The parent node of an element at index `i` is located at `⌊i/2⌋`.

Let us delve further into how we calculate the parent node, along with a technical example.

Calculating the Parent of a Vertex

To find the parent of a vertex (node) in a perfect binary tree, particularly in an array representation, you apply the following formula:

parent(i)=i2\text{parent}(i) = \left\lfloor \frac{i}{2} \right\rfloor

Example

Consider a perfect binary tree with `7` nodes (height `h = 2`):

2 3 4 5 6 7

Node 4 (at index 4): Its parent is at 42=2\left\lfloor \frac{4}{2} \right\rfloor = 2. • Node 5 (at index 5): Its parent is at 52=2\left\lfloor \frac{5}{2} \right\rfloor = 2. • Node 6 (at index 6): Its parent is at 62=3\left\lfloor \frac{6}{2} \right\rfloor = 3. • Node 7 (at index 7): Its parent is at 72=3\left\lfloor \frac{7}{2} \right\rfloor = 3.

Time Complexity: Retrieving the parent of a node using the mathematical approach is O(1)O(1) because it involves basic arithmetic. • Space Complexity: As this method does not store any additional data except the input and output, its space complexity is O(1)O(1).


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.