数据结构:二叉树(七)

一、二叉树的定义

1、二叉树的链式存储

将二叉树的节点定义为一个对象,节点之间通过类似链表的链接方式来连接

2、节点的定义

class BittreeNode:
     def __init__(self,data):
         self.data = data
         self.lchild = None
         self.rchild = None

3、二叉树图

二、前序遍历

1、实现代码

class BittreeNode:
     def __init__(self,data):
         self.data = data
         self.lchild = None
         self.rchild = None


a = BittreeNode('A')
b = BittreeNode('B')
c = BittreeNode('C')
d = BittreeNode('D')
e = BittreeNode('E')
f = BittreeNode('F')
g = BittreeNode('G')

e.lchild = a
e.rchild = g
a.rchild = c
c.lchild = b
c.rchild = d
g.rchild = f

root = e

def pre_order(root):
    if root:
        print(root.data)
        pre_order(root.lchild)
        pre_order(root.rchild)

pre_order(root)

2、输出

"D:Program FilesPython35python3.exe" E:/test/bitree.py
E
A
C
B
D
G
F

Process finished with exit code 0

实现代码

def pre_order(root):
    if root:
        print(root.data,end='')
        pre_order(root.lchild)
        pre_order(root.rchild)
		
pre_order(root)

输出

"D:Program FilesPython35python3.exe" E:/test/bitree.py
EACBDGF
Process finished with exit code 0

三、中序遍历

1、实现代码

class BittreeNode:
     def __init__(self,data):
         self.data = data
         self.lchild = None
         self.rchild = None


a = BittreeNode('A')
b = BittreeNode('B')
c = BittreeNode('C')
d = BittreeNode('D')
e = BittreeNode('E')
f = BittreeNode('F')
g = BittreeNode('G')

e.lchild = a
e.rchild = g
a.rchild = c
c.lchild = b
c.rchild = d
g.rchild = f

root = e

def pre_order(root):
    if root:
        print(root.data,end='')
        pre_order(root.lchild)
        pre_order(root.rchild)
def in_order(root):
    if root:
        in_order(root.lchild)
        print(root.data,end='')
        in_order(root.rchild)

pre_order(root)
print("")
in_order(root)

2、输出

"D:Program FilesPython35python3.exe" E:/test/bitree.py
EACBDGF
ABCDEGF
Process finished with exit code 0

四、后序遍历

1、实现代码

class BittreeNode:
     def __init__(self,data):
         self.data = data
         self.lchild = None
         self.rchild = None


a = BittreeNode('A')
b = BittreeNode('B')
c = BittreeNode('C')
d = BittreeNode('D')
e = BittreeNode('E')
f = BittreeNode('F')
g = BittreeNode('G')

e.lchild = a
e.rchild = g
a.rchild = c
c.lchild = b
c.rchild = d
g.rchild = f

root = e

def pre_order(root):
    if root:
        print(root.data,end='')
        pre_order(root.lchild)
        pre_order(root.rchild)
        
def in_order(root):
    if root:
        in_order(root.lchild)
        print(root.data,end='')
        in_order(root.rchild)
        
        
def post_order(root):
    if root:
        post_order(root.lchild)
        post_order(root.rchild)
        print(root.data, end='')
        

pre_order(root)
print("")
in_order(root)
print("")
post_order(root)

2、输出

"D:Program FilesPython35python3.exe" E:/BaiduNetdiskDownload/0921/test/bitree.py
EACBDGF
ABCDEGF
BDCAFGE
Process finished with exit code 0

五、层次遍历

1、实现代码

from collections import deque

class BittreeNode:
    def __init__(self, data):
        self.data = data
        self.lchild = None
        self.rchild = None


a = BittreeNode('A')
b = BittreeNode('B')
c = BittreeNode('C')
d = BittreeNode('D')
e = BittreeNode('E')
f = BittreeNode('F')
g = BittreeNode('G')

e.lchild = a
e.rchild = g
a.rchild = c
c.lchild = b
c.rchild = d
g.rchild = f

root = e


def pre_order(root):
    if root:
        print(root.data, end='')
        pre_order(root.lchild)
        pre_order(root.rchild)


def in_order(root):
    if root:
        in_order(root.lchild)
        print(root.data, end='')
        in_order(root.rchild)


def post_order(root):
    if root:
        post_order(root.lchild)
        post_order(root.rchild)
        print(root.data, end='')


def level_order(root):
    queue = deque()
    queue.append(root)
    while len(queue) > 0:
        node = queue.popleft()
        print(node.data,end='')
        if node.lchild:
            queue.append(node.lchild)
        if node.rchild:
            queue.append(node.rchild)

pre_order(root)
print("")
in_order(root)
print("")
post_order(root)
print("")
level_order(root)

2、输出

"C:Program FilesPython35python.exe" E:/test/btree.py
EACBDGF
ABCDEGF
BDCAFGE
EAGCFBD
Process finished with exit code 0

  

原文地址:https://www.cnblogs.com/luoahong/p/9705950.html