diff --git a/trees/README.md b/trees/README.md index 4559871..b43c543 100644 --- a/trees/README.md +++ b/trees/README.md @@ -1,8 +1,4 @@ -## trees, heaps, tries, graphs - -
- -### trees +## trees
@@ -115,78 +111,12 @@ def postorder(self, root): --- -### heaps + +### examples
-* a heap is a binary tree with two properties: it must have all of its nodes in a specific order and its shape must be complete (all the levels of the tree must be completely filled except maybe for the last one and the last level must have the left-most nodes filled, always). -* a heap's root node must have all its children either greater than or equal to its children. -* since you always remove the root, insertion and deletion takes O(log(n)). -* duplicate values are allowed. -* a **min heap** is a complete binary tree where each node is smaller than its children (the root is the min element). two key operations are: - - insert: always by the element at the bottom, at the most rightmost post - - extract_min: the minimum element is always on top, and removing it is the trickiest part: - 1. remove and swap it with the last element (the bottom most rightmost) - 2. the bubble down, swapping it with one of its children until the min-heap is properly restored (there is no order between right and left and it takes O(log n) time. -* a heap could also be represented with a queue (array). in this case, the index of the parent node = [(n-1)/2]. -* a priority queue is a queue of data structures with some additional properties: - 1. every item has a priority (usually an integer) - 2. an item with a high priority is dequeued before an item with low priority - 3. two items with an equal priority are dequeued based on their order in the queue - - -
- ----- - -### n-ary tree - -
- -* if a tree is a rooted tree in which each node has no more than N children, it's called N-ary tree. - - -
- ----- - -### tries - -
- -* tries, also called prefix tree, are a variant of n-ary tree in which characters are stored in each node. -* each trie node represents a string (a prefix) and each path down the tree represents a word. note that not all the strings represented by trie nodes are meaningful. -* the root is associated with the empty string. -* the * nodes (null nodes) are often used to indicate complete words (usually represented by a special type of child) or a boolean flag that terminates the parent node. -* a node can have anywhere from 1 through alphabet_size + 1 child. -* can be used to store the entire english language for quick prefix lookup (O(k), where k is the length of the string). they are also widely used on autocompletes, spell checkers, etc. -* tries structures can be represented by arrays and maps or trees. - -
- -#### insertion - -
- -* similar to a bst, when we insert a value to a trie, we need to decide which path to go depending on the target value we insert. -* the root node needs to be initialized before you insert strings. - -
- -#### search - -
- -* all the descendants of a node have a common prefix of the string associated with that node, so it should be easy to search if there are any words in the trie that starts with the given prefix. -* we go down the tree depending on the given prefix, once we cannot find the child node, the search fails. -* we can also search for a specific word rather than a prefix, treating this word as a prefix and searching in the same way as above. -* if the search succeeds, we need to check if the target word is only a prefix of words in the trie or if it's exactly a word (for example, by adding a boolean flag). - -
- ---- - -### `Tree.py` +#### `Tree.py`