0

I have this question that in the heap data structure , a left child can be more than a right child in its own level ? I mean that consider these three numbers 9,5,8 and I want to make a max-heap data structure so the root will be 9 and is it true that 8 be its left child and 5 be its right child? please help me thanks

user355002
  • 871
  • 2
  • 10
  • 22

2 Answers2

5

Max-Heap properties:

  1. Root is the max element. O(1) time to search for max element.
  2. Children is always smaller than root of any sub-tree.(there is no conditions between left and right children)
  3. Minimum element lies somewhere in the leaf elements, i.e. O(n/2) == O(n) time is needed to find the minimum element.

Min-Heap properties:

  1. Root is the min element. O(1) time to search for mim element.
  2. Children is always greater than root of any sub-tree.(there is no conditions between left and right children)
  3. Maximum element lies somewhere in the leaf elements, i.e. O(n/2) == O(n) time is needed to find the maximum element.

Hence, Heap is not suitable for searching but it is used for sorting an array of elements, because searching takes linear O(n) time.

For searching, we could always go for Binary Search trees(BSTs) which does the same thing in O(h) time. And in best case, it would do the searching in O(logn) if the BS tree is balanced.

CODError
  • 779
  • 2
  • 10
  • 25
3

That doesn't matter. A node in a max-heap must have children that are lower, and a node in a min-heap must have children that are greater. Those are the only requirements.

Daniel Egeberg
  • 8,359
  • 31
  • 44
  • so there is no rule for how we set the left and right child of a parent?because heap is nearly complete binary tree and I think it was a rule in the binary tree that left child should be less that right child!and I think for my above example I should write "root:9" and "leftChild:5" and "rightChild:8",isn't? – user355002 Jun 25 '10 at 07:16
  • The way you would typically build a max heap is to start in the middle of the array downto the first element and recursively let each node sift down through the tree by exchanging nodes. This can be done in O(n) time. It isn't a binary search tree, so there is no particular relation between sibling nodes besides the fact that they're both smaller (or greater in the case of a min-heap) than their parent. – Daniel Egeberg Jun 25 '10 at 07:25
  • aha and if we want to search a key in this data structure we don't need to make it as a binary search tree ? – user355002 Jun 25 '10 at 07:45
  • Heaps aren't really suited for searching. Searching in a heap is a O(n) operation, so it's no better than an array or list. If you want to search then a binary search tree would be better because you can search in O(h) time (where h is the height). For instance, a red-black tree has height O(lg n), so you can search in logarithmic time. – Daniel Egeberg Jun 25 '10 at 07:55
  • for example if I want to search for minimum in the Max heap so I need my tree to be like a binary search tree? – user355002 Jun 25 '10 at 08:10
  • If you need to find minimum elements, using a min-heap would be better as it would be the root. What exactly is it you're trying to do? Why do you want to use a heap? In general, if it's searching you need the a heap is not the appropriate data structure. – Daniel Egeberg Jun 25 '10 at 08:28