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;
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/prev
and left/right
), the differences are:
n.next
links to m
then m.prev
links to n
. This is not the case in a binary tree.If the doubly linked list is also cyclic, the following also holds:
n.next
links to n
and n.prev
also links to n
. This is not possible in a binary tree: n.left
and n.right
do not link to the same node.n.next
and n.prev
could 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.left
or n.right
. If the binary tree is not balanced, it could be that all nodes have a value for n.right
but not for n.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With