Open In App

Binary Tree Representation

Last Updated : 07 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Linked Node Representation
  2. 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.

Binary-Tree-Representation

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
Array-Representation-of-Binary-Tree

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

FeatureLinked Node RepresentationArray Representation
Memory UsageUses extra memory for pointersEfficient for complete trees
Ease of ImplementationSimple and intuitiveSimple but limited to complete trees
Dynamic SizeEasily grows and shrinksNot flexible for dynamic sizes
Access TimeSlower access for specific nodesFast access using indices
Suitable ForAny binary tree structureComplete binary tree



Next Article
Article Tags :
Practice Tags :

Similar Reads