table of Contents
Front binary tree defined in preorder
- Tree (English: Tree) is a non-presence of any of the only route to FIG. (Undirected graph), between two vertices. Or, as long as no circuit connected graph is the tree
- Binary tree (English: Binary tree) is at most two branches per node (in the absence of branching greater than 2 nodes)
of the tree structure. Branch is commonly referred to as "left subtree" and "right subtree." Binary tree has branches around the order, it can not be reversed. - Complete binary tree: the leaf nodes only occur once in the lower layer and the lowermost layer and the bottom layer of the binary tree nodes are concentrated in a plurality of positions of the leftmost layer.
- Balanced binary tree: it is the absolute value of the difference in height empty tree or its two left and right subtrees of not more than 1, and the left and right sub-trees are a balanced binary tree.
For traversal of the tree, there are two methods:
The depth-first traversal: including front, in the three kinds of traversal order;
6. breadth traversal: that is what we usually traverse the level (other data structure to be supported, such as the queue )
- Preorder traversal: root ----> left subtree ----> right subtree (for the above picture of the tree traversal result is: EBADCFHGI)
- Preorder: left subtree ----> root node ----> right subtree (for the above picture of the tree traversal result is: ABCDEFGHI)
- Follow-up traversal: left subtree ----> right subtree ----> root (for the above picture of the tree traversal result is: ACDBGIHFE)
- Breadth-first traversal: traverse the level, layer by layer traversal (for the above picture tree traversal result is: EBFADHCGI)
It should be noted that the most common preorder
Creating trees and tree traversal recursive method
class Node(object):
"""创建一个构造树的类"""
def __init__(self, value=None, left=None, right=None):
super(Node, self).__init__()
self.value = value
self.left = left # 左子树
self.right = right # 右子树
def PreOrder(root):
'''
前序遍历
'''
if root == None:
return
print(root.value)
PreOrder(root.left)
PreOrder(root.right)
def InOrder(root):
'''
中序遍历
'''
if root == None:
return
InOrder(root.left)
print(root.value)
InOrder(root.right)
def Subsequent(root):
'''
后序遍历
'''
if root == None:
return
Subsequent(root.left)
Subsequent(root.right)
print(root.value)
def LevelTraaverse(root):
'''
广度优先遍历:层次遍历
'''
if root == None:
return
queue = [] # 新建一个队列
queue.append(root) # 将根节点入队
while len(queue): # 只要队列不为空,一直循环
lenth_q = len(queue)
for i in range(lenth_q):
r = queue.pop(0) # 根节点出队,并存放于r
if r.left is not None:
queue.append(r.left) # 左节点入队
if r.right is not None:
queue.append(r.right) # 右节点入队
print(r.value)
main functions:
if __name__=='__main__':
root=Node('E',left=Node('B',left=Node('A'),right=Node('D',left=Node('C'))), right=Node('F',right=Node('H',left=Node('G'),right=Node('I'))))
print("PreOrder traversal result:")
PreOrder(root)
print("InOrder traversal result:")
InOrder(root)
print("Subsequent traversal result:")
Subsequent(root)
print("LevelTraaverse result:")
LevelTraaverse(root)
result:
Reference:
[1]. Depth study binary tree (a) binary basis
[2]. Binary tree traversal (preamble, in sequence, after, traverse the level, depth-first, breadth-first)