• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python实现的二叉树算法和kmp算法实例

python实现的二叉树算法和kmp算法实例

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python kmp算法,kmp算法,kmp算法详解,kmp算法代码,kmp算法next原理等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

主要是:前序遍历、中序遍历、后序遍历、层级遍历、非递归前序遍历、非递归中序遍历、非递归后序遍历


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):
        """后续遍历 """
        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):
        """非递归 后续遍历 """
        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:
          &n

您可能想查找下面的文章:

  • python实现的二叉树算法和kmp算法实例
  • python实现的二叉树算法和kmp算法实例

相关文章

  • python实现JAVA源代码从ANSI到UTF-8的批量转换方法
  • python实现数组插入新元素的方法
  • python实现去除下载电影和电视剧文件名中的多余字符的方法
  • Python中字典和JSON互转操作实例
  • Python 模板引擎的注入问题分析
  • 详解Python中的元组与逻辑运算符
  • Python使用Srapy框架爬虫模拟登陆并抓取知乎内容
  • python字符类型的一些方法小结
  • Python中使用第三方库xlrd来读取Excel示例
  • 使用PyCharm配合部署Python的Django框架的配置纪实

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 按日期打印Python的Tornado框架中的日志的方法
    • python抓取网页内容示例分享
    • Python入门学习之字符串与比较运算符
    • Python计算回文数的方法
    • python字符串排序方法
    • Python操作Access数据库基本步骤分析
    • python基于windows平台锁定键盘输入的方法
    • 使用Python编写一个最基础的代码解释器的要点解析
    • python time模块用法实例详解
    • 关于你不想知道的所有Python3 unicode特性

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有