Skip to content

Commit

Permalink
Merge pull request #47 from nbro/iss20
Browse files Browse the repository at this point in the history
Iss20 should be solved
  • Loading branch information
nbro committed Feb 5, 2017
2 parents c40c800 + f0f80f7 commit 64335f5
Show file tree
Hide file tree
Showing 36 changed files with 460 additions and 331 deletions.
46 changes: 26 additions & 20 deletions ands/ds/BST.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
# -*- coding: utf-8 -*-

"""
# Meta info
Author: Nelson Brochado
Creation: July, 2015
Created: 01/07/2015
Updated: 28/08/2016
# Description
Last update: 28/08/2016
### Coding Conventions
## Names' Conventions
In general, if a variable name has more than one word,
those words are separated by _ (underscores).
Functions' names should roughly describe what the function does.
Expand All @@ -17,11 +22,13 @@
comments are usually provide on the first occurrence of the name,
in order to explain the purpose of such a variable.
### Functions
#### Functions
- Methods that start with _ should not be called,
because they might either be "helper" or private functions.
### Parameters
#### Parameters
- `u`, `v`, `z` and `w` are used to indicate that a general `BSTNode` object is expected.
- `s` is used to indicate that a source node is expected.
Expand All @@ -31,12 +38,14 @@
- `ls` is usually used to indicate that a list or a tuple is expected.
### Local Variables
#### Local Variables
- `c` usually indicates some "current" changing variable.
- `p` is usually `c`'s parent.
### Docstrings
#### Docstrings
Under methods' signatures, h in O(h) is the height of the tree.
Note that the height of a BST varies depending on how elements
are inserted and removed.
Expand All @@ -46,25 +55,22 @@
Other names are self-descriptive.
For example, "key" and "value" are self-descriptive.
## Resources
- [https://en.wikipedia.org/wiki/Binary_search_tree](https://en.wikipedia.org/wiki/Binary_search_tree)
- [Introduction to Algorithms (3rd edition)](https://mitpress.mit.edu/books/introduction-algorithms) by CLRS, chapter 12
# TODO
- [http://algs4.cs.princeton.edu/32bst/](http://algs4.cs.princeton.edu/32bst/)
- [http://www.cs.princeton.edu/courses/archive/spr04/cos226/lectures/bst.4up.pdf](http://www.cs.princeton.edu/courses/archive/spr04/cos226/lectures/bst.4up.pdf
)
- [http://algs4.cs.princeton.edu/32bst/BST.java.html](http://algs4.cs.princeton.edu/32bst/BST.java.html)
## TODO
- Improve the "randomness" of insertion into the BSTImproved class.
- Add functions "intersection" and "union".
- Implement a recursive version of insert (OPTIONAL).
- implement "is balanced" function (http://codereview.stackexchange.com/questions/108459/binary-tree-data-structure)
- Maybe the methods of the BSTNode need an improvement in terms of implementation...
# Resources
- [https://en.wikipedia.org/wiki/Binary_search_tree](https://en.wikipedia.org/wiki/Binary_search_tree)
- [Introduction to Algorithms (3rd edition)](https://mitpress.mit.edu/books/introduction-algorithms) by CLRS, chapter 12
- [http://algs4.cs.princeton.edu/32bst/](http://algs4.cs.princeton.edu/32bst/)
- [http://www.cs.princeton.edu/courses/archive/spr04/cos226/lectures/bst.4up.pdf](http://www.cs.princeton.edu/courses/archive/spr04/cos226/lectures/bst.4up.pdf)
- [http://algs4.cs.princeton.edu/32bst/BST.java.html](http://algs4.cs.princeton.edu/32bst/BST.java.html)
"""

from random import randint
Expand Down
31 changes: 11 additions & 20 deletions ands/ds/DSForests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# -*- coding: utf-8 -*-

"""
## Meta info
# Meta info
Author: Nelson Brochado
Creation: 21/02/16
Created: 21/02/2016
Last update: 03/01/16
Updated: 03/01/2016
## Description
# Description
A disjoint-set (forests) or union-find data structure is a data structure which keeps track of a set of elements
partitioned into disjoint (non-overlapping, i.e. their intersection is the empty set) sets.
Expand All @@ -31,34 +31,26 @@
These two techniques complement each other: applied together, the amortized time per operation is only O( α (n)).
## References
# TODO
- Introduction to algorithms, 3rd, by C.L.R.S., chapter 21.3
- Deletion operation (OPTIONAL, since it's usually not part of the interface of a disjoint-set data structure)
- Pretty-print(x), for some element x in the disjoint-set data structure.
- Implement the version explained [here](http://algs4.cs.princeton.edu/15uf/)
- [https://en.wikipedia.org/wiki/Disjoint-set_data_structure](https://en.wikipedia.org/wiki/Disjoint-set_data_structure)
# References
- Introduction to algorithms, 3rd, by C.L.R.S., chapter 21.3
- [https://en.wikipedia.org/wiki/Disjoint-set_data_structure](https://en.wikipedia.org/wiki/Disjoint-set_data_structure)
- [http://orionsword.no-ip.org/blog/wordpress/?p=246](http://orionsword.no-ip.org/blog/wordpress/?p=246)
- [http://stackoverflow.com/a/22945492/3924118](http://stackoverflow.com/a/22945492/3924118)
- [http://stackoverflow.com/q/23055236/3924118](http://stackoverflow.com/q/23055236/3924118)
- [https://www.cs.usfca.edu/~galles/JavascriptVisual/DisjointSets.html](https://www.cs.usfca.edu/~galles/JavascriptVisual/DisjointSets.html)
to visualize how disjoint-sets work.
## TODO
- Deletion operation (OPTIONAL, since it's usually not part of the interface of a disjoint-set data structure)
- Pretty-print(x), for some element x in the disjoint-set data structure.
- Implement the version explained [here](http://algs4.cs.princeton.edu/15uf/)
"""


class DSNode:

def __init__(self, x, rank=0):
# This attribute can contain any hashable value.
self.value = x
Expand Down Expand Up @@ -97,7 +89,6 @@ def __repr__(self):


class DSForests:

def __init__(self):
# keeps tracks of the DSNodes in this disjoint-set forests.
self.sets = {}
Expand Down
13 changes: 9 additions & 4 deletions ands/ds/HashTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
# -*- coding: utf-8 -*-

"""
# Meta info
Author: Nelson Brochado
Creation: June, 2015
Created: 01/06/2015
Updated: 21/02/2016
Last update: 21/02/16
# Description
Hash table that re-sizes if no more slot is available.
The process of re-sizing doubles the current capacity of the hash table each time (for now).
Expand All @@ -19,10 +23,11 @@
h[12] = 3
print(h[12])
## References
- [http://interactivepython.org/runestone/static/pythonds/SortSearch/Hashing.html](http://interactivepython.org/runestone/static/pythonds/SortSearch/Hashing.html)
# References
- [http://interactivepython.org/runestone/static/pythonds/SortSearch/Hashing.html](http://interactivepython.org/runestone/static/pythonds/SortSearch/Hashing.html)
- [http://stackoverflow.com/questions/279539/best-way-to-remove-an-entry-from-a-hash-table](http://stackoverflow.com/questions/279539/best-way-to-remove-an-entry-from-a-hash-table)
"""

from tabulate import tabulate
Expand Down
Loading

0 comments on commit 64335f5

Please sign in to comment.