# Share the Python implementation of the two fork tree definition and traversal

Source: Internet
Author: User
This article mainly introduced the Python implementation of the two-fork tree definition and traversal algorithm, combined with a concrete example of the definition of Python-based two-tree and its common traversal operation implementation skills, the need for friends can refer to the next

In this paper, we describe the two-fork tree definition and traversal algorithm implemented by Python. Share to everyone for your reference, as follows:

Beginner python, you need to implement a decision tree, first of all to implement a binary tree data structure using Python. Make the time to do the processing, to ensure that the establishment of the two-fork tree is a balanced binary tree.

`#-*-Coding:utf-8-*-from Collections Import Dequeclass node:def init (self,val,left=none,right=none): Self.val=val    Self.left=left Self.right=right #setter and Getter def Get_val (self): return self.val def set_val (self,val):    Self.val=val def get_left (self): return self.left def set_left (self,left): Self.left=left def get_right (self):     Return self.right def set_right (self,right): Self.right=rightclass tree:def init (self,list): list=sorted (list) Self.root=self.build_tree (list) #递归建立平衡二叉树 def build_tree (self,list): l=0 r=len (list)-1 if (L&GT;R): RE Turn None if (l==r): Return Node (List[l]) mid= (l+r)/2 Root=node (List[mid]) Root.left=self.build_tree (list[      : Mid]) Root.right=self.build_tree (list[mid+1:]) return root #前序遍历 def preorder (self,root): if (Root is None):    return print Root.val self.preorder (root.left) self.preorder (root.right) #后序遍历 def postorder (self,root): if (Root is None): REturn Self.postorder (root.left) self.postorder (root.right) print Root.val #中序遍历 def inorder (self,root): if (r Oot is None): Return Self.inorder (root.left) print Root.val self.inorder (root.right) #层序遍历 def levelorder ( Self,root): If root is None:return queue =deque ([root]) while (Len (queue) >0): Size=len (queue) F or I in range (size): Node =queue.popleft () print node.val if node.left are not None:queue.ap Pend (Node.left) If Node.right is not None:queue.append (node.right) list=[1,-1,3,4,5]tree=tree (list) print ' Middle sequence Traversal: ' Tree.inorder (tree.root) print ' sequence traversal: ' Tree.levelorder (tree.root) print ' Pre-sequence traversal: ' Tree.preorder (tree.root) print ' Post-post traversal: ' Tree.postorder (Tree.root)`

Output:

`Middle sequence traversal-11345 sequence traversal 3-1415 pre-sequence Traversal 3-1145 post-order traversal 1-1543`

Build the two-fork tree as shown:

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.