From 906622f5a49e53119b776a674f49a41d188a8c37 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sat, 19 Oct 2024 21:05:00 +0800 Subject: [PATCH] 230th Commit --- README.md | 2 +- algorithms/heap/README.md | 41 +++++++++++++++++++++++++++++++++++++++ algorithms/tree/README.md | 21 ++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 algorithms/tree/README.md diff --git a/README.md b/README.md index b95a822..8c356ab 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ $ pnpm test twoSum.test.ts - [堆疊 (Stack)](./algorithms/stack/README.md) - [佇列 (Queue)](./algorithms/queue/README.md) - [雜湊表 (Hash Table)](./algorithms/hash-table/README.md) -- 樹 (Tree) +- [樹 (Tree)](./algorithms/tree/README.md) - [堆積 (Heap)](./algorithms/heap/README.md) - [圖 (Graph)](./algorithms/graph/README.md) - 字典樹 (Trie) diff --git a/algorithms/heap/README.md b/algorithms/heap/README.md index 0b61e5f..97f78ff 100644 --- a/algorithms/heap/README.md +++ b/algorithms/heap/README.md @@ -4,6 +4,20 @@ 父節點的值總是小於或等於其子節點的值。 +```ts +[1, 3, 6, 5, 9, 8, 15]; +``` + +```mermaid +graph TD + 0[1] --> 1[3] + 0 --> 2[6] + 1 --> 3[5] + 1 --> 4[9] + 2 --> 5[8] + 2 --> 6[15] +``` + ```ts type Comparator = (a: T, b: T) => number; @@ -31,6 +45,20 @@ const minHeap = new Heap(Heap.minComparator); 父節點的值總是大於或等於其子節點的值。 +```ts +[15, 9, 8, 1, 3, 6, 5]; +``` + +```mermaid +graph TD + 0[15] --> 1[9] + 0 --> 2[8] + 1 --> 3[1] + 1 --> 4[3] + 2 --> 5[6] + 2 --> 6[5] +``` + ```ts type Comparator = (a: T, b: T) => number; @@ -47,9 +75,22 @@ class Heap { if (b < a) return -1; return 0; } + + // 元素入堆積 + push(value: T): void { + // + } } ``` ```ts const maxHeap = new Heap(Heap.maxComparator); + +maxHeap.push(15); +maxHeap.push(9); +maxHeap.push(8); +maxHeap.push(1); +maxHeap.push(3); +maxHeap.push(6); +maxHeap.push(5); ``` diff --git a/algorithms/tree/README.md b/algorithms/tree/README.md new file mode 100644 index 0000000..14816b5 --- /dev/null +++ b/algorithms/tree/README.md @@ -0,0 +1,21 @@ +# 樹 (Tree) + +## 二元樹 (Binary Tree) + +```ts +class TreeNode { + val: T; + left: TreeNode | null; + right: TreeNode | null; + + constructor(val: T, left: TreeNode | null = null, right: TreeNode | null = null) { + this.val = val; + this.left = left; + this.right = right; + } +} +``` + +```ts +const n1 = new TreeNode(1); +```