Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
378 commits
Select commit Hold shift + click to select a range
eb4fb77
Fix test error
jonathanstallings Jul 5, 2015
46582ba
Complete first pass of script
jonathanstallings Jul 5, 2015
f513c57
Add pretty printing of node val and priority.
jonathanstallings Jul 5, 2015
784ea9b
Improve docstrings
jonathanstallings Jul 5, 2015
9184eb5
Change wording in docstring
jonathanstallings Jul 5, 2015
ff88ee3
Improve docstrings
jonathanstallings Jul 5, 2015
cca2904
Start tracking insertion order after priority
jonathanstallings Jul 5, 2015
36c1769
Make priority and order explicit in tests
jonathanstallings Jul 5, 2015
4a898a5
Improve pretty printing
jonathanstallings Jul 5, 2015
3691c3b
Improve tests
jonathanstallings Jul 5, 2015
439d784
Fix docstring for queue order tracking
jonathanstallings Jul 5, 2015
85aa656
Remove commment from test
jonathanstallings Jul 5, 2015
0a5c665
Change pop and peek return to value instead of QNode
jonathanstallings Jul 5, 2015
2303015
Use '==' instead of 'is' where appropriate
jonathanstallings Jul 5, 2015
2c5f347
Refactor __eq__
jonathanstallings Jul 5, 2015
b661900
Remove unneeded _count attribute
jonathanstallings Jul 5, 2015
fa8f06f
Merge pull request #12 from jonathanstallings/feature/doublelinklist_…
jonathanstallings Jul 5, 2015
c930f6e
Merge pull request #6 from jonathanstallings/feature/doublelinklist
jonathanstallings Jul 5, 2015
3020022
Merge pull request #13 from jonathanstallings/feature/priorityq_jonathan
jonathanstallings Jul 5, 2015
67293fd
Added iterable support for (val, priority) containerized pairs; issue…
jay-tyler Jul 5, 2015
3ed457b
updated QNode __repr__ to display '(val, priority)'
jay-tyler Jul 5, 2015
2c76e0e
Added README content for PriorityQ; issue #15
jay-tyler Jul 5, 2015
dd68c43
Merge branch 'feature/binaryheap'
jonathanstallings Jul 5, 2015
7852e84
Added instantiation testing
jay-tyler Jul 5, 2015
eb65555
Added incremental tests for peek() and pop()
jay-tyler Jul 5, 2015
a137018
Merge pull request #17 from jonathanstallings/feature/priorityq_jason
jay-tyler Jul 5, 2015
3efe966
Merge branch 'feature/priorityq'
jonathanstallings Jul 7, 2015
ce38801
Sketch out methods
jonathanstallings Jul 7, 2015
6b971de
Change dictionary name to avoid collision; fix dict.values() call
jonathanstallings Jul 7, 2015
ecd7253
Fill out some special methods
jonathanstallings Jul 7, 2015
d6950bd
Refactor a bit
jonathanstallings Jul 7, 2015
921a57a
Add helper for testing
jonathanstallings Jul 7, 2015
12b867b
Protect existing node from being overwritten
jonathanstallings Jul 7, 2015
942f7be
Complete first pass edges method.
jonathanstallings Jul 7, 2015
3caf246
Use generator under edges method
jonathanstallings Jul 7, 2015
6385d7a
Add another generator under neighbors method
jonathanstallings Jul 7, 2015
80125eb
Reorganize
jonathanstallings Jul 7, 2015
34a04d7
Add nodes if not exist with add_edge method
jonathanstallings Jul 7, 2015
e0adfae
Improve add_edge method
jonathanstallings Jul 7, 2015
d3da8c4
Raise appropriate exception for nonexistant node in neighbors method
jonathanstallings Jul 7, 2015
2b9603f
Mark areas for refactor
jonathanstallings Jul 7, 2015
af20524
Add some notes to graph script
jonathanstallings Jul 7, 2015
0085a0e
Merge branch 'feature/simple_graph_jonathan' into feature/simple_graph
jonathanstallings Jul 7, 2015
a249b70
Fix neighbors method; add cleanu to __delitem__; raise appropriate er…
jonathanstallings Jul 7, 2015
6aa479e
Add test_graph for TAD; I promiss TDD next time!
jonathanstallings Jul 7, 2015
022d3a0
Improved doc strings; re issue #21
jay-tyler Jul 7, 2015
febdf74
fixed merge conflict
jay-tyler Jul 7, 2015
13ff138
Add simple fixtures for tests. refs #22
jonathanstallings Jul 8, 2015
32f316e
Add some add_node tests
jonathanstallings Jul 8, 2015
71ef5d2
Add further tests for add_node
jonathanstallings Jul 8, 2015
663d950
test for .nodes()
jay-tyler Jul 8, 2015
30272f6
test for .edges()
jay-tyler Jul 8, 2015
56897f2
test for .edges()
jay-tyler Jul 8, 2015
18fe134
Add further tests; reorganize
jonathanstallings Jul 8, 2015
41bd2d8
Complete add edge tests
jonathanstallings Jul 8, 2015
d398310
Complete outlined tests
jonathanstallings Jul 8, 2015
68546d9
.adjacent() was bidirectional, fixed now. Tests complete.
jay-tyler Jul 8, 2015
1b66234
Lint fixes
jay-tyler Jul 8, 2015
7619d02
resolve merge conflict
jay-tyler Jul 8, 2015
d9a5ecd
cleanup re: #27
jay-tyler Jul 8, 2015
c80e0f9
Fix linting errors
jonathanstallings Jul 8, 2015
c6e1ac3
Fix remaining style issues
jonathanstallings Jul 9, 2015
f7a9b5e
Add first-pass traversal methods
jonathanstallings Jul 10, 2015
eb44296
Improve docstrings. refs #31
jonathanstallings Jul 10, 2015
62edc74
Improve docstrings
jonathanstallings Jul 10, 2015
9be6cff
Fix Sublime EOF
jonathanstallings Jul 10, 2015
20b939b
Update readme. refs #30
jonathanstallings Jul 10, 2015
439efce
added tests for depth and breadth traversals
jay-tyler Jul 12, 2015
28f3931
Merge pull request #34 from jonathanstallings/feature/graph_traversal…
jonathanstallings Jul 12, 2015
db6e7e7
Add simple travis yml file
jonathanstallings Jul 20, 2015
8bac407
Update README with travis badge
jonathanstallings Jul 20, 2015
df55cb9
Add requirements.txt
jonathanstallings Jul 20, 2015
f3890f6
Test travis
jonathanstallings Jul 20, 2015
064b1d1
Remove test line in readme
jonathanstallings Jul 20, 2015
686f024
Update test fixtures with weighted dictionary structure
jonathanstallings Jul 20, 2015
25c1165
refactored from a dict>set to dict>dict for containerizing edges; edg…
jay-tyler Jul 20, 2015
aad9c05
Begin changing old tests to weighted dict structure
jonathanstallings Jul 20, 2015
76368ac
refactored some set.add() to dict.update()
jay-tyler Jul 20, 2015
83f0aab
Merge pull request #37 from jonathanstallings/feature/edge_weights_jo…
jonathanstallings Jul 20, 2015
9b18d2d
Complete work through line 202 on test conversion
jonathanstallings Jul 20, 2015
b825b3e
Complete test conversion up to traversal tests
jonathanstallings Jul 20, 2015
ce667f0
Fix error with neighbors tests
jonathanstallings Jul 20, 2015
4ef1bc3
refactored tests for traversal
jay-tyler Jul 20, 2015
f2083c2
Merge pull request #38 from jonathanstallings/feature/edge_weights_jo…
jonathanstallings Jul 20, 2015
3616131
Fix typo
jonathanstallings Jul 20, 2015
d7141e6
Merge branch 'feature/edge_weights'
jonathanstallings Jul 21, 2015
7571c49
Complete first pass at uniform cost search
jonathanstallings Jul 21, 2015
6a78388
Fix typo
jonathanstallings Jul 21, 2015
47bd4f5
Improve docstring
jonathanstallings Jul 22, 2015
6e7171c
Add simple test for uniform cost search
jonathanstallings Jul 22, 2015
fd58d80
added bellmanford algorithm
jay-tyler Jul 22, 2015
d36c1f1
Merge pull request #40 from jonathanstallings/feature/shortest-path_j…
jay-tyler Jul 22, 2015
5a40baf
resolved merge
jay-tyler Jul 22, 2015
c50b93c
added tests for bellmanford
jay-tyler Jul 22, 2015
ddae430
Updates to readme
jay-tyler Jul 22, 2015
f700317
Merge pull request #42 from jonathanstallings/feature/shortest-path
jonathanstallings Jul 22, 2015
ea78729
Update README.md
jonathanstallings Jul 22, 2015
8783857
Add bst.py
jonathanstallings Jul 22, 2015
2d6dfd0
Frame out methods
jonathanstallings Jul 22, 2015
81e73d0
Change data to val
jonathanstallings Jul 22, 2015
bd1bf7b
First pass insert method
jonathanstallings Jul 22, 2015
ed53183
Add contains method
jonathanstallings Jul 22, 2015
80756e4
Implement size using count; balance method
jonathanstallings Jul 22, 2015
3e73a07
Commit initial tests to repo
sjschmidt44 Jul 22, 2015
b301b15
Merge github repo with local machine.
sjschmidt44 Jul 22, 2015
f6254e2
Fix typo
jonathanstallings Jul 22, 2015
0367ba6
Merge branch 'feature/bst' of https://github.com/jonathanstallings/da…
jonathanstallings Jul 22, 2015
ebd8c36
First pass depth method
jonathanstallings Jul 22, 2015
3c1e6b5
Update tests
sjschmidt44 Jul 22, 2015
7471402
Update local.
sjschmidt44 Jul 22, 2015
12c6b2c
Add repr; fix insert method
jonathanstallings Jul 22, 2015
d5861fd
Merge branch 'feature/bst' of https://github.com/jonathanstallings/da…
jonathanstallings Jul 22, 2015
b011e06
Fix none case in balance method
jonathanstallings Jul 22, 2015
ff36fff
Change size method; fix none checks
jonathanstallings Jul 22, 2015
cb38133
Fix test errors
jonathanstallings Jul 22, 2015
0eb9788
Fix test for none checks
jonathanstallings Jul 22, 2015
85b8211
Improve docstrings
jonathanstallings Jul 22, 2015
aa62fe0
Add futher none check to balance method
jonathanstallings Jul 23, 2015
678c379
Document best and worst case in main section
jonathanstallings Jul 23, 2015
7be0ff3
Fix typo
jonathanstallings Jul 23, 2015
be8ae50
Update README.md
jonathanstallings Jul 23, 2015
f67b8c6
Add traversals to tree.
sjschmidt44 Jul 23, 2015
b152a63
Fix balance method
jonathanstallings Jul 23, 2015
4975392
Fix test balance; need to improve test architecture.
jonathanstallings Jul 23, 2015
ed8718f
Add __len__ and __iter__
jonathanstallings Jul 23, 2015
5bd3a8c
Allow empty node creation
jonathanstallings Jul 23, 2015
990f9ca
Fix size method to match spec with empty node
jonathanstallings Jul 23, 2015
ce06efa
Add function to generate a balanced BST for testing
jonathanstallings Jul 23, 2015
3d23651
Improve best worst case demo
jonathanstallings Jul 24, 2015
a915ee9
Improve docstrings
jonathanstallings Jul 24, 2015
a6b82a9
Add dot methods; reorganize helper class methods
jonathanstallings Jul 24, 2015
1e25fcc
Allow saving graphviz output; update .gitignore to exclude
jonathanstallings Jul 24, 2015
719af5e
Update requirements for graphviz
jonathanstallings Jul 24, 2015
9d8dcc9
Add __init__.py
jonathanstallings Jul 24, 2015
bd18ed1
Improve save render method
jonathanstallings Jul 24, 2015
12d9996
Improve repr
jonathanstallings Jul 24, 2015
883a637
Add simple init tests
jonathanstallings Jul 24, 2015
5335420
Add further insert tests
jonathanstallings Jul 24, 2015
7200e68
Add further contains, size, depth, and balance tests
jonathanstallings Jul 24, 2015
befeabe
Move method for organization
jonathanstallings Jul 24, 2015
33810ff
Add docstrings to traversal methods
jonathanstallings Jul 24, 2015
e321d81
Fix implementation of balance method
jonathanstallings Jul 24, 2015
2b723d7
Fix test for balance
jonathanstallings Jul 24, 2015
905730c
Add traversal tests
jonathanstallings Jul 24, 2015
37f5193
Add simple tests for helper methods
jonathanstallings Jul 24, 2015
9351093
Fix typo
jonathanstallings Jul 24, 2015
3e85845
Add a few more tests
jonathanstallings Jul 24, 2015
1d73c05
Update README
jonathanstallings Jul 24, 2015
ea26dab
Update README.md
jonathanstallings Jul 24, 2015
f8a003b
Improve docstring
jonathanstallings Jul 24, 2015
c8e65dc
Merge pull request #43 from jonathanstallings/feature/bst
jonathanstallings Jul 24, 2015
64e0bfb
Fix some style issues
jonathanstallings Jul 24, 2015
9c9183e
Clean up typos after linter fix
jonathanstallings Jul 24, 2015
d54315e
Resolve Merge conflict
jonathanstallings Jul 27, 2015
8f0f862
Merge pull request #44 from jonathanstallings/feature/bst_traversal
sjschmidt44 Jul 27, 2015
c942bcd
Update bst with delete functionality.
sjschmidt44 Jul 27, 2015
8d43fec
Add some tests for delete method
jonathanstallings Jul 27, 2015
f85ec95
Add simple test for private lookup method
jonathanstallings Jul 27, 2015
bc09e78
Add a few more tests for deletion of root
jonathanstallings Jul 27, 2015
ddd3ed7
Improve docstrings
jonathanstallings Jul 27, 2015
44b374e
Remove unneeded deletion of node; Note: Linter bug identified here
jonathanstallings Jul 27, 2015
d8a64cb
Fix string formatting syntax
jonathanstallings Jul 27, 2015
f096e37
Add module docstring; fix some style issues.
jonathanstallings Jul 27, 2015
412fbd7
Fix typo
jonathanstallings Jul 27, 2015
9de9de7
Merge pull request #45 from jonathanstallings/feature/bst_delete
sjschmidt44 Jul 29, 2015
85f3858
Push new balance branch to repo
sjschmidt44 Jul 29, 2015
0b3ef04
Update insert method to accept parent argument.
sjschmidt44 Jul 29, 2015
889712c
Extend _sorted_list_to_bst to use node parent
jonathanstallings Jul 29, 2015
2bb64bb
Adapt delete method for parent references
jonathanstallings Jul 29, 2015
ca74dfa
Add rotate and self_balance methods to bst."
sjschmidt44 Jul 29, 2015
d44a876
Add balancing to insert and delete.
sjschmidt44 Jul 29, 2015
1c7ff68
Fix bug with insert
jonathanstallings Jul 30, 2015
1110f5f
Save work
jonathanstallings Jul 30, 2015
21d6190
Add create_worst_case method for testing
jonathanstallings Jul 30, 2015
8115677
Refactor self_balance, delete
jonathanstallings Jul 30, 2015
0997970
Refactor _lookup due to parent attribute
jonathanstallings Jul 30, 2015
ba50348
Fix balance point in delete
jonathanstallings Jul 30, 2015
315a954
Fix rotate methods to not lose root object
jonathanstallings Jul 30, 2015
e6ce537
Add auto-render flag to insert and delete; fix parent level on self_b…
jonathanstallings Jul 30, 2015
ea045b9
Add ability to shut off AVL balancing on insert or delete
jonathanstallings Jul 30, 2015
14b02ef
Fix old non-AVL tests
jonathanstallings Jul 30, 2015
29d7527
Add tests for AVL insert
jonathanstallings Jul 30, 2015
91e0b2b
Complete AVL tests for insert and delete
jonathanstallings Jul 30, 2015
e20b171
Add tests for create worst case and _is_left
jonathanstallings Jul 30, 2015
039a5f5
Add rotate tests
jonathanstallings Jul 30, 2015
4ad6cb0
Complete self_balance tests
jonathanstallings Jul 30, 2015
3bd3542
Improve docstrings
jonathanstallings Jul 30, 2015
109755f
Update readme
jonathanstallings Jul 30, 2015
f7b8955
Mark some methods private; reorganize
jonathanstallings Jul 30, 2015
9912eff
Add addition and subtraction from iterables for fun
jonathanstallings Jul 30, 2015
f4c452f
Merge pull request #46 from jonathanstallings/self_balance_jonathan
sjschmidt44 Jul 30, 2015
28bec05
Merge pull request #47 from jonathanstallings/self_balance
sjschmidt44 Jul 30, 2015
8c60480
Add hash.py
jonathanstallings Jul 30, 2015
ce692cf
Add test_hash.py
jonathanstallings Jul 30, 2015
f8f1479
Sketch out first methods
jonathanstallings Jul 30, 2015
fb5d56b
Fix typo
jonathanstallings Jul 30, 2015
07f5c32
Add insert method
jonathanstallings Jul 30, 2015
5f4d91c
First pass spec methods
jonathanstallings Jul 30, 2015
0bd8dd3
Refactor to remove item class
jonathanstallings Jul 30, 2015
67f9e0f
Fix change to tuple storage in hashtable
jonathanstallings Jul 30, 2015
8168189
Add string validation to insert
jonathanstallings Jul 30, 2015
43c0c58
Add if name is main block
jonathanstallings Jul 30, 2015
7e53a3a
Add len method; set default table size
jonathanstallings Jul 30, 2015
2bf7564
Refactor and remove char2int
jonathanstallings Jul 30, 2015
d023f30
Rename hash.py due to reserved name
jonathanstallings Jul 31, 2015
c85af6b
Save work
jonathanstallings Jul 31, 2015
7241ae6
Fix error in get method
jonathanstallings Jul 31, 2015
cbf584f
fix len method
jonathanstallings Jul 31, 2015
cd60859
Add some tests
jonathanstallings Jul 31, 2015
1353e9c
Add further tests
jonathanstallings Jul 31, 2015
7c1f530
Change default hash table size to 8192; good lookup perfomance
jonathanstallings Jul 31, 2015
48c374b
Improve docstrings
jonathanstallings Aug 1, 2015
a7f0aaa
Update readme; add module docstring
jonathanstallings Aug 1, 2015
df9281e
Merge pull request #48 from jonathanstallings/feature/hash_jonathan
jonathanstallings Aug 1, 2015
b68e277
Merge pull request #49 from jonathanstallings/feature/hash
jonathanstallings Aug 3, 2015
06fc94b
Add insertion sort method to module.
sjschmidt44 Aug 3, 2015
9cfc5c5
Update module with timeit testing for best and worst case scenarios.
sjschmidt44 Aug 3, 2015
e0ebd4c
Update insertion sort with list validation
sjschmidt44 Aug 3, 2015
1f9ba14
Add test_insertion_sort
jonathanstallings Aug 3, 2015
9f4d0e6
Add another test
jonathanstallings Aug 3, 2015
b84d61d
Merge branch 'insertion_sort' of https://github.com/jonathanstallings…
jonathanstallings Aug 3, 2015
f3cb538
Improve docstrings
jonathanstallings Aug 3, 2015
1a237fd
Resolve name clobbering; add TypeError raise when approriate.
jonathanstallings Aug 3, 2015
57a02c9
Add insertion sort with duplicate values test
jonathanstallings Aug 3, 2015
1750302
Merge pull request #50 from jonathanstallings/insertion_sort
sjschmidt44 Aug 4, 2015
2ea6645
Add merge_sort to repo.
sjschmidt44 Aug 4, 2015
4732cfb
Save work on performance demo
jonathanstallings Aug 4, 2015
1c69cf6
Add test_merge_sort
jonathanstallings Aug 4, 2015
0f606c8
Raise TypeError on merge_sort if not passed an iterable
jonathanstallings Aug 4, 2015
7451a0d
Complete simple tests
jonathanstallings Aug 4, 2015
8219fe0
Improve docstrings.
jonathanstallings Aug 4, 2015
49317a6
Update readme
jonathanstallings Aug 4, 2015
3ab8d10
Require merge_srt to take a list
jonathanstallings Aug 4, 2015
62b3b31
Remove list-type validation from merge sort; allow natural errors to …
jonathanstallings Aug 4, 2015
96d9038
Merge pull request #51 from jonathanstallings/merge_sort
sjschmidt44 Aug 5, 2015
80c2584
Add quicksort to repo.
sjschmidt44 Aug 5, 2015
d34b0b1
Refactor tests
jonathanstallings Aug 5, 2015
adc3fa7
Update index error in _sort method
sjschmidt44 Aug 5, 2015
0af1b0b
Add basic tests for quick sort
jonathanstallings Aug 5, 2015
a453d75
Merge branch 'quick_sort' of https://github.com/jonathanstallings/dat…
jonathanstallings Aug 5, 2015
648b8ca
Refactor to account for larger data sets.
sjschmidt44 Aug 5, 2015
da76f0b
Jonathan blew it up!
sjschmidt44 Aug 5, 2015
009488b
Improve docstrings
jonathanstallings Aug 5, 2015
e44a589
Update readme
jonathanstallings Aug 5, 2015
d8f4550
Add reference attribution to module docstring
jonathanstallings Aug 5, 2015
af9c686
Add a few more tests for edge cases
jonathanstallings Aug 6, 2015
82cb060
Merge pull request #52 from jonathanstallings/quick_sort
jonathanstallings Aug 6, 2015
34f8ba6
Fix docstring
jonathanstallings Aug 13, 2015
2ee41a5
Simplify bst.py rotation algorithms
Oct 17, 2018
9e83d2d
Merge pull request #57 from Caidentp/master
jonathanstallings Oct 18, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ docs/_build/

