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</