Sample code for a number of classic data structures implemented in Python
- Jim Grant
- Johnson Jew
- Jim Grant
- Johnson Jew
http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance
- Jim Grant
- Johnson Jew
- Jim Grant
- Johnson Jew
A doubly-linked list should be used instead of a standard linked list whenever the data can be added or removed from either end. For example, if one were modeling a series of lego blocks, with the desire to either put a new block on top, or put the entire stack on top of another block. Any model that needs to build elements "out from the middle", instead of from one end or another, is an ideal use case.
METHODS size(): returns number of nodes balance(): returns positive, negative or zero value as it relates to the balance of the tree. depth(): returns the depth of the tree as an integer insert(value): creates a new node of a given value and places it appropriately. TRAVERSAL METHODS in_order() pre_order() post_order() breadth_first() ^ returns a list of nodes in the order specified. Inspiration for depth and balance methods taken from Jonathan Stallings. delete(node): removes the node in question and moves other nodes accordingly.
Iterate through the list from left to right, comparing each value to the previous values until it finds one that it is greater than or it reaches the beginning of the list.
Benefits:
- Simple to implement
- Fast at small arrays
Best Case: O(n)
Worst Case: O(n^2)
Average Case: O(n^2)
- Jim Grant
- Megan Slater
Return an integer representing whether all open parentheses are closed in order in the provided input.
- Jim Grant
- Johnson Jew
sp_dijkstra - An implementation of Dijkstra's algorithm. Iterates through all "unvisited" nodes, selecting the one with the lowest estimated weight each time to find the lowest-cost route to all other nodes in the graph. Once this is done, it follows the chain backwards from the destination node to return the path.
sp_bellmanford - An implementation of the Bellman-Ford algorithm. Unlike Dijkstra's algorithm, Bellman-Ford does not preferentially select nodes while calculating weights. This makes it take longer, but allows it to process graphs which have negatively-weighted edges.
- Jim Grant
- Megan Slater The hash table hashes and stores keys and their values for quick retreival. Methods include:
get(key): returns the value stored with the given key set(key, val): stores the given val using the given key hash(key): hashes the key provided