0
  1. What is the run-time associated with (Max-heapify) that is implemented using k-ary heap.
  2. Is a k-ary heap more efficient than a binary heap asymptotically speaking?
  3. Is a k-ary heap more efficient than a binary heap in practice?
  4. can a search tree be implemented as k-arry?
Omar Kayali
  • 113
  • 1
  • 2
  • 7

1 Answers1

0

You've asked a lot of questions, so I'll try to answer all of them in turn.

  1. The runtime of the heapify operation on a k-ary heap is O(n), which is independent of k. This isn't immediately obvious, but most introductory algorithms textbooks have a proof of this result for the case where k = 2.

  2. Let's do the analysis for a k-ary heap in general, which we can then compare against a binary heap by just setting k = 2. In a k-ary heap, the cost of a find-min operation is O(1) (just look at the top of the heap) and the cost of a heapify operation is O(n), as mentioned above. When adding a new element to a k-ary heap, the runtime is proportional to the height of the heap, which is O(logk n) = O(log n / log k) (that follows from using the change-of-base formula for logarithms). It's not common to include the base of a logarithm inside big-O notation, but in this case because k is a parameter we can't ignore its contribution. In an extract-min operation, we need to work from the top of the tree down to the bottom. At each level, we look at up to k children of the current node to find the largest, then potentially do a swap down. This means that there is O(k) work per layer and there are O(log n / log k) layers, so the work done is O(k log n / log k). Asymptotically, for any fixed k, the runtimes of these operations are O(1), O(n), O(log n), and O(log n), respectively, so there's no asymptotic difference between a k-ary heap and a binary heap.

  3. In practice, though, there are differences. One good way to see this is to make k really, really big (say, 10100). In that case, the cost of a deletion will be quite large because there will be up to 10100 children per node, which will dwarf the height of the corresponding binary tree. For middling values of k (k = 3 or 4), there's a chance that it may actually be faster to use a 3-ary or 4-ary tree over a binary tree, but really the best way to find out would be to profile it and see what happens. The interactions of factors like locality of reference, caching, and division speed will all be competing with one another to affect the runtime.

  4. Yes! There are such things as multiway search trees. One of the most famous of these is the B-tree, which is actually a pretty fun data structure to read up on.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065