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 - PHP Tutorial
From the course: Learning the Standard PHP Library
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
(Locked)
Doubly linked lists, stacks, and queues3m 10s
-
(Locked)
Sorting XML and JSON with SplDoublyLinkedList8m 26s
-
(Locked)
Using SplStack and SplQueue6m 25s
-
(Locked)
Understanding heaps2m 55s
-
(Locked)
SplMinHeap and SplMaxHeap5m 3s
-
(Locked)
Sorting XML and JSON with SplHeap7m 9s
-
(Locked)
Finding important information with SplPriorityQueue5m 32s
-
(Locked)
Keeping priority items in chronological order4m 23s
-
(Locked)
Speeding up array access with SplFixedArray6m 48s
-
(Locked)
-