Python implements a two-fork tree traversal

Source: Internet
Author: User

A binary tree is a set of finite elements that are either empty or have an element called the root node (root) and two disjoint, respectively, two-tree trees that are called Saozi right subtrees.
    • Each node of a binary tree has a maximum of two subtrees trees (no nodes with a degree greater than 2), and the subtree of the binary tree has left and right points, and the order cannot be reversed.
    • The first layer of the binary tree has a maximum of 2^{i-1} nodes.
    • The two-fork tree with a depth of K has at most 2^k-1 nodes;
    • For any binary tree T, if its terminal node number is N0, the number of nodes with a degree of 2 is N2, then n0=n2+1

First build a binary tree:

1 class Node:   2     def __init__ (self,value=none,left=none,right=None):   3         self.value=value  4         self.left=left    # left dial hand tree 5         Self.right=right  # Right sub-tree

The following gives the pre-sequence traversal/middle sequence traversal/post-order traversal of a two-fork tree

1 defPretraverse (Root):2     " "3 Pre-sequence traversal4     " "5     ifroot==None:6         return  7     Print(Root.value)8 pretraverse (root.left)9 pretraverse (root.right)Ten  One defMidtraverse (Root): A     " " - Middle Sequence Traversal -     " " the     ifroot==None: -         return   - midtraverse (root.left) -     Print(Root.value) + midtraverse (root.right) -    + defAftertraverse (Root): A     " " at Post-post traversal -     " " -     ifroot==None: -         return   - aftertraverse (root.left) - aftertraverse (root.right) in     Print(Root.value)

An example is given below to verify the program

1 if __name__=='__main__':2Root=node ('D', Node ('B', Node ('A'), Node ('C')), Node ('E', Right=node ('G', Node ('F'))))3     Print('Pre-order traversal:')4 Pretraverse (Root)5     Print('\ n')6     Print('Middle sequence Traversal:')7 Midtraverse (Root)8     Print('\ n')9     Print('Post-post traversal:')Ten Aftertraverse (Root) One     Print('\ n')

The result of the output is

Pre-sequence traversal: DBACEGF sequence Traversal: ABCDEFG post-order traversal: acbfged

So, if we know the binary tree's pre-sequence traversal and the middle sequence traversal, we ask the second-order traversal of the binary tree

1Prelist = List ('12473568')2Midlist = List ('47215386')3Afterlist = []4 5 defFindtree (prelist, Midlist, afterlist):6     ifLen (prelist) = =0:7         return8     ifLen (prelist) = = 1:9 afterlist.append (prelist[0])Ten         return OneRoot =Prelist[0] An =Midlist.index (Root) -Findtree (prelist[1:n + 1], midlist[:n], afterlist) -Findtree (Prelist[n + 1:], Midlist[n + 1:], afterlist) theAfterlist.append (Root)

The result is:

 [ " 7  "  ,  4  "  ,  2  "  ,  5  "  ,  8  "  ,  6  "  ,  3  "  ,  1  "  ]
1 if the preceding sequence: DBACEGF and middle order: ABCDEFG, the results are: 2 ['A''C''B'  'F'G'E'  D']

Python implements traversal of a two-fork tree

Contact Us

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.