Skip to content

Commit

Permalink
fix binary tree, heap cheatsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 12, 2023
1 parent a628ecb commit 8f963ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions doc/cheatsheet/binary_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## 0) Concept

- Definition : a binary tree is a tree data structure in which each node has *at most* two children, referred to as the left child and the right child.
- BST (binary search tree), Heap.. are all binary tree
- [wiki](https://en.wikipedia.org/wiki/Binary_tree)
- [Binary Tree - 演算法筆記](https://web.ntnu.edu.tw/~algo/BinaryTree.html)

- Complete Tree to Array
- Note if we use an `array` to represent the `complete binary tree`,and `store the root node at index 1`
- so, index of the `parent` node of any node is `[index of the node / 2]`
Expand Down
15 changes: 9 additions & 6 deletions doc/cheatsheet/heap.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
- Intro
- https://leetcode.com/explore/learn/card/heap/
- In many CS applications, we only need to `access the largest or smallest element` in the dataset. We DO NOT care about `the order of other data in the data set`. How do we efficiently access the largest or smallest element in the current dataset? The answer would be `Heap`.

- Priority Queue
- a priority queue is an `abstract data type` similar to a regular queue or stack data structure in which each element additionally has a `"priority"` associated with it. In a priority queue, an element with high priority is served before an element with low priority.
- Heap != Priority Queue
- `Heap != Priority Queue`
- Priority Queue is a abstract data type
- Heap is a way to implemenrt Priority Queue

- Heap
- a special `completed binary tree`
- a special `completed binary tree` (heap is binary tree)
- The value of each node must be no greater than (or no less than) the value of its child nodes.
- Properties:
- Insertion of an element into the Heap has a time complexity of `O( log N)`
Expand Down Expand Up @@ -80,15 +81,17 @@
- complexity
- push/pop (each)
- time : O(log(N))
- spce : O(N)
- ref : https://stackoverflow.com/questions/38806202/whats-the-time-complexity-of-functions-in-heapq-library#:~:text=heapq%20is%20a%20binary%20heap,O(n%20log%20n)
- space : O(N)
- ref : [SF - whats-the-time-complexity-of-functions-in-heapq-library](https://stackoverflow.com/questions/38806202/whats-the-time-complexity-of-functions-in-heapq-library#:~:text=heapq%20is%20a%20binary%20heap,O(n%20log%20n))
- so, if implement push/pop on all elements, will cost
- time : O(N log(N))
- spce : O(N)
- space : O(N)
- Basic API
- heapify : transform list to heap
- heappush : put element into heap
- heappop : get (remove) element from heap
- heappop : get (remove) top element from heap
- Min heap : delete top element from the Min Heap
- Max heap : delete top element from the Max Heap
- heappushpop : heappush then heappop (put first, then pop)
- heapreplace : heappop then heappush (pop first, then put)
- nlargest : return top N large elements
Expand Down

0 comments on commit 8f963ce

Please sign in to comment.