一、二叉树的概遍历
二叉树的链式存储:将二叉树的节点定义为一个对象,节点之间通过类似链表的链接方式来连接。
节点定义:
class BiTreeNode:
def __init__(self, datta):
self.data = data
self.lchild = None
self.rchild = None
按照上图结构,自定义一个二叉树
class BirTreeNode:
def __init__(self, data):
self.data = data
self.lchild = None # 左孩子
self.rchild = None # 右孩子
a = BirTreeNode("A")
b = BirTreeNode("B")
c = BirTreeNode("C")
d = BirTreeNode("D")
e = BirTreeNode("E")
f = BirTreeNode("F")
g = BirTreeNode("G")
e.lchild = a
e.rchild = g
a.rchild = c
c.lchild = b
c.rchild = d
g.rchild = f
root = e # 根节点
print(root.lchild.rchild.data)
二叉树的遍历方式:
- 前序遍历:EACBDGF
取出根节点,将左边子树当作一个整体,遍历左子树;然后再将右子树当作一个整体,