In: Computer Science
Does the order of arrival of data play a role in the pattern of data stored in a Heap?
Yes of course because Heap can either be min heap or max heap. In min heap child of a parent node should be greater than their parents and in max heap the child of parent node will less than its parent node.
But as we know that Heap is a binary tree so it can have maximum two children i.e. the left child and the right child. So, both the children can be at any position and that will be decided by their order of arrival.
For example if we have 1,2,3 to insert in min heap then
1 will be parent because it is minimum.
2 will be left child of the node containing 1.
And 3 will be it's right child.
But if we insert in the sequence 1,3,2 then
1 will be at root node.
But now 3 will be at left child of it's parent i.e. node containing 1
And 2 will be it's right child.
Yes of course because Heap can either be min heap or max heap. In min heap child of a parent node should be greater than their parents and in max heap the child of parent node will less than its parent node.
But as we know that Heap is a binary tree so it can have maximum two children i.e. the left child and the right child. So, both the children can be at any position and that will be decided by their order of arrival.
For example if we have 1,2,3 to insert in min heap then
1 will be parent because it is minimum.
2 will be left child of the node containing 1.
And 3 will be it's right child.
But if we insert in the sequence 1,3,2 then
1 will be at root node.
But now 3 will be at left child of it's parent i.e. node containing 1
And 2 will be it's right child.
And we know Generally that the elements are stored in a memory in a level order manner i.e. level by level and from left to right so the location of elements in both above sequences will be different.
And we know Generally that the elements are stored in a memory in a level order manner i.e. level by level and from left to right so the location of elements in both above sequences will be different.