二叉樹的構造
class
node
(object):
"""節點類"""
def__init__
(self, value=
none
, left=
none
, right=
none):
self.value = value
self.left = left
self.right = right
class
tree
(object):
"""二叉樹類"""
def__init__
(self)
:"""初始化樹類的根節點和遍歷佇列"""
self.root = node(
) self.tree_queue =
# 葉子結點的資料
defadd
(self, value)
:"""新增資料建構函式"""
node = node(value)
if self.root.value is
none
:# 樹為空則進行節點的新增
self.root = node
else
: cur_node = self.tree_queue[0]
# 獲取當前的根節點
if cur_node.left is
none
:# 左子樹為空則進行新增
cur_node.left = node
else
:# 有字數為空則加入
cur_node.right = node
self.tree_queue.pop(0)
# 當前節點分配完成,退出佇列
defmain()
: tree = tree(
) tree.add(0)
tree.add(1)
tree.add(2)
tree.add(3)
tree.add(4)
tree.add(5)
tree.add(6)
tree.add(7)
tree.add(8)
tree.add(9)
return tree
if __name__ ==
'__main__'
: tree = main(
)print
(tree)
前序遍歷
import 二叉樹的構造 as tree_con
defpre_order
(root)
:"""遞迴實現樹的前序遍歷"""
if root is
none
:return
print
(root.value, end=
"\t"
) pre_order(root.left)
pre_order(root.right)
defpre_order_stack
(root)
:"""非遞迴實現樹的前序遍歷"""
if root is
none
:return
cur_node = root
pre_stack =
while pre_stack or cur_node:
while cur_node:
print
(cur_node.value, end=
"\t"
) cur_node = cur_node.left
cur_node = pre_stack.pop(
) cur_node = cur_node.right
print()
if __name__ ==
'__main__'
: tree = tree_con.main(
)print
(tree)
# pre_order(tree.root)
pre_order_stack(tree.root)
中序遍歷
import 二叉樹的構造 as tree_con
defin_order
(root)
:"""遞迴實現樹的中序遍歷"""
if root is
none
:return
in_order(root.left)
print
(root.value, end=
"\t"
) in_order(root.right)
defin_order_stack
(root)
:"""非遞迴實現樹的中序遍歷"""
if root is
none
:return
cur_node = root
in_stack =
while in_stack or cur_node:
while cur_node:
cur_node = cur_node.left
cur_node = in_stack.pop(
)print
(cur_node.value, end=
"\t"
) cur_node = cur_node.right
print()
if __name__ ==
'__main__'
: tree = tree_con.main(
)print
(tree)
print
([ele.value for ele in tree.tree_queue]
) in_order_stack(tree.root)
in_order(tree.root)
後序遍歷
import 二叉樹的構造 as tree_con
defpost_order
(root)
:"""遞迴實現樹的後序遍歷"""
if root is
none
:return
post_order(root.left)
post_order(root.right)
print
(root.value, end=
"\t"
)def
post_order_stack
(root)
:"""非遞迴實現樹的後序遍歷"""
if root is
none
:return
cur_node = root
post_stack1 =
post_stack2 =
while post_stack1:
cur_node = post_stack1.pop(
)if cur_node.left:
if cur_node.right:
while post_stack2:
print
(post_stack2.pop(
).value, end=
"\t"
)print()
defpost_order
(root):if
not root:
return
none
stack =
[root]
last =
none
while stack:
while stack[-1
].left:-1
].left)
while stack:
if stack[-1
].right == last or
not stack[-1
].right:
last = stack.pop(
)print
(last.val)
elif stack[-1
].right:-1
].right)
if __name__ ==
'__main__'
: tree = tree_con.main(
)print
(tree)
# print([ele.value for ele in tree.tree_queue])
post_order_stack(tree.root)
post_order(tree.root)
層次遍歷,廣度優先遍歷
import 二叉樹的構造 as tree_con
deflevel_order
(root)
:if root is
none
:return
level_queue =
[root]
while level_queue:
cur_node = level_queue.pop(0)
print
(cur_node.value, end=
"\t"
)if cur_node.left:
if cur_node.right:
print()
if __name__ ==
'__main__'
: tree = tree_con.main(
)print
(tree)
level_order(tree.root)
專案二 二叉樹遍歷的遞迴思想
問題及 2015,煙台大學計算機與控制工程學院 完成日期 2015年11月2日 問題描述 實現二叉樹的先序 中序 後序遍歷的遞迴演算法,並對用 a b d,e h j,k l,m n c f,g i 建立的二叉樹進行測試。1 標頭檔案 ifndef btree h included define b...
資料結構與演算法 樹的應用 二 二叉樹遍歷
題目描述 輸入 兩個字串,其長度n均小於等於26。第一行為前序遍歷,第二行為中序遍歷。二叉樹中的結點名稱以大寫字母表示 最多26個結點 輸出 輸入樣例可能有多組,對於每組測試樣例,輸出一行,為後序遍歷的字串。樣例輸入 abc bacfdxeag xdefag 樣例輸出 bca xedgaf 演算法分...
二叉樹的7種遍歷演算法
中根遞迴遍歷演算法 後根遞迴遍歷演算法 先根非遞迴遍歷演算法 中根非遞迴遍歷演算法 後根非遞迴遍歷演算法 層序遍歷演算法 1.若二叉樹為空則退出,否則進行以下步驟 2.訪問當前的根節點 3.先根順序遍歷訪問左子樹 4.先根順序遍歷訪問右子樹 5.退出 public static void proor...