python實現二叉樹

來源:互聯網
上載者:User

        最近對python比較感興趣,基本看完了網上流傳甚廣的 簡明python教程,自己也試著玩了玩,感覺python確實是一門優雅的語言。

        前幾天在網上看到一個python的二叉樹代碼,也學著寫了寫,增加了對二叉樹的遍曆方法。

        代碼如下:

         不知道格式有沒有亂

#!/usr/bin/python#*_*coding:utf-8*_*import osclass Node:        def __init__(self, value = None, parent = None , left = None, right = None):        self.value = value        self.parent = parent        self.left = left        self.right = rightdef wideOrder(n):    if n==None:        return    l = list()    l.append(n)    while len(l)>0:        tmp = l.pop(0)        print tmp.value        if tmp.left!=None:            l.append(tmp.left)        if tmp.right!=None:            l.append(tmp.right)def preOrder(n):    if n==None:        return    print n.value    preOrder(n.left)    preOrder(n.right)def midOrder(n):        if n==None:        return    midOrder(n.left)    print n.value    midOrder(n.right)def aftOrder(n):    if n==None:        return    aftOrder(n.left)    aftOrder(n.right)    print n.valuedef getDepth(n):    if n==None:        return 0;    n_left = getDepth(n.left)    n_right = getDepth(n.right)    if n_left>=n_right:        return n_left+1    else:        return n_right+1class Tree:    def __init__(self):        self.root = None        self.size = 0        self.depth = 0    def insert(self, n):        if n == None:            return        if self.root == None:            self.root = n            self.size=self.size+1            self.depth = 1            return        tmp = self.root        tmpDepth = 1        while True:            tmpDepth = tmpDepth+1            if n.value <= tmp.value:                if tmp.left == None:                    n.parent = tmp                    tmp.left = n                    self.size = self.size+1                    self.depth = max(self.depth, tmpDepth)                    break                else:                    tmp = tmp.left            elif n.value > tmp.value:                if tmp.right == None:                    n.parent = tmp                    tmp.right = n                    self.size = self.size+1                    self.depth = max(self.depth, tmpDepth)                    break                else:                    tmp=tmp.right    def find(self, v):        if v==None or self.root==None:            print 'None'            return        tmp=self.root        while True:            if v==tmp.value:                print 'find it ', v                break            elif v<tmp.value:                if tmp.left==None:                    print 'None'                    break                else:                    tmp=tmp.left            else:                if tmp.right==None:                    print 'None'                    break                else:                    tmp=tmp.righttree = Tree()tree.insert(Node(5))tree.insert(Node(2))tree.insert(Node(-3))tree.insert(Node(4))tree.insert(Node(3))tree.insert(Node(6))tree.insert(Node(-1))tree.insert(Node(7))print u'先序遍曆'preOrder(tree.root)print u'中序遍曆'midOrder(tree.root)print u'後序遍曆'aftOrder(tree.root)print u'廣度遍曆'wideOrder(tree.root)print 'Classic Depth ', getDepth(tree.root)print 'Tree size %d depth %d' %(tree.size, tree.depth)

python豐富的模組,和指令碼的特性比較適合進行快速開發,對一些演算法的示範也比較方便。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.