# PyBuilder
target/

# Graphviz Output
graphviz/
28 changes: 28 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Set up notification options
notifications:
email:
# change is when the repo status goes from pass to fail or vice versa
on_success: change
on_failure: always

# specify language
language: python
python:
- "2.7"

## command to install dependencies
install:
- 'pip install -r requirements.txt'

## Script to run
script: py.test

branches:
## whitelist
only:
- master
- staging

# blacklist
except:
- /^.*test.*$/
187 changes: 187 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
[![Build Status](https://travis-ci.org/jonathanstallings/data-structures.svg?branch=master)](https://travis-ci.org/jonathanstallings/data-structures)


#Data Structures
Implementation of LinkedList and Stack data structures in Python.

##LinkedList
The LinkedList class is composed of a Node base class.

Available methods inlude:
* insert(val)
* pop()
* size()
* search(val)
* remove(node)
* display()

##Stack
The Stack data class is a first-in-first-out data structure built via composition from LinkedList.
Available methods include:
* push(value)
* pop()

##Queue
The Queue data class is a first-in-last-out data structure built via encapsulation of a LinkedList.

Available methods inlude:
* enqueque(value)
* dequeque()
* len()

##Binary Heap
The Binary Heap data class is a binary tree data structure built implemented on a built-in Python
list. The binary heap default to a minheap sort, meaning that the smallest values will be sorted to
the top of the heap. Alternatively, the binary heap can be instantiated as a maxheap so that the
greatest values will be sorted to the top.

Available methods include:
* pop()
* push()
See the doc strings for implementation details.

##PriorityQ
The PriorityQ data class is a binary tree that implements sorting primarily by priority value and
secondarily by insertion order. The PriorityQ defaults to minheap sorting for both. A QNode is implemented
as a base class to containerize the value and priority and to provide convenient APIs for comparison.

Available methods include:
* insert(item)
* pop()
* peek()
See the doc strings for implementation details.

Instantiation of a PriorityQ takes an iterable which may contain (value, priority) iterables,
non-iterable values, or QNode objects.

##Graph
The graph data class is a network consisting nodes with an arbitrary number of references (edges) to other
nodes in the graph. Methods allows abilities such as adding, removing, and checking the existance of nodes
and edges in the graph. Additionaly, the graph class contains to traversal methods. Given a start node, the
methods will traverse the entire network reachable from that node and return the path travelled as a list of
nodes travelled. Both [depth-first](https://en.wikipedia.org/wiki/Graph_traversal#Depth-first_search) and [breadth first](https://en.wikipedia.org/wiki/Graph_traversal#Breadth-first_search) traversal methods are available.

Available methods include:

* nodes()
* edges()
* add_node(n)
* add_edge(n1, n2)
* del_node(n)
* del_edge(n1, n2)
* has_node(n)
* neighbors(n)
* adjacent(n1, n2)
* depth_first_traversal(start)
* breadth_first_traversal(start)
* uniform_cost_search(n1, n2)
* bellmanford(n1, n2)

The uniform_cost_search method returns a path that corresponds to the
shortest path between n1 and n2. This algorithm tracks historical paths
and is able to search for the shortest path in a relatively uncostly way.
Time complexity is O(edges + node log nodes) with relatively low memory
overhead.

The bellmanfor search method also returns the same path, but has the added
ability to handle edges with negative values and detect negative feedback
loops. It is relatively robust, but at added time complexity cost of
O(nodes * edges).

See the doc strings for additional implementation details.

##Binary Search Tree
Contains a Node class which implements an AVL binary search tree.

Each node can be considered a binary search tree and has the usual
methods to insert, delete, and check membership of nodes. By default,
the insert and delete methods will perform self-balancing consistent
with an AVL tree. This behavior can be suppressed by passing the optional
'balanced=False' keyword argument to the insert or delete methods.

The class also supports four traversal methods which return generators:

- in_order
- pre_order
- post_order
- breadth_first.

Additionally, methods are included to help visualize the tree structure.
get_dot returns DOT source code, suitable for use with programs such as
Graphviz (http://graphviz.readthedocs.org/en/stable/index.html), and
save_render saves a rendering of the tree structure to the file system.
Passing the optional 'render=True' keyword argument to the insert and
delete methods will automatically save a render to disk upon execution.

Finally, the helper methods 'create_best_case' and 'create_worst_case'
facilitates creation of tree composeds of _n_ integers.

This module was completed with reference to the following:

[Binary Search Tree libary in Python](http://www.laurentluce.com/posts/binary-search-tree-library-in-python/)
by Laurent Luce.

[How to Balance your Binary Search Trees - AVL Trees](https://triangleinequality.wordpress.com/2014/07/15/how-to-balance-your-binary-search-trees-avl-trees/)

[The AVL Tree Rotations Tutorial](http://pages.cs.wisc.edu/~paton/readings/liblitVersion/AVL-Tree-Rotations.pdf)
by John Hargrove
Available methods include:

* insert(val, balanced=True, render=False)
* delete(val, balanced=True, render=False)
* contains(val)
* lookup(val)
* size()
* depth()
* balance()
* in_order()
* pre_order()
* post_order()
* breadth_first()
* get_dot()
* save_render()
* create_best_case()
* create_worst_case()


See the doc strings for additional implementation details.

##Hash Table
Contains a HashTable class. The [hash table](https://en.wikipedia.org/wiki/Hash_table)
is implemented on a list of lists, with a default table size of 8192. This table
size can be overridden on initialization by passing a size keyword argument. Insertion
and lookup time complexity ranges from O(1) at best to O(n) at worst.

Available methods include:

* set(key, value)
* get(key)

##Insertion Sort
This module contains the in_sort method, which performs an
in-place insertion sort on a passed in list. Insertion sort has a best case time
complexity of O(n) when list is nearly sorted, and a worst case of O(n2)
when the incoming list is already reverse sorted. While this not always
the most efficient algorithm, it is still popular when the data is nearly
sorted (because it is adaptive) or when the problem size is small.
See the excellent 'sortingalgorithms.com' for more information.

## Merge Sort
This module contains the merge_srt method, which performs an
in-place merge sort on a passed in list. Merge sort has a best case time
complexity of O(n log n) when list is nearly sorted, and also a worst case of
O(n log n). Merge sort is a very predictable and stable sort, but it is not
adaptive. See the excellent 'sortingalgorithms.com' for more information.

##Quick Sort
"""This module contains the quick sort method (quick_srt), which performs an
in-place sort on a passed in list. Quick sort has a best case time
complexity of O(n log n) when all elements are equal, and a worst case of
O(n2). Quick sort is not stable or adaptive, but it is robust and has low
overhead.

This module was completed with reference to:
[Quicksort: A Python Implementation](http://pfsensesetup.com/pythonscript.net/quicksort-a-python-implementation/)
by maximumdx

See the excellent 'sortingalgorithms.com' for more information.
Empty file added __init__.py
Empty file.
143 changes: 143 additions & 0 deletions binary_heap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
from __future__ import unicode_literals


class BinaryHeap(object):
"""A class for a binary heap."""
def __init__(self, iterable=(), minheap=True):
"""Initializes a binary heap, optionally with items from an iterable.

By default, the binary will sort as a minheap, with smallest values
at the head. If minheap is set to false, the binary heap will sort
as a maxheap, with largest values at the head.
"""
self.tree = []
self.minheap = minheap
for val in iterable:
self.push(val)

def __repr__(self):
return repr(self.tree)

def __len__(self):
return len(self.tree)

def __iter__(self):
return iter(self.tree)

def __getitem__(self, index):
return self.tree[index]

def __setitem__(self, index, value):
self.tree[index] = value

def pop(self):
"""Pop the head from the heap and return."""
if len(self) <= 1:
to_return = self.tree.pop()
else:
endpos = len(self) - 1
self._swap(0, endpos)
to_return = self.tree.pop()
self._bubbledown(0)
return to_return

def push(self, value):
"""Push a value onto a stack.

args:
value: the value to add
"""
self.tree.append(value) # Add protection for different types case
if len(self) > 1:
endpos = len(self) - 1
self._bubbleup(endpos)

def _bubbleup(self, pos):
"""Perform one step of heap sort up the tree.

args:
pos: the index position to inspect
"""
parent = self._find_parent(pos)
if pos == 0: # find_parent will return -1 at end of list
return
elif self._is_unsorted(self[pos], self[parent]):
self._swap(pos, parent)
self._bubbleup(parent)

def _bubbledown(self, pos):
"""Perform one step of heap sort down the tree.

args:
pos: the index position to inspect
"""
lchild, rchild = self._find_children(pos)
try: # Evaluating whether lchild exists; may refactor
lval = self[lchild]
try:
rval = self[rchild]
except IndexError: # Case of left_child only
if self._is_unsorted(lval, self[pos]):
self._swap(lchild, pos)
else: # Case of left_child and right_child
if self._is_unsorted(lval, rval):
target = lchild
else:
target = rchild
if self._is_unsorted(self[target], self[pos]):
self._swap(target, pos)
self._bubbledown(target)

except IndexError: # Case of no lchild
return

def _find_parent(self, pos):
"""Returns the parent index of given position.

args:
pos: the index position to inspect

Returns: index of the parent
"""
parent = (pos - 1) // 2
return parent

def _find_children(self, pos):
"""Returns the indices of children from given position.

args:
pos: the index position to inspect

Returns: index of left child and right child
"""
lchild = (pos * 2) + 1
rchild = lchild + 1
return lchild, rchild

def _is_unsorted(self, item1, item2):
"""Compare two items according to heaptype.

For a minheap, checks if first item is less than second item.
For a maxheap, checks if first item is greater than second item.

args:
item1: first item
item2: second item

Returns: True if heaptype comparison matches, else False
"""
if self.minheap is True:
return item1 < item2
elif self.minheap is False:
return item1 > item2
else:
raise AttributeError('heaptype not assigned')

def _swap(self, pos1, pos2):
"""Swap the values at given index positions.

args:
pos1: the index of the first item
pos2: the index of the second item
"""
self[pos1], self[pos2] = self[pos2], self[pos1]
Loading