Binary Tree Representation
Last Updated : 07 Oct, 2024
Binary tree is a tree data structure (non-linear) in which each node can have at most two children which are referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves. A binary tree can be visualized as a hierarchical structure with the root at the top and the leaves at the bottom.
Binary trees can be represented in multiple ways, each with its own advantages, depending on the use case. Let's explore the two common methods: linked node representation and array implementation.
Representation of Binary Trees
There are two primary ways to represent binary trees:
- Linked Node Representation
- Array Representation
1. Linked Node Representation
This is the simplest way to represent a binary tree. Each node contains data and pointers to its left and right children.
This representation is mostly used to represent binary tree with multiple advantages. The most common advantages are given below.
Advantages:
- It can easily grow or shrink as needed, so it uses only the memory it needs.
- Adding or removing nodes is straightforward and requires only pointer adjustments.
- Only uses memory for the nodes that exist, making it efficient for sparse trees.
Disadvantages:
- Needs extra memory for pointers.
- Finding a node can take longer because you have to start from the root and follow pointers.
Create/Declare a Node of a Binary Tree:
Syntax to declare a node of "linked list representation of binary tree" in different languages:
C++ // Use any below method to implement Nodes of binary tree // 1: Using struct struct Node { int data; Node* left, * right; Node(int val) { data = val; left = nullptr; right = nullptr; } }; // 2: Using class class Node { public: int data; Node* left, * right; Node(int val) { data = val; left = nullptr; right = nullptr; } };
C // Node structure of each node of the binary tree. struct Node { int data; struct Node* left; struct Node* right; }; // Note : Unlike other languages, C does not support // Object Oriented Programming. So we need to write // a separat method for create and instance of tree node struct Node* createNode(int val) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = val; newNode->left = NULL; newNode->right = NULL; return newNode; }
Java // Node structure of each node of the binary tree. class Node { int data; Node left, right; Node(int val) { data = val; left = null; right = null; } }
Python # Node structure of each node of the binary tree. class Node: def __init__(self, val): self.data = val self.left = None self.right = None
C# // Node structure of each node of the binary tree. class Node { public int data; public Node left, right; public Node(int val) { data = val; left = null; right = null; } }
JavaScript // Node structure of each node of the binary tree. class Node { constructor(val) { this.data = val; this.left = null; this.right = null; } }
For more detailed information about how it works, please check out our article "Introduction to Binary Tree".
2. Array Representation
Array Representation is another way to represent binary trees, especially useful when the tree is complete (all levels are fully filled except possibly the last, which is filled from left to right). In this method:
- The tree is stored in an array.
- For any node at index i:
- Left Child: Located at 2 * i + 1
- Right Child: Located at 2 * i + 2
- Root Node: Stored at index 0
Advantages:
- Easy to navigate parent and child nodes using index calculations, which is fast
- Easier to implement, especially for complete binary trees.
Disadvantages:
- You have to set a size in advance, which can lead to wasted space.
- If the tree is not complete binary tree then then many slots in the array might be empty, this will result in wasting memory
- Not as flexible as linked representations for dynamic trees.
For more detailed information about how it works, please check out our article "Binary Tree (Array implementation)".
Comparison of Linked list vs Array Representation of Binary Tree
Feature | Linked Node Representation | Array Representation |
---|
Memory Usage | Uses extra memory for pointers | Efficient for complete trees |
Ease of Implementation | Simple and intuitive | Simple but limited to complete trees |
Dynamic Size | Easily grows and shrinks | Not flexible for dynamic sizes |
Access Time | Slower access for specific nodes | Fast access using indices |
Suitable For | Any binary tree structure | Complete binary tree |
Similar Reads
Binary representation of a given number Given an integer n, the task is to print the binary representation of the number. Note: The given number will be maximum of 32 bits, so append 0's to the left if the result string is smaller than 30 length.Examples: Input: n = 2Output: 00000000000000000000000000000010Input: n = 0Output: 000000000000
6 min read
XOR counts of 0s and 1s in binary representation Given a number, the task is to find XOR of count of 0s and count of 1s in binary representation of a given number. Examples: Input : 5 Output : 3 Binary representation : 101 Count of 0s = 1, Count of 1s = 2 1 XOR 2 = 3. Input : 7 Output : 3 Binary representation : 111 Count of 0s = 0 Count of 1s = 3
4 min read
Implementation of compressed 2D Binary Indexed tree A compressed 2D Binary Indexed Tree (BIT) is a data structure that is used to efficiently store and query two-dimensional arrays, particularly when the values in the array are frequently updated. The compressed 2D Binary Indexed Tree (BIT) is a data structure used for fast querying and updating valu
15+ min read
What is Binary String? A binary string is a string that only has two characters, usually the numbers 0 and 1, and it represents a series of binary digits. Binary String Variables:In computer programming, binary string variables are used to store binary data, which is data that is represented in a binary (base-2) format, r
9 min read
Check if binary representations of two numbers are anagram Given two numbers you are required to check whether they are anagrams of each other or not in binary representation.Examples: Input : a = 8, b = 4 Output : Yes Binary representations of both numbers have same 0s and 1s. Input : a = 4, b = 5 Output : No Simple Approach: Find the Binary Representation
9 min read