The two binary trees are representing.
- Sequential representation
- Linked representation
Sequential Representation:
- Nodes are sequentially arranged from the top to bottom from left to right.
- The numbering will start from the root node and then the remaining nodes will give ever-increasing numbering in a level-wise direction.
- Nodes will be the same level and the number will start from left to right.
- Sequential representation of frees is done using single- or one-dimensional arrays.
- The major disadvantage with this type of representation is the wastage of memory.
- The only advantage with this type of representation is that direct access to any node can be possible and finding the parents, left or right children of any node. (Because of the Random access)
The below figure represented the how the sequential representation of the binary tree.
2. Linked Representation of Binary Tree.
In linked representation of binary tree each node will have left child, right child, and data field, below are the one node structure of linked representation of the binary tree.
- The left child is nothing but the left link which points to some of the addresses of the left subtree.
- The right child also right link which points to some of the addresses of the right subtree.
- The Data field gives the information about the nodes.
Let’s see the below structure of linked representation of the binary tree.
Advantages of linked representation:
- Array representations there is no waste of memory, and so there is no need to have prior knowledge of the depth of the tree. Using the dynamic memory concept one can create as many memories (nodes) as required.
- Insertions and deletions which are the most common operations can be done without moving the other nodes.
Disadvantages of linked representation:
- This representation does not have direct access to the nodes and needs to have special algorithms.
- Need additional space in each node for storing the left and right sub-trees.
Reference Link for more about Binary Tree
Link