Build a binary tree based on the forward and middle order of Python & re-build the binary tree based on the middle and back order, and output the results of the previous, middle, and post-order traversal.

Source: Internet
Author: User

This is based on my other C language version using Python implementation, added according to the central order and the post order re-built binary tree function, more detailed explanation can refer to: http://blog.csdn.net/hinyunsin/archive/2011/04/11/6315502.aspx

 

The following is the Python code:

#-*-Coding: UTF-8-*-<br/> #! /Usr/bin/python <br/> # filename: btreenode. PY <br/> ''' <br/> created on 2011-4-11 <br/> @ Author: Boyce <br/> @ Contact: boyce.ywr@gmail.com <br/> @ version: 1.0 <br/> ''' <br/> class btree: <br/> ''' <br/> represent a no in a binary tree. <br/> ''' <br/> def _ init _ (self, c = '/0', L = none, r = none ): <br/> ''' <br/> initializes the node's data <br/> ''' <br/> self. E = C <br/> self. left = L <br/> self. right = r <br/> def preordertraverse (BT ): <br/> ''' <br/> returns the result of the forward traversal <br/> ''' <br/> If BT: <br/> return '% S % s' % (BT. e, preordertraverse (BT. left), preordertraverse (BT. right) <br/> return ''<br/> def inordertraverse (BT ): <br/> ''' <br/> returns the result of the Middle-order traversal <br/> ''' <br/> If BT: <br/> return '% S % s' % (inordertraverse (BT. left), BT. e, inordertraverse (BT. right) <br/> return ''; <br/> def postordertraverse (BT ): <br/> ''' <br/> returns the result of post-sequential traversal. <br/> ''' <br/> If BT: <br/> return '% S % s' % (postordertraverse (BT. left), postordertraverse (BT. right), BT. e) <br/> return ''<br/> def printbtree (BT, depth): <br/> ''' <br/> Print the binary tree recursively, * indicates that the node is null. <br/> ''' <br/> if not BT: <br/> CH = '*' <br/> else: <br/> CH = BT. E <br/> ''' <br/> # Ch = (lambda X: (x and X. e) or '*') (BT) <br/> CH = BT. e if BT else '*' <br/> If (depth> 0): <br/> Print '% S % s' % (depth-1) * '', '--', CH) <br/> else: <br/> Print ch <br/> if not BT: <br/> return <br/> printbtree (BT. left, depth + 1) <br/> printbtree (BT. right, depth + 1) <br/> def buildbtreefromprein (preo, Ino ): <br/> ''' <br/> reconstruct the binary tree based on the pre-order and mid-order traversal results <br/> ''' <br/> If (preo = ''or ino = ''): <br/> return none <br/> Pos = ino. find (preo [0]) <br/> If (Pos <0): <br/> return none <br/> return btree (preo [0], buildbtreefromprein (preo [1: POS + 1], iNO [0: POS]), buildbtreefromprein (preo [POS + 1:], iNO [POS + 1:]) <br/> # Return nd <br/> def buildbtreefrominpost (Ino, po ): <br/> ''' <br/> reconstruct the binary tree based on the result of the central and backward traversal. <br/> ''' <br/> If (ino = ''or po = ''): <br/> return none <br/> Pos = ino. find (po [-1]) <br/> If (Pos <0): <br/> return none <br/> return btree (po [-1], buildbtreefrominpost (ino [0: POS], po [0: POS]), buildbtreefrominpost (ino [POS + 1:], po [pos:-1]) </P> <p> If _ name _ = '_ main __': <br/> preo = 'abdgcef' <br/> ino = 'dgbaechf '<br/> po = 'gdbehfca' <br/> bt = buildbtreefromprein (preo, Ino) <br/> Print 'build from preorder & inorder' <br/> Print 'preorder: % s' % (preordertraverse (BT) <br/> Print 'inorder: % s' % (inordertraverse (BT) <br/> Print 'storder: % s' % (postordertraverse (BT )) <br/> Print 'the btree is (* means no such a node): '<br/> printbtree (BT, 0) <br/> bt = buildbtreefrominpost (Ino, po) <br/> Print 'build from inorder & postorder' <br/> Print 'preorder: % s' % (preordertraverse (BT) <br/> Print 'inorder: % s' % (inordertraverse (BT) <br/> Print 'storder: % s' % (postordertraverse (BT )) <br/> Print 'the btree is (* means no such a node): '<br/> printbtree (BT, 0) <br/>

Python is really interesting. It is much simpler to write than C. Of course, I am actually far from concise enough. I can't help it. I am a newbie ~

Here, the function of rebuilding a binary tree in the middle and back order is added. The principle is the same as that in the pre-order and Middle-order reconstruction.

 

The execution result is as follows:

Related Article

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.