mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-30 11:57:13 +00:00
build
This commit is contained in:
@@ -131,7 +131,9 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
|
||||
### 二元樹的陣列表示 ###
|
||||
# 使用 nil 來表示空位
|
||||
tree = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -1256,7 +1258,87 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="array_binary_tree.rb"
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
### 陣列表示下的二元樹類別 ###
|
||||
class ArrayBinaryTree
|
||||
### 建構子 ###
|
||||
def initialize(arr)
|
||||
@tree = arr.to_a
|
||||
end
|
||||
|
||||
### 串列容量 ###
|
||||
def size
|
||||
@tree.length
|
||||
end
|
||||
|
||||
### 獲取索引為 i 節點的值 ###
|
||||
def val(i)
|
||||
# 若索引越界,則返回 nil ,代表空位
|
||||
return if i < 0 || i >= size
|
||||
|
||||
@tree[i]
|
||||
end
|
||||
|
||||
### 獲取索引為 i 節點的左子節點的索引 ###
|
||||
def left(i)
|
||||
2 * i + 1
|
||||
end
|
||||
|
||||
### 獲取索引為 i 節點的右子節點的索引 ###
|
||||
def right(i)
|
||||
2 * i + 2
|
||||
end
|
||||
|
||||
### 獲取索引為 i 節點的父節點的索引 ###
|
||||
def parent(i)
|
||||
(i - 1) / 2
|
||||
end
|
||||
|
||||
### 層序走訪 ###
|
||||
def level_order
|
||||
@res = []
|
||||
|
||||
# 直接走訪陣列
|
||||
for i in 0...size
|
||||
@res << val(i) unless val(i).nil?
|
||||
end
|
||||
|
||||
@res
|
||||
end
|
||||
|
||||
### 深度優先走訪 ###
|
||||
def dfs(i, order)
|
||||
return if val(i).nil?
|
||||
# 前序走訪
|
||||
@res << val(i) if order == :pre
|
||||
dfs(left(i), order)
|
||||
# 中序走訪
|
||||
@res << val(i) if order == :in
|
||||
dfs(right(i), order)
|
||||
# 後序走訪
|
||||
@res << val(i) if order == :post
|
||||
end
|
||||
|
||||
### 前序走訪 ###
|
||||
def pre_order
|
||||
@res = []
|
||||
dfs(0, :pre)
|
||||
@res
|
||||
end
|
||||
|
||||
### 中序走訪 ###
|
||||
def in_order
|
||||
@res = []
|
||||
dfs(0, :in)
|
||||
@res
|
||||
end
|
||||
|
||||
### 後序走訪 ###
|
||||
def post_order
|
||||
@res = []
|
||||
dfs(0, :post)
|
||||
@res
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
@@ -137,9 +137,9 @@ AVL 樹既是二元搜尋樹,也是平衡二元樹,同時滿足這兩類二
|
||||
right: TreeNode | null; // 右子節點指標
|
||||
constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -222,7 +222,18 @@ AVL 樹既是二元搜尋樹,也是平衡二元樹,同時滿足這兩類二
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
### AVL 樹節點類別 ###
|
||||
class TreeNode
|
||||
attr_accessor :val # 節點值
|
||||
attr_accessor :height # 節點高度
|
||||
attr_accessor :left # 左子節點引用
|
||||
attr_accessor :right # 右子節點引用
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -455,9 +466,19 @@ AVL 樹既是二元搜尋樹,也是平衡二元樹,同時滿足這兩類二
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="avl_tree.rb"
|
||||
[class]{AVLTree}-[func]{height}
|
||||
### 獲取節點高度 ###
|
||||
def height(node)
|
||||
# 空節點高度為 -1 ,葉節點高度為 0
|
||||
return node.height unless node.nil?
|
||||
|
||||
[class]{AVLTree}-[func]{update_height}
|
||||
-1
|
||||
end
|
||||
|
||||
### 更新節點高度 ###
|
||||
def update_height(node)
|
||||
# 節點高度等於最高子樹高度 + 1
|
||||
node.height = [height(node.left), height(node.right)].max + 1
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -638,7 +659,14 @@ AVL 樹既是二元搜尋樹,也是平衡二元樹,同時滿足這兩類二
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="avl_tree.rb"
|
||||
[class]{AVLTree}-[func]{balance_factor}
|
||||
### 獲取平衡因子 ###
|
||||
def balance_factor(node)
|
||||
# 空節點平衡因子為 0
|
||||
return 0 if node.nil?
|
||||
|
||||
# 節點平衡因子 = 左子樹高度 - 右子樹高度
|
||||
height(node.left) - height(node.right)
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -913,7 +941,19 @@ AVL 樹的特點在於“旋轉”操作,它能夠在不影響二元樹的中
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="avl_tree.rb"
|
||||
[class]{AVLTree}-[func]{right_rotate}
|
||||
### 右旋操作 ###
|
||||
def right_rotate(node)
|
||||
child = node.left
|
||||
grand_child = child.right
|
||||
# 以 child 為原點,將 node 向右旋轉
|
||||
child.right = node
|
||||
node.left = grand_child
|
||||
# 更新節點高度
|
||||
update_height(node)
|
||||
update_height(child)
|
||||
# 返回旋轉後子樹的根節點
|
||||
child
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -1174,7 +1214,19 @@ AVL 樹的特點在於“旋轉”操作,它能夠在不影響二元樹的中
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="avl_tree.rb"
|
||||
[class]{AVLTree}-[func]{left_rotate}
|
||||
### 左旋操作 ###
|
||||
def left_rotate(node)
|
||||
child = node.right
|
||||
grand_child = child.left
|
||||
# 以 child 為原點,將 node 向左旋轉
|
||||
child.left = node
|
||||
node.right = grand_child
|
||||
# 更新節點高度
|
||||
update_height(node)
|
||||
update_height(child)
|
||||
# 返回旋轉後子樹的根節點
|
||||
child
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -1648,7 +1700,34 @@ AVL 樹的特點在於“旋轉”操作,它能夠在不影響二元樹的中
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="avl_tree.rb"
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
### 執行旋轉操作,使該子樹重新恢復平衡 ###
|
||||
def rotate(node)
|
||||
# 獲取節點 node 的平衡因子
|
||||
balance_factor = balance_factor(node)
|
||||
# 左遍樹
|
||||
if balance_factor > 1
|
||||
if balance_factor(node.left) >= 0
|
||||
# 右旋
|
||||
return right_rotate(node)
|
||||
else
|
||||
# 先左旋後右旋
|
||||
node.left = left_rotate(node.left)
|
||||
return right_rotate(node)
|
||||
end
|
||||
# 右遍樹
|
||||
elsif balance_factor < -1
|
||||
if balance_factor(node.right) <= 0
|
||||
# 左旋
|
||||
return left_rotate(node)
|
||||
else
|
||||
# 先右旋後左旋
|
||||
node.right = right_rotate(node.right)
|
||||
return left_rotate(node)
|
||||
end
|
||||
end
|
||||
# 平衡樹,無須旋轉,直接返回
|
||||
node
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -2039,9 +2118,28 @@ AVL 樹的節點插入操作與二元搜尋樹在主體上類似。唯一的區
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="avl_tree.rb"
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
### 插入節點 ###
|
||||
def insert(val)
|
||||
@root = insert_helper(@root, val)
|
||||
end
|
||||
|
||||
[class]{AVLTree}-[func]{insert_helper}
|
||||
### 遞迴插入節點(輔助方法)###
|
||||
def insert_helper(node, val)
|
||||
return TreeNode.new(val) if node.nil?
|
||||
# 1. 查詢插入位置並插入節點
|
||||
if val < node.val
|
||||
node.left = insert_helper(node.left, val)
|
||||
elsif val > node.val
|
||||
node.right = insert_helper(node.right, val)
|
||||
else
|
||||
# 重複節點不插入,直接返回
|
||||
return node
|
||||
end
|
||||
# 更新節點高度
|
||||
update_height(node)
|
||||
# 2. 執行旋轉操作,使該子樹重新恢復平衡
|
||||
rotate(node)
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -2640,9 +2738,41 @@ AVL 樹的節點插入操作與二元搜尋樹在主體上類似。唯一的區
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="avl_tree.rb"
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
### 刪除節點 ###
|
||||
def remove(val)
|
||||
@root = remove_helper(@root, val)
|
||||
end
|
||||
|
||||
[class]{AVLTree}-[func]{remove_helper}
|
||||
### 遞迴刪除節點(輔助方法)###
|
||||
def remove_helper(node, val)
|
||||
return if node.nil?
|
||||
# 1. 查詢節點並刪除
|
||||
if val < node.val
|
||||
node.left = remove_helper(node.left, val)
|
||||
elsif val > node.val
|
||||
node.right = remove_helper(node.right, val)
|
||||
else
|
||||
if node.left.nil? || node.right.nil?
|
||||
child = node.left || node.right
|
||||
# 子節點數量 = 0 ,直接刪除 node 並返回
|
||||
return if child.nil?
|
||||
# 子節點數量 = 1 ,直接刪除 node
|
||||
node = child
|
||||
else
|
||||
# 子節點數量 = 2 ,則將中序走訪的下個節點刪除,並用該節點替換當前節點
|
||||
temp = node.right
|
||||
while !temp.left.nil?
|
||||
temp = temp.left
|
||||
end
|
||||
node.right = remove_helper(node.right, temp.val)
|
||||
node.val = temp.val
|
||||
end
|
||||
end
|
||||
# 更新節點高度
|
||||
update_height(node)
|
||||
# 2. 執行旋轉操作,使該子樹重新恢復平衡
|
||||
rotate(node)
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
@@ -316,7 +316,26 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_search_tree.rb"
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
### 查詢節點 ###
|
||||
def search(num)
|
||||
cur = @root
|
||||
|
||||
# 迴圈查詢,越過葉節點後跳出
|
||||
while !cur.nil?
|
||||
# 目標節點在 cur 的右子樹中
|
||||
if cur.val < num
|
||||
cur = cur.right
|
||||
# 目標節點在 cur 的左子樹中
|
||||
elsif cur.val > num
|
||||
cur = cur.left
|
||||
# 找到目標節點,跳出迴圈
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
cur
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -773,7 +792,38 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_search_tree.rb"
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
### 插入節點 ###
|
||||
def insert(num)
|
||||
# 若樹為空,則初始化根節點
|
||||
if @root.nil?
|
||||
@root = TreeNode.new(num)
|
||||
return
|
||||
end
|
||||
|
||||
# 迴圈查詢,越過葉節點後跳出
|
||||
cur, pre = @root, nil
|
||||
while !cur.nil?
|
||||
# 找到重複節點,直接返回
|
||||
return if cur.val == num
|
||||
|
||||
pre = cur
|
||||
# 插入位置在 cur 的右子樹中
|
||||
if cur.val < num
|
||||
cur = cur.right
|
||||
# 插入位置在 cur 的左子樹中
|
||||
else
|
||||
cur = cur.left
|
||||
end
|
||||
end
|
||||
|
||||
# 插入節點
|
||||
node = TreeNode.new(num)
|
||||
if pre.val < num
|
||||
pre.right = node
|
||||
else
|
||||
pre.left = node
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -1545,7 +1595,57 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_search_tree.rb"
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
### 刪除節點 ###
|
||||
def remove(num)
|
||||
# 若樹為空,直接提前返回
|
||||
return if @root.nil?
|
||||
|
||||
# 迴圈查詢,越過葉節點後跳出
|
||||
cur, pre = @root, nil
|
||||
while !cur.nil?
|
||||
# 找到待刪除節點,跳出迴圈
|
||||
break if cur.val == num
|
||||
|
||||
pre = cur
|
||||
# 待刪除節點在 cur 的右子樹中
|
||||
if cur.val < num
|
||||
cur = cur.right
|
||||
# 待刪除節點在 cur 的左子樹中
|
||||
else
|
||||
cur = cur.left
|
||||
end
|
||||
end
|
||||
# 若無待刪除節點,則直接返回
|
||||
return if cur.nil?
|
||||
|
||||
# 子節點數量 = 0 or 1
|
||||
if cur.left.nil? || cur.right.nil?
|
||||
# 當子節點數量 = 0 / 1 時, child = null / 該子節點
|
||||
child = cur.left || cur.right
|
||||
# 刪除節點 cur
|
||||
if cur != @root
|
||||
if pre.left == cur
|
||||
pre.left = child
|
||||
else
|
||||
pre.right = child
|
||||
end
|
||||
else
|
||||
# 若刪除節點為根節點,則重新指定根節點
|
||||
@root = child
|
||||
end
|
||||
# 子節點數量 = 2
|
||||
else
|
||||
# 獲取中序走訪中 cur 的下一個節點
|
||||
tmp = cur.right
|
||||
while !tmp.left.nil?
|
||||
tmp = tmp.left
|
||||
end
|
||||
# 遞迴刪除節點 tmp
|
||||
remove(tmp.val)
|
||||
# 用 tmp 覆蓋 cur
|
||||
cur.val = tmp.val
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
@@ -110,7 +110,7 @@ comments: true
|
||||
val: number;
|
||||
left: TreeNode | null;
|
||||
right: TreeNode | null;
|
||||
|
||||
|
||||
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val; // 節點值
|
||||
this.left = left === undefined ? null : left; // 左子節點引用
|
||||
@@ -193,7 +193,16 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
### 二元樹節點類別 ###
|
||||
class TreeNode
|
||||
attr_accessor :val # 節點值
|
||||
attr_accessor :left # 左子節點引用
|
||||
attr_accessor :right # 右子節點引用
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -440,7 +449,18 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_tree.rb"
|
||||
|
||||
# 初始化二元樹
|
||||
# 初始化節點
|
||||
n1 = TreeNode.new(1)
|
||||
n2 = TreeNode.new(2)
|
||||
n3 = TreeNode.new(3)
|
||||
n4 = TreeNode.new(4)
|
||||
n5 = TreeNode.new(5)
|
||||
# 構建節點之間的引用(指標)
|
||||
n1.left = n2
|
||||
n1.right = n3
|
||||
n2.left = n4
|
||||
n2.right = n5
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -605,7 +625,13 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_tree.rb"
|
||||
|
||||
# 插入與刪除節點
|
||||
_p = TreeNode.new(0)
|
||||
# 在 n1 -> n2 中間插入節點 _p
|
||||
n1.left = _p
|
||||
_p.left = n2
|
||||
# 刪除節點
|
||||
n1.left = n2
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
@@ -318,7 +318,20 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_tree_bfs.rb"
|
||||
[class]{}-[func]{level_order}
|
||||
### 層序走訪 ###
|
||||
def level_order(root)
|
||||
# 初始化佇列,加入根節點
|
||||
queue = [root]
|
||||
# 初始化一個串列,用於儲存走訪序列
|
||||
res = []
|
||||
while !queue.empty?
|
||||
node = queue.shift # 隊列出隊
|
||||
res << node.val # 儲存節點值
|
||||
queue << node.left unless node.left.nil? # 左子節點入列
|
||||
queue << node.right unless node.right.nil? # 右子節點入列
|
||||
end
|
||||
res
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -791,11 +804,35 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_tree_dfs.rb"
|
||||
[class]{}-[func]{pre_order}
|
||||
### 前序走訪 ###
|
||||
def pre_order(root)
|
||||
return if root.nil?
|
||||
|
||||
[class]{}-[func]{in_order}
|
||||
# 訪問優先順序:根節點 -> 左子樹 -> 右子樹
|
||||
$res << root.val
|
||||
pre_order(root.left)
|
||||
pre_order(root.right)
|
||||
end
|
||||
|
||||
[class]{}-[func]{post_order}
|
||||
### 中序走訪 ###
|
||||
def in_order(root)
|
||||
return if root.nil?
|
||||
|
||||
# 訪問優先順序:左子樹 -> 根節點 -> 右子樹
|
||||
in_order(root.left)
|
||||
$res << root.val
|
||||
in_order(root.right)
|
||||
end
|
||||
|
||||
### 後序走訪 ###
|
||||
def post_order(root)
|
||||
return if root.nil?
|
||||
|
||||
# 訪問優先順序:左子樹 -> 右子樹 -> 根節點
|
||||
post_order(root.left)
|
||||
post_order(root.right)
|
||||
$res << root.val
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
Reference in New Issue
Block a user