Try   HackMD

2024q1 Homework4 (quiz3+4)

contributed by < Ken-LuWeiRu >

2024q1 第 4 週測驗題

XTree 是自平衡二元搜尋樹,它結合了 AVL 樹和紅黑樹的特點,旨在提供快速插入和刪除操作同時保持相對優良的平衡性。

  • xt_create 函數初始化一棵空樹。
  • xt_destroy__xt_destroy 函數負責遞迴銷毀樹及其節點。
  • xt_firstxt_last 函數分別找到子樹中最小和最大的節點。
  • xt_rotate_leftxt_rotate_right 函數執行基本的樹旋轉操作以幫助維持或恢復平衡。
  • xt_balancext_max_hint 函數計算樹的平衡因子和更新節點的平衡提示。
  • xt_update 函數根據節點的平衡情況進行調整以維護整體樹的平衡。
  • __xt_find__xt_find2 函數用於在樹中查找特定鍵值的節點。
  • __xt_insertxt_insert 函數實現節點的插入操作。
  • xt_replace_rightxt_replace_left 函數在刪除操作中用來替換節點。
  • __xt_removext_remove 函數處理刪除操作,包括選擇適當的替代節點並更新樹以維持平衡。






XTree



struct1

XTree

root

cmp

create_node

destroy_node



struct2

xt_node

left

right

hint

parent



struct1:root->struct2


root



struct3

xt_node

left

right

hint

parent



struct2:left->struct3:parent





struct4

xt_node

left

right

hint

parent



struct2:right->struct4:parent





這張圖顯示了 XTree 的基本結構,包括根節點和其他節點之間的關係。每個節點可以有左右子節點和一個父節點。

程式碼解釋

xt_rotate_left

以下是左旋操作的圖示,紅色虛線表示旋轉後的新連接:







G



N

n



L

l



N->L


Left child



LR

l's right



N->LR


New Left child



L->N


New Right child



L->LR


Right child



P

p



P->N


Parent



P->L


New Parent



左旋轉前後對比圖(白色是之前,藍色之後):







G



P

p (Parent)



N

n (Node to rotate)



P->N





P2

p




L

l (Left child of n)



N->L





L2

l




LL

ll (Left child of l)



L->LL





LR

lr (Right child of l)



L->LR





N2

n




LL2

ll




LR2

lr




P2->L2





N2->LL2





L2->N2





L2->LR2





xt_rotate_right

以下是右旋操作的圖示,紅色虛線表示旋轉後的新連接:







G



N

n



R

r



N->R


Right child before rotation



RL

r's left



N->RL


New Right child



R->N


New Left child



R->RL


Left child of r



P

p



P->N


Parent



P->R


New Parent



xt_update

這是 xt_update 函數的操作示意圖:







%0


cluster_0

xt_update



Start

Start xt_update



CheckNull

Is node NULL?



Start->CheckNull





CalcBalance

Calculate balance factor



CheckNull->CalcBalance


No



End

End xt_update



CheckNull->End


Yes



RotateRight

Rotate right



CalcBalance->RotateRight


balance < -1



RotateLeft

Rotate left



CalcBalance->RotateLeft


balance > 1



UpdateHint

Update hint



CalcBalance->UpdateHint


balance is ok



RotateRight->UpdateHint





RotateLeft->UpdateHint





UpdateHint->End





xt_insert & __xt_insert







xt_insert



start

開始插入



find

使用 __xt_find
尋找插入位置



start->find





check

節點是否已存在?



find->check





create

創建新節點



check->create






end

結束插入



check->end






insert

調用 __xt_insert
進行實際插入



create->insert





update

調用 xt_update
平衡樹



insert->update





update->end





__xt_remove xt_replace_right xt_replace_left

__xt_remove 使用 xt_replace_right 或 xt_replace_left 來替換節點,在使用 xt_update 來平衡 xtree

以下是 xt_replace_right ,紅色虛線表示旋轉後的新連接:







%0



n

Node n

key: n_key



r

Replacement Node r

key: r_key



n->r


right child



nr

Right Child of n

key: nr_key



n->nr


right child



r->n


left child (after rotation)



rr

Right Child of r

key: rr_key



r->rr


right child



p

Parent Node p

key: p_key



p->n


left or right child



p->r


new child



rp

Parent of Replacement Node rp

key: rp_key



rp->r


left or right child



rp->rr


new child



可改進之處

與 AVL 樹和紅黑樹相比,XTree 在插入和刪除操作中尋求更快的執行速度,但可能在保持完美平衡方面稍遜色。改進的方向包括:

  1. 平衡策略的優化: 目前的平衡操作是在插入或刪除後立即執行。考慮引入更智能的策略,例如僅在必要時進行平衡操作,可以減少總體平衡次數,提升效率。
  2. 動態調整策略: 根據樹的大小和操作的特性(如連續插入或刪除),動態調整平衡策略,可能會提高效率。
  3. 記憶體管理: 樹的節點操作經常伴隨記憶體分配和釋放,可以考慮使用記憶體池來管理節點的分配和釋放,以減少記憶體分配的開銷。

循序輸入和亂序輸入的狀況

  • 循序輸入: 在連續插入排序資料時,某些二元搜尋樹(如未平衡的 BST)可能退化成鏈表,導致性能下降。對於 XTree,即使在面對循序輸入時,也應保持一定程度的平衡,避免退化成鏈表。
  • 亂序輸入: 亂序輸入一般有助於保持樹的平衡,但過度的平衡操作可能會導致不必要的性能損耗

XTree 需要在快速插入/刪除與維持樹平衡之間找到適當的平衡點。

代做事項