I was asked in a interview about the difference between node structures of Doubly linked list and binary Tree.
Doubly Linked List Struct
typedef struct
{
int data;
struct node * next;
struct node * prev;
}node;
Binary Tree Struct
typedef struct
{
int data;
struct node * left;
struct node * right;
}node;
- In Doubly linked list, we use pointers for traversing back and front in a linearly arranged list.
- But where as left & right pointers are used to access the left & right nodes.
I don’t find any difference in node structure except the way they are used. Could you please give me some differences???
I think you have answered your own questions. Other than the obvious differences in names of the pointers (
next/prevandleft/right), the differences are:n.nextlinks tomthenm.prevlinks ton. This is not the case in a binary tree.If the doubly linked list is also cyclic, the following also holds:
n.nextlinks tonandn.prevalso links ton. This is not possible in a binary tree:n.leftandn.rightdo not link to the same node.n.nextandn.prevcould point to no node (i.e., the tree is just a leaf node) but in a cyclic doubly linked list with one node both links always have a value (albeit to the same node).n.leftorn.right. If the binary tree is not balanced, it could be that all nodes have a value forn.rightbut not forn.left. This is not the case for a cyclic doubly linked list where all pointers have a value.In terms of structure and content the nodes are the same but the differences are in how the pointers are used and what their values can be.