Skip to content

Instantly share code, notes, and snippets.

View antoniopicone's full-sized avatar

Antonio Picone antoniopicone

View GitHub Profile
@antoniopicone
antoniopicone / Queue.php
Last active August 23, 2016 10:27
A simple PHP implementation of a Queue
<?php
class Node {
public $data;
public $next;
public function __construct($data)
{
$this->data = $data;
@antoniopicone
antoniopicone / Stack.php
Last active August 23, 2016 10:20
A simple PHP implementation of a Stack
<?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;
@antoniopicone
antoniopicone / binary_tree_balanced.php
Created August 20, 2016 10:08
Solution to a problem to check if a binary tree is balanced
<?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 {
@antoniopicone
antoniopicone / LinkedList.php
Last active April 25, 2023 22:46
A simple Linked List in PHP
<?php
class Node {
public $data;
public $next;
public function __construct($data)
{
$this->data = $data;
@antoniopicone
antoniopicone / Graph.php
Created August 23, 2016 13:51
A simple Graph implementation with a DFS and BFS traversal algorithms.
<?php
class GraphNode {
public $data;
public $adiacent_nodes;
public function __construct($data, $adiacent_nodes)
{
$this->data = $data;
@antoniopicone
antoniopicone / BinaryTree.php
Created August 23, 2016 15:16
A Binary Tree implementation with DFS and BFS traversal algorithms
<?php
class Node {
public $data;
public $next;
public function __construct($data)
@antoniopicone
antoniopicone / MergeSort.php
Created August 23, 2016 15:54
A simple implementation of Merge Sort in PHP
<?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;
@antoniopicone
antoniopicone / QuickSort.php
Created August 23, 2016 16:00
A simple implementation of Quick Sort in PHP
<?php
function quickSort($array) {
if(sizeof($array) <= 1) return $array;
$pivot = $array[0];
$left = $right = array();
@antoniopicone
antoniopicone / BubbleSort.php
Created August 23, 2016 16:04
A simple PHP implementation of the worst Bubble Sort
<?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];
@antoniopicone
antoniopicone / Dijkstra.php
Created August 25, 2016 09:14
A PHP implementation of the Dijkstra algorithm to find a good path to travel between two nodes
<?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;