or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
2023q1 Homework4 (quiz4)
contributed by <
Joshualee0321
>開發環境
測驗一
rb.h
歡迎各位幫我指正我哪裡解釋錯誤。筆記和程式碼本來就要讓各方指教,不用特別提及。
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →理解 rb.h 的實作
展開該巨集,利用
rb_gen(static, ex_, ex_t, ex_node_t, ex_link, ex_cmp)
把以下的巨集展開之後可以得到避免非必要的項目標示 (亦即
*
開頭的描述),並改進你的漢語表達。- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →接下來我們可以觀察
ex_insert
的實作方法,其中大致可以分成幾個部分ex_path_entry_t path[RB_MAX]
以及ex_path_entry_t *pathp
作為 iteratorex_path_entry_t
的陣列當中15
,則會先以BST
的特性找到該插入的地方並且插入注意這邊先行設定愈插入的 node 為紅色
此時的
path
會長這樣依照
rb_insert
中的pathp->node = node
,把D:15
插入在NULL
中,接下來就進入到程式碼的第二個片段由於在上一個迴圈中
pathp
的位置如下故要把原本的指針往前調整如下
所以才會在這個迴圈先做一次
pathp--
往前走一格。再來,把以上
if
的部分展開之後可以看到else
在下一個部分再看if(pathp->cmp < 0){...} else{...}
,並且由於是左傾斜的樹,他只需要對右邊的特殊狀況處理即可RB_t
並不平均,那他就會旋轉,圖示如下當然,這邊只考慮了往左的狀況,那如果往右邊呢?
最後再根據紅黑樹的特性,把樹根設定為黑色即可
rbtree-root = path->node; rbtn_black_set(ex_node_t, ex_link, rbtree->root);
以上為 linux 左傾斜紅黑樹插入的實作
`AAAA` = `rbtn_black_set` `BBBB` = `tred` `CCCC` = `rbtn_red_set`以下為
rb_tree
的刪除部分,由於太長,這邊只列出想法以及答案此程式碼也可以分成幾段來觀看,並且於先前
insert
一樣,都會創建一個iteration_list
來記錄當前走過的路。第一段successor
) 互相交換,並且把要刪除的點放在path[-1]
,反之,則調整顏色successor
以及設定顏色後) 開始從當前位置往前調整紅黑樹根據此網誌得知紅黑樹的刪除過程參照更權威的書籍和文獻,例如大學教科書,避免從語焉不詳的網誌學習。
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →首先先來看到程式碼片段的 FFFF
欲刪除的節點為目前節點的右節點,為了要使刪除時維持左傾斜的特性,作者選擇右旋轉兩次,以下為圖示
在最後一步之前會轉化為這樣
在經過最後一次左旋轉即可得到左傾斜的紅黑樹
如此一來即恢復平衡
在
GGGG
則是可以直接以rbtn_rotate_right
填入來恢復平衡以上為 linux 左傾斜紅黑樹刪除的想法
DDDD
=0
: 為了要記錄刪除的點EEEE
=!rbtn_right_get(x_type, x_field, node)
: 為了看是否右邊沒有東西FFFF
=rbtn_rotate_left
GGGG
=rbtn_rotate_right
測驗二
TimSort
O(nlogn)
(comparison based sort 的最佳時間複雜度),其中利用了insertion_sort
跟merge_sort
來分別處理比較少的資料以及比較多的資料。__inplace_merge
為了要合併兩個
run
並且保持inplace
, 這邊採用的方法為反轉裡面的資料來達到排序的效果insertion_sort
排序,所以為升冪排序,以下給予圖片解說來解釋裡面程式碼的作用[1, 2, 4, 5]
跟[3, 6, 10]
流程如下:
cur_1
指到比first_2
更小的位置 利用while(cur_1 < last_2 && !cmp(cur_2, cur_1)) cur_1 += size;
cur_2
指到比cur_1
更小的位置 利用while(cur_2 < last_2 && cmp(cur_2, cur_1)) cur_2 += size;
即可得到目前的狀況
接下來把
cur_1
到first_2
之前相反過來__reverse(cur_1, first_2, size)
再來將
first_2
與cur_2
之前相反過來__reverse(first_2, cur_2, size)
即可得到當前的陣列
可以注意到,根據剛剛的翻轉,中間已經呈現了一個降冪的關係,此時只需要重新反轉一次 即可得到最後排序好的兩個
run
__reverse(cur_1, cur_2, size)
為避免單個 run 太長,導致
merge
速度變差tim_sort
的人有稍微考慮到,當兩個run
的長度不一致,達到非常懸殊的等級的時候,假設兩個run
分別為A_run, (len(A_run) == 10)
以及B_run, (len(B_run) == 100)
,此時的merge
會在兩個已經sort好的陣列中做出相當多不需要的操作,而當兩個run
的長度差不多時,此時的merge
才算比較有效率。merge_rule
如下意即若當前這個
run
加上前一個run
沒有比前兩個run
大時,不會進入merge
的片段各答案如下: `AAAA` : `__reverse(cur_1, cur_2, size)` `BBBB` : `top - 1` `CCCC` : `top - 2` `DDDD` : `first + MIN_RUN * size` 我想這邊應該不需要太過解釋,就只是沒有超過時取最小 `EEEE` : `top--` 為了要合併 top 的內容 `FFFF` : `stack[top - 1].last = stack[top].last` 一個一個合併不用抄寫答案,只要專注程式碼本身和你的理解。
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →測驗三
RB_LOG2_MAX_NODES
可以根據
rb_tree
的定義,樹高為2 * log(n + 1)
以及不超出記憶體限制的規則得到以下結論。sum_tree
沒必要將程式碼註解改為中文,直接解讀程式碼的作用,避免逐行解說,而該揣摩設計者的思維。
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →