There are two ways to represent a binary tree in Data Structure
- Array Representation
- Linked Representation
Array Representation
An Array is a linear data structure and for representing a binary tree as an array we have to create an array of a specific size. Let us consider the size of the array is 50.
The biggest demerit of array representation is we can't insert data after the array is full, i.e., we can't insert 51 elements in the array.
For that, we have to create another array of size 51 and copy all the previous elements to it and then insert the next element.
Linked Representation
This method of representing binary trees using linked nodes is considered the most efficient method of representation. For this, we use doubly-linked lists.
Each tree node has two pointers (usually named left and right). The tree class has a pointer to the root node of the tree (red colored in the diagram above).
Any pointer in the tree structure that does not point to a node will normally contain the value NULL. A linked tree with N nodes will always contain N + 1
Null links.