Skip to content

Commit

Permalink
[chore] update readme, replace py file with notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
gitgik committed Feb 25, 2021
1 parent afe0aaf commit de58a67
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 150 deletions.
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ A catalogue of data structures implementation + algorithms and coding problems a

## Binary Trees

- [All paths from root to leaves](binary-trees/root_to_leaves_path.ipynb)
- [Check whether tree T is subtree of S](binary-trees/compare_subtree.ipynb)
- [Depth of a tree](binary-trees/depths.ipynb)
- [Diameter of a tree](binary-trees/diameter.ipynb)
- [Find successor of a given node](binary-trees/successor.ipynb)
- [Sum nodes within a given range](binary-trees/binary_range_sum.ipynb)
- [All paths from root to leaves](binary-trees/root_to_leaves_path.md)
- [Check whether tree T is subtree of S](binary-trees/compare_subtree.md)
- [Depth of a tree](binary-trees/depths.md)
- [Diameter of a tree](binary-trees/diameter.md)
- [Find successor of a given node](binary-trees/successor.md)
- [Sum nodes within a given range](binary-trees/binary_range_sum.md)
- [Invert a binary tree](binary-trees/binary_tree_inversion.py)
- [Binary tree sum](binary-trees/binary_tree_sum.ipynb)
- [Flatten a binary tree](binary-trees/flatten_binary_tree.ipynb)
- [Is binary tree balanced?](binary-trees/is_binary_tree_balanced.ipynb)
- [Print binary tree nodes level-wise](binary-trees/print_binary_tree_nodes.py)
- [Serialize and deserialize binary tree](binary-trees/serialize_deserialize_tree.ipynb)
- [Traverse in-order iteratively](binary-trees/traverse_in_order_iteratively.ipynb)
- [Binary tree sum](binary-trees/binary_tree_sum.md)
- [Flatten a binary tree](binary-trees/flatten_binary_tree.md)
- [Is binary tree balanced?](binary-trees/is_binary_tree_balanced.md)
- [Print binary tree nodes level-wise](binary-trees/print_tree_nodes.md)
- [Serialize and deserialize binary tree](binary-trees/serialize_deserialize_tree.md)
- [Traverse in-order iteratively](binary-trees/traverse_in_order_iteratively.md)


## Bit Manipulation
Expand All @@ -70,19 +70,19 @@ A catalogue of data structures implementation + algorithms and coding problems a
## Dynamic Programming

- [Count ways to decode](dynamic-programming/count_decodings.md)
- [Disk stacking](dynamic-programming/disk_stacking.ipynb)
- [Efficient fibonacci](dynamic-programming/fibonacci.ipynb)
- [Find denominations](dynamic-programming/find_denominations.ipynb)
- [Kadenes algorithm](dynamic-programming/kadenes_algorithm.ipynb)
- [Longest common subsequence](dynamic-programming/longest_common_subsequence.ipynb)
- [Knapsack problem](dynamic-programming/knapsack.ipynb)
- [Disk stacking](dynamic-programming/disk_stacking.md)
- [Efficient fibonacci](dynamic-programming/fibonacci.md)
- [Find denominations](dynamic-programming/find_denominations.md)
- [Kadenes algorithm](dynamic-programming/kadenes_algorithm.md)
- [Longest common subsequence](dynamic-programming/longest_common_subsequence.md)
- [Knapsack problem](dynamic-programming/knapsack.md)
- [Levenshtein dinstance](dynamic-programming/levenshtein_distance.py)
- [Max sum increasing subsequence](dynamic-programming/max_sum_increasing_subsequence.ipynb)
- [Max sum of non-adjacent elements in array](dynamic-programming/max_sum_no_adjacent.ipynb)
- [Minimum number of coins to make change](dynamic-programming/minimum_number_of_coins_for_change.ipynb)
- [Minimum number of jumps to last index](dynamic-programming/min_jumps_to_end.ipynb)
- [Throw dice](dynamic-programming/throw_dice.ipynb)
- [Ways to make change given denominations](no_of_ways_to_make_change.ipynb)
- [Max sum increasing subsequence](dynamic-programming/max_sum_increasing_subsequence.md)
- [Max sum of non-adjacent elements in array](dynamic-programming/max_sum_no_adjacent.md)
- [Minimum number of coins to make change](dynamic-programming/minimum_number_of_coins_for_change.md)
- [Minimum number of jumps to last index](dynamic-programming/min_jumps_to_end.md)
- [Throw dice](dynamic-programming/throw_dice.md)
- [Ways to make change given denominations](no_of_ways_to_make_change.md)

## Graphs

Expand All @@ -96,8 +96,8 @@ A catalogue of data structures implementation + algorithms and coding problems a

## Heaps

- [Given buildings, model a skyline](heaps/max_heap_skyline.ipynb)
- [Min heap construction + heap operations](heaps/min_heap.ipynb)
- [Given buildings, model a skyline](heaps/max_heap_skyline.md)
- [Min heap construction + heap operations](heaps/min_heap.md)

