This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Node { | |
public $data; | |
public $next; | |
public function __construct($data) | |
{ | |
$this->data = $data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// I could use array_push and array_pop with an array, as PHP already implements internally a stack with a doubly linked list | |
class Node { | |
public $data; | |
public $next; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// check if a binary tree is balanced (it means | max-depth - min-depth | <= 1) as an AVL tree | |
// time complexity is O(N) , where N is the number of nodes | |
// space complexity iis O(h) , where h is the number of levels | |
class Node { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Node { | |
public $data; | |
public $next; | |
public function __construct($data) | |
{ | |
$this->data = $data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class GraphNode { | |
public $data; | |
public $adiacent_nodes; | |
public function __construct($data, $adiacent_nodes) | |
{ | |
$this->data = $data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Node { | |
public $data; | |
public $next; | |
public function __construct($data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function mergeSort($array) { | |
if( sizeof($array) == 1 ) return $array; | |
$middle = floor(sizeof($array) / 2); | |
$l_arr = mergeSort( array_slice( $array, 0, $middle)); | |
$r_arr = mergeSort( array_slice( $array, $middle)); | |
$l_ptr = $r_ptr = $d_ptr = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function quickSort($array) { | |
if(sizeof($array) <= 1) return $array; | |
$pivot = $array[0]; | |
$left = $right = array(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function bubbleSort($array) { | |
for($i=0;$i<sizeof($array);$i++) { | |
for($j=0;$j<sizeof($array)-1;$j++) { | |
if($array[$j]>$array[$j+1]) { | |
$temp = $array[$j]; | |
$array[$j] = $array[$j+1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function dijkstraRoute($matrix, $start, $stop) { | |
$Q = array(); // this will be the array containing the estimated cost to each node | |
$S = array(); //this set will have, for each key, the prevoious node and the cost to reach it | |
foreach (array_keys($matrix) as $node) $Q[$node] = 99999; // assume this value as infinite | |
$Q[$start] = 0; |
OlderNewer