From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

SplMinHeap and SplMaxHeap

SplMinHeap and SplMaxHeap - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

SplMinHeap and SplMaxHeap

- The SplMinHeap and SplMaxHeap classes automatically compare values as they're added to the heap. With SplMinHeap, the smallest value rises to the top. With SplMaxHeap, it's the largest value. To show you how they work I've opened min_maxheap.php, which you can find in the chapter seven folder of the exercise files. On lines two and three is an array called animals with seven names in random order. Line five creates an instance of SplMinHeap, and the foreach loop on the following lines uses the insert method to insert each item in the animal array to the heap. Lines 10 to 13 create an instance of MaxHeap and insert the animals in the same way, then printr is used to inspect both heaps. So let's load the page into a browser to see the contents. I'll just expand that for a moment, and this shows us that we have got MinHeap and MaxHeap objects. They've got private properties, flags is set to zero, that means no flags have been set, is corrupted has no value, that means the heap is not…

Contents