Is PHP SplHeap really a bunch?

Is the PHP heap implementation really a full version?

When I read this article, http://en.wikipedia.org/wiki/Heap_%28data_structure%29 , I realized that the child node has a specific parent, and that the parent has specific children.

However, when I look at an example in the PHP documentation, http://au.php.net/manual/en/class.splheap.php , it seems that the child nodes are at the same level, but the specific parent / child information is not is important.

For example, which node is the parent for each of the three nodes, which takes 10th place in the PHP example?

In my application, when the user selects "node 156", I need to know who his children are, so that I can pay for them every visit. (I could make them the identifiers node 1561 ',' node 1562 ', etc., so the relationship is obvious).

Is the PHP heap implementation incomplete? Should I forget the Spl class and go my own way? Or am I missing something about how heaps should work? Or maybe I should look at a specific heap option?

Thanks heaps!

+6
source share
2 answers

The API for this heap implementation does not allow access to the array, which is what you need in this case. Heaps are commonly used to implement other structures that make it easy to remove items from above.

You can create a wrapper iterator that searches for the positions you need. I suspect this will be a bad decision, not a very good one.


In this situation, a BinaryTree is what I think you need. Given the spot in the tree, you can go down with it to the children, because they are directly connected. I have a BinarySearchTree that saves the tree and you can call getRoot to get a copy of BinaryTree . There is also AvlTree , which will save the tree for an optimal search.

+8
source

The heap is usually used to answer questions that revolve around "what is the max / min element in the set."

While the heap could randomly efficiently answer โ€œwho are the children of the max / min nodeโ€, the heap does not stand out as a good data structure to use when you need to access an arbitrary node to answer your question (a node that doesn't is the maximum node). Also, you should not use a data structure if there are certain parent child relationships that need to be represented and maintained, as children can and can be replaced between different parent nodes.

So php heap implementation is definitely not incomplete for these reasons. You just need a different data structure.

+4
source

Source: https://habr.com/ru/post/922152/


All Articles