This file contains hidden or 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
| class Queue { | |
| protected $first; | |
| protected $last; | |
| public function enqueue($data) { | |
| $node = new Node($data); | |
| if ( is_null( $this->first ) ) { | |
| $this->first = $this->last = $node; | |
| } else { |
This file contains hidden or 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
| /** | |
| * Childs game Tower of Hanoi with Stacks | |
| * O(n^2) | |
| * | |
| * | |
| * Disk layout | |
| * | |
| * 1|1 | | | |
| * 2_|_2 | | | |
| * 3__|__3 | | |
This file contains hidden or 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
| function BFS( $node, $data ) { | |
| if ( $node->data == $data ) | |
| return $node; | |
| $node->visited = true; | |
| $queue = new Queue; | |
| $queue->enqueue($node); | |
| while ( !$queue->isEmpty() ) { |
This file contains hidden or 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
| function DFS( $node, $data ) { | |
| if ( $node->data == $data ) | |
| return $node; | |
| $new_node = null; | |
| if ( !is_null( $node->left ) ) | |
| $new_node = DFS($node->left, $data); | |
| if ( !$new_node && !is_null( $node->right ) ) |
This file contains hidden or 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
| function getBit($binary_number, $byte_placement) { | |
| return ( $binary_number & ( 1 << $byte_placement ) ) != 0; | |
| } | |
| function setBit( $binary_number, $byte_placement ) { | |
| return $binary_number | ( 1 << $byte_placement ); | |
| } | |
| function clearBit( $binary_number, $byte_placement ) { | |
| $mask = ~( 1 << $byte_placement ); |
This file contains hidden or 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, $start, $end) { | |
| if ( $start < $end ) { | |
| $pivotIndex = partition($array, $start, $end); | |
| QuickSort($array, $start, $pivotIndex - 1); | |
| QuickSort($array, $pivotIndex + 1, $end); | |
| } | |
| } | |
| function partition(&$array, $start, $end) { |
This file contains hidden or 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
| class LinkedList(object): | |
| head = None | |
| def add(self, data): | |
| new_node = Node(data) | |
| if self.head is None: | |
| self.head = new_node | |
| else: | |
| node = self.head |
This file contains hidden or 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
| class Stack(object): | |
| top = None | |
| def push(self, data): | |
| node = Node(data) | |
| node.next = self.top | |
| self.top = node | |
| def pop(self): | |
| if self.top is None: |
This file contains hidden or 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
| class BST(object): | |
| root = None | |
| def search(self, data): | |
| return self._binarySearch(data, self.root) | |
| def _binarySearch(self, data, root): | |
| if root is None: | |
| return False | |
| elif root.data == data: |
This file contains hidden or 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
| def prime(n): | |
| if n == 2: | |
| return True | |
| elif n % 2 == 0: | |
| return False | |
| max = math.ceil(math.sqrt(n)) | |
| for i in range(3,max,2): | |
| if n % i == 0: |