欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)的二叉樹算法和kmp算法實例

 更新時間:2014年04月25日 12:07:29   作者:  
最近重溫數(shù)據(jù)結構,又用python,所以就用python重新寫了數(shù)據(jù)結構的一些東西,以下是二叉樹的python寫法

主要是:前序遍歷、中序遍歷、后序遍歷、層級遍歷、非遞歸前序遍歷、非遞歸中序遍歷、非遞歸后序遍歷

復制代碼 代碼如下:

#!/usr/bin/env python
#-*- coding:utf8 -*-


class TreeNode(object):
    def __init__(self, data=None, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right


class Tree(object):
    def __init__(self, root=None):
        self.root = None

    def makeTree(self, data, left, right):
        self.root = TreeNode(data, left, right)

    def is_empty(self):
        """是否為空 """
        if self.root is None:
            return True
        return False

    def preOrder(self, r):
        """前序遍歷 """
        if not r.is_empty():
            print r.root.data
            if r.root.left is not None:
                r.preOrder(r.root.left)
            if r.root.right is not None:
                r.preOrder(r.root.right)

    def inOrder(self, r):
        """中序遍歷 """
        if not r.is_empty():
            if r.root.left is not None:
                r.preOrder(r.root.left)
            print r.root.data
            if r.root.right is not None:
                r.preOrder(r.root.right)

    def postOrder(self, r):
        """后續(xù)遍歷 """
        if not r.is_empty():
            if r.root.left is not None:
                r.preOrder(r.root.left)
            print r.root.data
            if r.root.right is not None:
                r.preOrder(r.root.right)

    def levelOrder(self, r):
        """層級遍歷 """
        if not r.is_empty():
            s = [r]
            while len(s) > 0:
                temp = s.pop(0)  # 先彈出最先append到的點
                if temp and temp.root is not None:
                    print temp.root.data
                    if temp.root.left is not None:
                        s.append(temp.root.left)
                    if self.root.right is not None:
                        s.append(temp.root.right)

    def preOrder1(self, r):
        """非遞歸 前序遍歷 """
        stack = []
        current = r
        while len(stack) > 0 or (current and not current.is_empty()):
            while current and not current.is_empty():
                print current.root.data
                stack.append(current)
                current = current.root.left
            if len(stack) > 0:
                current = stack.pop()
                current = current.root.right

    def inOrder1(self, r):
        """非遞歸 中序遍歷 """
        stack = []
        current = r
        while len(stack) > 0 or (current and not current.is_empty()):
            while current and not current.is_empty():
                stack.append(current)
                current = current.root.left
            if len(stack) > 0:
                current = stack.pop()
                print current.root.data
                current = current.root.right

    def postOrder1(self, r):
        """非遞歸 后續(xù)遍歷 """
        stack = []
        current = r
        pre = None
        while len(stack) > 0 or (current and not current.is_empty()):
            if current and not current.is_empty():
                stack.append(current)
                current = current.root.left
            elif stack[-1].root.right != pre:
                current = stack[-1].root.right
                pre = None
            else:
                pre = stack.pop()
                print pre.root.data

    def leaves_count(self, r):
        """求葉子節(jié)點個數(shù) """
        if r.is_empty():
            return 0
        elif (not r.root.left) and (not r.root.right):
            return 1
        else:
            return r.root.left.leaves_count(r.root.left) + r.root.right.leaves_count(r.root.right)


if __name__ == '__main__':
    """二叉樹"""
    ra, rb, rc, rd, re, rf = Tree(), Tree(), Tree(), Tree(), Tree(), Tree()
    ra.makeTree("a", None, None)
    rb.makeTree("b", None, None)
    rc.makeTree("c", None, None)
    rd.makeTree("d", None, None)
    re.makeTree("e", None, None)
    rf.makeTree("f", None, None)
    r1, r2, r3, r4, r = Tree(), Tree(), Tree(), Tree(), Tree()
    r1.makeTree("-", rc, rd)
    r2.makeTree("*", rb, r1)
    r3.makeTree("+", ra, r2)
    r4.makeTree("/", re, rf)
    r.makeTree("-", r3, r4)
    r.preOrder(r)
    r.inOrder(r)
    r.postOrder(r)
    r.levelOrder(r)
    print r.leaves_count(r)


大學的時候學過kmp算法,最近在看的時候發(fā)現(xiàn)竟然忘了,所以去重新看了看書,然后用python寫下了這個算法:

復制代碼 代碼如下:

def kmp(text, pattern):
    """kmp算法 """
    pattern = list(pattern)
    next = [-1] * len(pattern)
    #next 函數(shù)
    i, j = 1, -1
    for i in range(1, len(pattern)):
        j = next[i - 1]
        while True:
            if pattern[i - 1] == pattern[j] or j == -1:
                next[i] = j + 1
                break
            else:
                j = next[j]
    #循環(huán)比較
    i, j = 0, 0
    while i < len(text) and j < len(pattern):
        if text[i] == pattern[j] or j == -1:
            i += 1
            j += 1
        else:
            j = next[j]
    #返回結果 如果匹配,返回匹配的位置,否則返回-1
    if j == len(pattern):
        print i – j
    else:
        print -1

相關文章

  • Python繪制趨勢線的示例代碼

    Python繪制趨勢線的示例代碼

    趨勢線是用來顯示數(shù)據(jù)趨勢或者預測未來發(fā)展方向的一種圖形表示方法,這篇文章主要為大家詳細介紹了如何使用Python繪制趨勢線,需要的可以了解下
    2024-03-03
  • python中virtualenvwrapper安裝與使用

    python中virtualenvwrapper安裝與使用

    本篇文章給大家介紹了python環(huán)境神器virtualenvwrapper安裝與使用,對此有需要的朋友可以跟著操作一下。
    2018-05-05
  • 在jupyter notebook中調用.ipynb文件方式

    在jupyter notebook中調用.ipynb文件方式

    這篇文章主要介紹了在jupyter notebook中調用.ipynb文件方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python中的文件打開與關閉操作命令介紹

    python中的文件打開與關閉操作命令介紹

    下面小編就為大家分享一篇python中的文件打開與關閉操作命令介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python爬蟲使用腳本登錄Github并查看信息

    Python爬蟲使用腳本登錄Github并查看信息

    這篇文章主要介紹了Python爬蟲之用腳本登錄Github并查看信息,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • python基礎之定義類和對象詳解

    python基礎之定義類和對象詳解

    這篇文章主要為大家詳細介紹了python的定義類和對象,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • 查看Django和flask版本的方法

    查看Django和flask版本的方法

    今天小編就為大家分享一篇查看Django和flask版本的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Python分支語句常見的使用方法

    Python分支語句常見的使用方法

    這篇文章主要介紹了Python分支語句常見的使用方法,Python分支語句,也稱為選擇語句,體現(xiàn)了程序的選擇結構,即對應不同的場景,選擇不同的處理方式,具體常見的用法需要的朋友可參考下面文章內容
    2022-06-06
  • 關于python的第三方庫下載與更改方式

    關于python的第三方庫下載與更改方式

    這篇文章主要介紹了關于python的第三方庫下載與更改方式,使用python的朋友都知道python有很多非常方便的第三方庫可以使用,那么如果下載這些第三方庫呢,今天小編就帶你們來看看
    2023-04-04
  • pyqt5 tablewidget 利用線程動態(tài)刷新數(shù)據(jù)的方法

    pyqt5 tablewidget 利用線程動態(tài)刷新數(shù)據(jù)的方法

    今天小編就為大家分享一篇pyqt5 tablewidget 利用線程動態(tài)刷新數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06

最新評論