## Linked Lists

Expand Down
195 changes: 195 additions & 0 deletions linked-lists/doubly_linked_list.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0-final"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3.9.0 64-bit",
"metadata": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
}
}
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"class Node:\n",
" def __init__(self, value):\n",
" self.value = value\n",
" self.prev = None\n",
" self.next = None\n",
"\n",
"\n",
"class DoubleLinkedList:\n",
" def __init__(self):\n",
" self.head = None\n",
" self.tail = None\n",
"\n",
" def set_head(self, node):\n",
" \"\"\"O(1) time | O(1) space\"\"\"\n",
" if self.head is None:\n",
" self.head = node\n",
" self.tail = node\n",
" return\n",
" self.insert_before(self.head, node)\n",
"\n",
" def set_tail(self, node):\n",
" \"\"\"O(1) time | O(1) space\"\"\"\n",
" if self.tail is None:\n",
" self.set_head(node)\n",
" return\n",
" self.insert_after(self.tail, node)\n",
"\n",
" def insert_before(self, node, node_to_insert):\n",
" \"\"\"O(1) time | O(1) space\"\"\"\n",
" if node_to_insert == self.head and node_to_insert == self.tail:\n",
" return\n",
" # first, remove node if it exists\n",
" self.remove(node_to_insert)\n",
" node_to_insert.prev = node.prev\n",
" node_to_insert.next = node\n",
"\n",
" if node.prev is None:\n",
" self.head = node_to_insert\n",
" else:\n",
" node.prev.next = node_to_insert\n",
" node.prev = node_to_insert\n",
"\n",
" def insert_after(self, node, node_to_insert):\n",
" \"\"\"O(1) time | O(1) space\"\"\"\n",
" if node_to_insert == self.head and node_to_insert == self.tail:\n",
" return\n",
" # first remove the node to insert if it currently exists in the linked list\n",
" self.remove(node_to_insert)\n",
" node_to_insert.prev = node\n",
" node_to_insert.next = node.next\n",
"\n",
" if node.next is None:\n",
" self.tail = node_to_insert\n",
" else:\n",
" node.prev.next = node_to_insert\n",
" node.next = node_to_insert\n",
"\n",
" def insert_at_position(self, position, node_to_insert):\n",
" \"\"\"O(1) space | O(p) time - since we are iterating up until position p\"\"\"\n",
" if position == 1:\n",
" self.set_head(node_to_insert)\n",
" return\n",
" node = self.head\n",
" current_position = 1\n",
" while node is not None and current_position != position:\n",
" node = node.next\n",
" current_position += 1\n",
" if node is not None:\n",
" self.insert_before(node, node_to_insert)\n",
" else:\n",
" # node is None so we are at the tail\n",
" self.set_tail(node_to_insert)\n",
"\n",
" def remove_nodes_with_value(self, value):\n",
" \"\"\"O(n) time | O(1) space\"\"\"\n",
" node = self.head\n",
" while node is not None:\n",
" # temporary save the current node, to not lose it while we check\n",
" # if it needs to be removed\n",
" node_to_remove = node\n",
" # update the node to be the next node\n",
" node = node.next\n",
" if node_to_remove.value == value:\n",
" self.remove(node_to_remove)\n",
"\n",
" def remove(self, node):\n",
" \"\"\"O(1) time | O(1) space\"\"\"\n",
" if node == self.head:\n",
" self.head = self.head.next\n",
" if node == self.tail:\n",
" self.tail = self.tail.prev\n",
" self.update_node_bindings(node)\n",
"\n",
" def contains_node_with_value(self, value):\n",
" \"\"\"O(n) time | O(1) space\"\"\"\n",
" node = self.head\n",
" while node is not None and node.value != value:\n",
" node = node.next\n",
" return node is not None\n",
"\n",
" def update_node_bindings(self, node):\n",
" \"\"\"Update the pointers of a node when a node is removed from the doubly linked list.\n",
" \"\"\"\n",
" # the order in which you remove pointers matters. otherwise, we might delete nodes\n",
" # and never recover them forever.\n",
" if node.prev is not None:\n",
" node.prev.next = node.next\n",
" if node.next is not None:\n",
" node.next.prev = node.prev\n",
" node.prev = None\n",
" node.next = None"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"\n",
"doubly_linked_list = DoubleLinkedList()\n",
"first_node = Node(1)\n",
"second_node = Node(2)\n",
"third_node = Node(3)\n",
"\n",
"doubly_linked_list.set_head(first_node)\n",
"doubly_linked_list.set_tail(third_node)\n",
"doubly_linked_list.insert_before(third_node, second_node)\n",
"another_node = Node(7)\n",
"doubly_linked_list.insert_after(third_node, another_node)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {},
"execution_count": 5
}
],
"source": [
"doubly_linked_list.head.value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
]
}
124 changes: 0 additions & 124 deletions linked-lists/doubly_linked_list.py

This file was deleted.

0 comments on commit de58a67

Please sign in to comment.