-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.php
More file actions
130 lines (124 loc) · 3.09 KB
/
heap.php
File metadata and controls
130 lines (124 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
class Solutionff
{
public $heap;
public function __construct($H)
{
$this->heap = $H;
}
public function left($i)
{
return (int)($i * 2);
}
public function right($i)
{
return (int)($i * 2 + 1);
}
public function parent($i)
{
return (int)($i / 2);
}
function max_heapify($i, $heap_size)
{
$largest = $i;
$l = $this->left($i);
$r = $this->right($i);
if ($l < $heap_size && $this->heap[$l] > $this->heap[$largest]) {
$largest = $l;
}
if ($r < $heap_size && $this->heap[$r] > $this->heap[$largest]) {
$largest = $r;
}
if ($i == $largest) {
return;
}
$temp = $this->heap[$i];
$this->heap[$i] = $this->heap[$largest];
$this->heap[$largest] = $temp;
$this->max_heapify($largest, $heap_size);
}
public function is_max_heap()
{
$len = count($this->heap);
for ($i = $len - 1; $i >= 1; $i++) {
$p = $this->parent($i);
if ($this->heap[$i] > $this->heap[$p] && $p>=1) {
return 'no';
}
return 'yes';
}
}
public function build_heap()
{
$len = count($this->heap);
for ($i = $len - 1; $i >= 1; $i--) {
$this->max_heapify($i, $len);
}
}
public function heap_sort(){
$len=count($this->heap);
for ($i=$len-1; $i >=1 ; $i--) {
$max_i=$this->heap[1];
$this->heap[1]=$this->heap[$i];
$this->heap[$i]=$max_i;
$len--;
$this->max_heapify(1, $len);
}
}
public function print_heap()
{
for ($i = 1; $i < count($this->heap); $i++) {
echo $this->heap[$i] . " ";
}
}
public function get_maximum(){
return $this->heap[1];
}
public function extract_max(){
$mx = $this->heap[1];
$this->heap[1]=$this->heap[count($this->heap)-1];
array_pop($this->heap);
$this->max_heapify(1,count($this->heap)-1);
return $mx;
}
public function insert_node($node)
{
$heap_size= count($this->heap) - 1;
$heap_size+=1;
$this->heap[$heap_size]=$node;
$i=$heap_size;
$p=$this->parent($i);
while($i>1 && $this->heap[$i]>$this->heap[$p]){
$p = $this->parent($i);
$t=$this->heap[$i];
$this->heap[$i]=$this->heap[$p];
$this->heap[$p]=$t;
$i=$p;
}
}
}
$x = [0,19,10,17,5,7,12,1,2,6];
$obj = new solutionff($x);
$obj->build_heap();
// $obj->heap_sort();
$obj->print_heap();
echo "<br>";
echo $obj->is_max_heap();
echo "<br>";
// echo $obj->extract_max(); echo "<br>";
// $obj->print_heap();
// echo "<br>";
// echo $obj->extract_max();
// echo $obj->extract_max() ;
// echo "<br>";
// $obj->print_heap();
// echo "<br>";
$obj->insert_node(20);
$obj->insert_node(21);
$obj->insert_node(26);
$obj->insert_node(6);
// $obj->heap_sort();
$obj->print_heap();
echo "<br>";
echo $obj->is_max_heap();
echo "<br>";