mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 20:36:29 +08:00
f616dac7da
* Fix is_empty() implementation in the stack and queue chapter * Update en/CONTRIBUTING.md * Remove "剩余" from the state definition of knapsack problem * Sync zh and zh-hant versions * Update the stylesheets of code tabs * Fix quick_sort.rb * Fix TS code * Update chapter_paperbook * Upload the manuscript of 0.1 section * Fix binary_tree_dfs.rb * Bug fixes * Update README * Update README * Update README * Update README.md * Update README * Sync zh and zh-hant versions * Bug fixes
36 lines
932 B
Ruby
36 lines
932 B
Ruby
=begin
|
|
File: binary_tree_bfs.rb
|
|
Created Time: 2024-04-18
|
|
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
|
=end
|
|
|
|
require_relative '../utils/tree_node'
|
|
require_relative '../utils/print_util'
|
|
|
|
### 層序走訪 ###
|
|
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
|
|
|
|
### Driver Code ###
|
|
if __FILE__ == $0
|
|
# 初始化二元樹
|
|
# 這裡藉助了一個從陣列直接生成二元樹的函式
|
|
root = arr_to_tree([1, 2, 3, 4, 5, 6, 7])
|
|
puts "\n初始化二元樹\n\n"
|
|
print_tree(root)
|
|
|
|
# 層序走訪
|
|
res = level_order(root)
|
|
puts "\n層序走訪的節點列印序列 = #{res}"
|
|
end
|