python遍历树结构_python 数据结构与算法——树的遍历

1、广度优先遍历

2、深度优先遍历

先序遍历:把根放在最前面

中序遍历:把根放在中间

后序遍历:把根放在后面

# -*- coding: utf-8 -*-

"""

Created on Mon Feb 25 21:47:53 2019

树的添加

@author: Xavier

"""

class Node(object):

def __init__(self,item):

self.elem=item

self.lchild=None

self.rchild=None

class Tree(object):

#二叉树

def __init__(self):

self.root=None

def add(self,item):

node=Node(item)

if self.root is None:

self.root=node

return

queue=[self.root]

while queue:

cur_node=queue.pop(0)

if cur_node.lchild is None:

cur_node.lchild=node

return

else:

queue.append(cur_node.lchild)

if cur_node.rchild is None:

cur_node.rchild=node

return

else:

queue.append(cur_node.rchild)

def breadth_travel(self):

"""广度遍历"""

if self.root is None:

return

queue=[self.root]

while queue:

cur_node=queue.pop(0)

print(cur_node.elem,end=" ")

if cur_node.lchild is not None:

queue.append(cur_node.lchild)

if cur_node.rchild is not None:

queue.append(cur_node.rchild)

def preorder(self,node):

#先序遍历

#每次使用递归的代码都很简单

if node is None:

return

print(node.elem,end=" ")

self.preorder(node.lchild)

self.preorder(node.rchild)

def inorder(self,node):

#中序遍历

#每次使用递归的代码都很简单

if node is None:

return

self.inorder(node.lchild)

print(node.elem,end=" ")

self.inorder(node.rchild)

def postorder(self,node):

#后序遍历

#每次使用递归的代码都很简单

if node is None:

return

self.postorder(node.lchild)

self.postorder(node.rchild)

print(node.elem,end=" ")

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)

tree.breadth_travel()

print("")

tree.preorder(tree.root)

print("")

tree.inorder(tree.root)

print("")

tree.postorder(tree.root)

运行结果

runfile('D:/learn_code/code/数据结构与算法python/binary_tree.py', wdir='D:/learn_code/code/数据结构与算法python')

0 1 2 3 4 5 6 7 8 9

0 1 3 7 8 4 9 2 5 6

7 3 8 1 9 4 0 5 2 6

7 8 3 9 4 1 5 6 2 0

二、二叉树由树遍历确定一棵树

只要包含中序遍历就可以写出来,需要两个

标签:node,None,遍历,cur,python,self,tree,add,数据结构

来源: https://blog.csdn.net/zuyuhuo6777/article/details/87949182

你可能感兴趣的:(python遍历树结构)