Good day, please help solve this problem:

Write a generic class called a pyramid (or heap) that stores elements of an arbitrary reference type. The elements are organized in the form of a binary tree, but are stored in an array of size 2 ^ h - 1, where h is the height of the tree (must be specified in the constructor). Initially, the array elements are equal to the zero reference and are considered empty. Items are added in the same way as in a binary search tree. The class must contain methods: Add - add an element with the specified value to the pyramid, • Exist - check whether the pyramid contains an element with the specified value.

How do I set the size? here is an example: https://habrahabr.ru/post/112222/ but there the size is different and I don’t really understand how and where it is set. Can you help ?

  • By the way, проверить, содержит ли пирамида элемент с указанным значением this is not a standard heap operation at all. So non-standard that I would start a separate map for it. And by the way, why bother if all the operations are to add and check if there is an element. This is set ... - pavel

1 answer 1

The article is a bunch of dynamic size. You are also asked to make a tree on top of the array. In a sense, this is a bicycle - you first have to write your memory manager on top of the array (in fact, implement std :: vector from C ++), and then, using this manager, make a tree.

  • But is there some example or something like that? for I do not imagine Pts how to implement it: ( - Lolidze
  • I would say that the vector is not needed here, it’s enough to support the number of elements / pointer to the last one. - pavel
  • @pavel, and what, to implement std :: vector you need to store something else? What exactly? - gbg
  • one
    @Lolidze and how do you imagine it for any type? At a minimum, it is necessary for the comparison operation to be defined. - pavel
  • one
    @Lolidze e-maxx.ru/algo/treap here is the most suitable for your question. If in literature ask. - pavel