• 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中的闭包详细介绍和实例
  • python中enumerate函数遍历元素用法分析
  • python基础教程之python消息摘要算法使用示例
  • 跟老齐学Python之for循环语句
  • Python基础入门之seed()方法的使用
  • python保存字符串到文件的方法
  • 使用70行Python代码实现一个递归下降解析器的教程
  • 详细解读Python的web.py框架下的application.py模块
  • python网络编程学习笔记(一)
  • 跟老齐学Python之list和str比较

文章分类

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

最近更新的内容

    • python xml.etree.ElementTree遍历xml所有节点实例详解
    • python实现端口转发器的方法
    • Python 字符串中的字符倒转
    • Python实现带百分比的进度条
    • Python实现从百度API获取天气的方法
    • 在Python下利用OpenCV来旋转图像的教程
    • PYTHON正则表达式 re模块使用说明
    • windows10系统中安装python3.x+scrapy教程
    • Python实现的多线程http压力测试代码
    • Python yield 使用浅析

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

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