Introduction:
Tree is a very important data structure, the main purpose is to improve the search efficiency, for the case of repeated lookups, such as binary sorting tree, fp-tree. It can also be used to improve coding efficiency, such as Havermann trees.
Code:
Using Python to implement tree construction and several traversal algorithms, although not difficult, but still the code for a summary of the collation. Implementation features:
- The structure of a tree
- Recursive implementation of First order traversal, middle sequence traversal, post-order traversal
- Stack implementation of First order traversal, middle sequence traversal, post-order traversal
- Queue implementation Hierarchy traversal
#coding =utf-8 class Node(object): "" node Class " " " def __init__(self, elem=-1, Lchild=none, Rchild=none):Self.elem = Elem Self.lchild = lchild Self.rchild = rchild class Tree(object): "" " Tree Class " "" def __init__(self):Self.root = Node () def add(self, elem): "" Add Node "" to the treenode = node (elem)ifSelf.root.elem = =-1:#如果树是空的, the root node is assigned a valueSelf.root = nodeElse: Myqueue = [] TreeNode = Self.root myqueue.append (treeNode) whileMyqueue:#对已有的节点进行层次遍历TreeNode = Myqueue.pop (0)ifTreenode.lchild = =None: Treenode.lchild = nodereturn elifTreenode.rchild = =None: Treenode.rchild = nodereturn Else: Myqueue.append (Treenode.lchild) myqueue.append (treenode.rchild) def Front_digui(self, root): "" " using recursion to implement the tree's first order traversal " " ifRoot = =None:return PrintRoot.elem, Self.front_digui (root.lchild) Self.front_digui (root.rchild) def Middle_digui(self, root): "" " using recursion to implement the middle sequence traversal of the tree " " ifRoot = =None:returnSelf.middle_digui (Root.lchild)PrintRoot.elem, Self.middle_digui (Root.rchild) def Later_digui(self, root): "" " using recursion to implement the sequential traversal of the tree " " ifRoot = =None:returnSelf.later_digui (Root.lchild) Self.later_digui (root.rchild)PrintRoot.elem, def front_stack(self, root): "" " using the stack to implement the tree's first order traversal " " ifRoot = =None:returnMystack = [] node = root whileNodeorMystack: whileNode#从根节点开始, keep looking for its left subtree. PrintNode.elem, Mystack.append (node) node = node.lchild node = Mystack.pop ()#while结束表示当前节点node为空 that the previous node has no left subtreenode = Node.rchild#开始查看它的右子树 def middle_stack(self, root): "" using the stack to implement a tree's middle order traversal "" " ifRoot = =None:returnMystack = [] node = root whileNodeorMystack: whileNode#从根节点开始, keep looking for its left subtree.Mystack.append (node) node = node.lchild node = Mystack.pop ()#while结束表示当前节点node为空 that the previous node has no left subtree PrintNode.elem, node = Node.rchild#开始查看它的右子树 def later_stack(self, root): "" "The sequential traversal of the tree using the stack implementation " "" ifRoot = =None:returnMyStack1 = [] MyStack2 = [] node = root mystack1.append (node) whileMyStack1:#这个while循环的功能是找出后序遍历的逆序, there is myStack2 insidenode = Mystack1.pop ()ifNode.lchild:myStack1.append (Node.lchild)ifNode.rchild:myStack1.append (node.rchild) mystack2.append (node) whileMyStack2:#将myStack2中的元素出栈, which is the order of sequential traversal PrintMystack2.pop (). Elem, def level_queue(self, root): "" The hierarchical traversal of the tree using the queue "" " ifRoot = =None:returnMyqueue = [] node = root myqueue.append (node) whileMyqueue:node = Myqueue.pop (0)PrintNode.elem,ifNode.lchild! =None: Myqueue.append (Node.lchild)ifNode.rchild! =None: Myqueue.append (Node.rchild)if__name__ = =' __main__ ':"" " main function" ""Elems = Range (Ten)#生成十个数据作为树节点Tree = Tree ()#新建一个树对象 forEleminchElems:tree.add (Elem)#逐个添加树的节点 Print ' \ n Queue implementation hierarchy traversal: 'Tree.level_queue (Tree.root)Print ' \ n ' recursively implement first order traversal: 'Tree.front_digui (Tree.root)Print ' \ n recursive implementation of the sequence traversal: 'Tree.middle_digui (Tree.root)Print ' \ n recursively implement post-traversal: 'Tree.later_digui (Tree.root)Print ' \ n \ nthe stack implements the first order traversal: 'Tree.front_stack (Tree.root)Print ' \ n stack implementation in sequence traversal: 'Tree.middle_stack (Tree.root)Print ' \ n stack implementation post-traversal: 'Tree.later_stack (Tree.root)
Summary:
There are two main types of tree traversal, one is depth-first traversal, such as pre-order, sequence, post-order; one is breadth-first traversal, like hierarchical traversal. The difference between the two is not very obvious, but the efficiency and effect of depth-first search and breadth-first search are very different from tree to graph and to graph without.
Depth first generally uses recursion, and breadth first generally uses queues. Most of the algorithms that can be implemented recursively can also be implemented with stacks.
My impression is that there is a recursive tree method, but I can't think of how to construct it. Later thought carefully, the recursive thought is a bit similar to the depth first algorithm, but the tree construction should be the breadth first. If recursive, there must be a termination condition, such as the provision of tree depth. Otherwise the tree will be constructed to favor the left tree or the right list tree. Therefore, the structure of the general tree should be used in the queue better.
The above said is not rigorous, there are mistakes, please correct me!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python implements two fork trees and seven of its traversal