树
深度遍历
def preorder(self, node):
if node == None:
return
print(node.elem)
self.preorder(node.lchild)
self.preorder(node.rchild)
def preorder(self, node):
if node == None:
return
self.preorder(node.lchild)
print(node.elem)
self.preorder(node.rchild)
def preorder(self, node):
if node == None:
return
self.preorder(node.lchild)
self.preorder(node.rchild)
print(node.elem)
广度遍历
def breadth_travel(self, root):
if root == None:
return
queue = []
queue.append(root)
while queue:
node = queue.pop(0)
print node.elem,
if node.lchild != None:
queue.append(node.lchild)
if node.rchild != None:
queue.append(node.rchild)