→ +3 | 3 |
→ +2 | 2 | | 2 |
| | → +1 | 1 | | 1 | | 1 |
| 3 | → -3 | 2 | | 2 | → -2 | 1 | | 1 | | 1 | → -1 | |
→ +3 | 3 |
→ +2 | 2 | | 2 |
| | → +1 | 1 | | 1 | | 1 |
| 3 | | 2 | | 3 | | 1 | → -1 | 2 | → -2 | 3 | → -3 | |
// 好比 值越小優先級越高 | 1 : priority 3 | | 2 : priority 2 | | 3 : priority 1 |
------- ------- ------- | v : 1 | → | v : 2 | → | v : 3 | | next | next | next | next | next | ------- ------- -------
-------- -------- -------- | v : 1 | next | v : 2 | next | v : 3 | | next | → | next | → | next | | return | ← | return | ← | return | -------- return -------- return --------
const [num, setNum] = useState(1);
const [str, setStr] = useState("a");
-------------------- | baseState : 1 | | baseUpdate : null | | memoizedState : 1 | | next | | queue | -------------------- next ↓ -------------------- | baseState : "a" | | baseUpdate : null | | memoizedState : "a"| | next | | queue | -------------------- next ↓ -------------------- | baseState : null | | baseUpdate : null | | memoizedState | | next : null | | queue : null | --------------------
const mySet = new Set([1, 2, 3, 3, 4, 5, 6, 7]); // Set(7) {1, 2, 3, 4, 5, 6, 7}
A ∪ B : [ 1, 2, 3 ] ∪ [ 2, 3, 4 ] => [ 1, 2, 3, 4 ] A ∩ B : [ 1, 2, 3 ] ∩ [ 2, 3, 4 ] => [ 2, 3 ] A - B : [ 1, 2, 3 ] - [ 2, 3, 4 ] => [ 1 ] B - A : [ 2, 3, 4 ] - [ 1, 2, 3 ] => [ 4 ] B ⊆ A : [ 2, 3 ] ⊆ [ 1, 2, 3 ] => [ 2, 3 ]
const myMap = new Map([["1", 1], [1, "1"]]); // Map(2) {"1" => 1, 1 => "1"}
--------------------------------------------------------- | key | hash fn | hash key | hash table | --------------------------------------------------------- | "aa" | fn("aa") | 149 | [149] aa@gmail.com | --------------------------------------------------------- | "bb" | fn("bb") | 153 | [153] bb@gmail.com | --------------------------------------------------------- | "cc" | fn("cc") | 157 | [157] cc@gmail.com | ---------------------------------------------------------
// 表 var table = []; // 舉個例子,好比一個簡單的散列計算函數 var hashCode = key => { let hash = 0; for (let i = 0; i < key.length; i++) { hash = hash + key.charCodeAt(i); } return hash; }; // 存值 var put = (key, value) => { var position = hashCode(key); table[position] = value; }; // 取值 var query = key => { var position = hashCode(key); return table[position]; }; put("hy", "hello yeshou"); put("ht", "hello tutu"); put("hw", "hello world"); console.log(query("hy")); // output hello yeshou console.log(query("ht")); // output hello tutu console.log(query("hw")); // output hello world put("yh", "hello haha"); console.log(query("hy")); // output hello world console.log(query("yh")); // output hello world
var hashCode = key => { let hash = 5381; // 初始 hash 值 for (let i = 0; i < key.length; i++) { hash = hash * 33 + key.charCodeAt(i); // 乘積因子 33 } return hash % 1013; // mod 5381 }; console.log(hashCode("yh"), hashCode("hy")); // output 762 218
(root) 10 / \ 8 9 / / | \ \ | 1 2 4 5 7 15
(root) 11 / \ 7 15 / \ / \ 5 9 13 17 / \ / \ / \ / \ 4 6 8 10 12 14 16 18
function traverse(node, callback) { if (node !== null) { callback(node.v); traverse(node.left, callback); traverse(node.right, callback); } }
function traverse(node, callback) { if (node !== null) { traverse(node.left, callback); callback(node.v); traverse(node.right, callback); } }
function traverse(node, callback) { if (node !== null) { traverse(node.left, callback); traverse(node.right, callback); callback(node.v); } }
(root) 13(B) / \ 8(R) 17(R) / \ / \ 1(B) 11(B) 15(B) 23(B) / \ / \ / \ / \ n 6(R) n n n n 22(R) 25(R) // 若是僅插入 14 (root) 13(B) / \ 8(R) 17(R) / \ / \ 1(B) 11(B) 15(B) 23(B) / \ / \ / \ / \ n 6(R) n n 14(R) n 22(R) 25(R) / \ n n // 若是僅插入 21 (root) 17(B) / \ 8(R) 23(R) / \ / \ 1(B) 13(B) 22(B) 25(B) / \ / \ / \ / \ n 6(R) 11(R) 15(R) 21(R) n n n / \ / \ / \ / \ n n n n n n n n // 若是僅刪除 15 (root) 13(B) / \ 6(R) 23(R) / \ / \ 1(B) 8(B) 22(B) 25(B) / \ / \ / \ / \ n n n n n n n n
// 無(方)向圖 A - B - C - D | | | E F - G - H | | -I- // 有(方)向圖 A → B → C → D ↓ ↓ ↓ E F → G → H ↓ ↓ →I←
圖的遍歷能夠用來尋找特定的節點或尋找兩個節點之間的路徑,檢查節點之間是否連通或者是否含有環等。java
A E
A B F G I
A B F G H I
A B C D H I
A E
A B C
A B F
A B C D
A B F G
A B C D H
A B F G I
A B F G H
A B C D H I
A B F G H I