二叉树类实现
class BinTNode:
def __init__(self, dat, left = None, right = None):
self.data = data
self.left = left
self.right = right
统计树中节点个数
def count_BinTNodes(t):
if t is None:
return 0
else:
return 1 + count_BinTNode(t.left) \
+ count_BinTNode(t.right)
求二叉树所有数值和
def sum_BinTNodes(t):
if t is None:
return 0
else:
return t.dat + sum_BinTNodes(t.left) \
+ sum_BinTNodes(t.right)
非递归的先根序遍历函数
def preorder_nonrec(t, proc):
s = SStack()
while t is not None or s.is_empty():
while t is not None: # 沿左分支下行