Python練手之根據前序和中序&根據中序和後序重建二叉樹,輸出前序、中序和後序遍曆結果

來源:互聯網
上載者:User

這是根據本人另一個C語言版本採用python實現的,添加了根據中序和後序重建二叉樹功能,更多詳細解釋可參考:http://blog.csdn.net/hinyunsin/archive/2011/04/11/6315502.aspx

 

下面就是python代碼:

#-*- 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 /> 返回前序走訪結果<br /> '''<br /> if bt:<br /> return '%s%s%s' % (bt.e, preorderTraverse(bt.left), preorderTraverse(bt.right))<br /> return ''<br />def inorderTraverse(bt):<br /> '''<br /> 返回中序遍曆結果<br /> '''<br /> if bt:<br /> return '%s%s%s' % (inorderTraverse(bt.left), bt.e, inorderTraverse(bt.right))<br /> return '';<br />def postorderTraverse(bt):<br /> '''<br /> 返回後序遍曆結果<br /> '''<br /> if bt:<br /> return '%s%s%s' % (postorderTraverse(bt.left), postorderTraverse(bt.right), bt.e)<br /> return ''<br />def printBTree(bt, depth):<br /> '''<br /> 遞迴列印這棵二叉樹,*號表示該節點為NULL<br /> '''<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%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 /> 根據前序和中序遍曆結果重構這棵二叉樹<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 /> 根據中序和後序遍曆結果重構這棵二叉樹<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 = 'ABDGCEFH'<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 'Postorder: %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 'Postorder: %s' % (postorderTraverse(bt))<br /> print 'The BTree is (* means no such a node):'<br /> printBTree(bt, 0)<br />

python確實很有意思,寫出來的東西比c要簡潔很多,當然,我的其實還遠遠不夠簡潔,沒辦法,還是新手嘛,呵呵~

這裡多了根據中序和後序重建二叉樹功能,原理和根據前序和中序重建一樣。

 

下面是執行結果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.