二叉排序树 python实现

简介: 二叉排序树 python实现
class BTNode:
    def __init__(self, data, left, right):
        self.data = data
        self.left = left
        self.right = right
class BTree:
    def __init__(self, root):
        self.root = root;
    def insert(self, value):
        self.insertNode(value, self.root)
    def insertNode(self, data, btnode):
        if btnode == None:
            btnode = BTNode(data, None, None)
        elif data < btnode.data:
            if btnode.left == None:
                btnode.left = BTNode(data, None, None)
                return
            self.insertNode(data, btnode.left)
        elif data > btnode.data:
            if btnode.right == None:
                btnode.right = BTNode(data, None, None)
                return
            self.insertNode(data, btnode.right)
    def printBTreeImpl(self, btnode):
        if btnode == None:
            return
        self.printBTreeImpl(btnode.left)
        print(btnode.data)
        self.printBTreeImpl(btnode.right)
    def printBTree(self):
        self.printBTreeImpl(self.root)
if __name__ == '__main__':
    root = BTNode(2, None, None)
    btree = BTree(root)
    for i in [5, 8, 3, 1, 4, 9, 0, 7]:
        btree.insert(i)
    btree.printBTree()
相关文章
Python print() 打印两个 list ,实现中间换行
Python print() 打印两个 list ,实现中间换行
|
Python
Python实现因子分析(附案例实战)
Python实现因子分析(附案例实战)
1115 0
Python实现因子分析(附案例实战)
|
JSON 区块链 数据格式
Python实现一个简单的区块链
本文介绍如何用Python实现一个简单的区块链。
491 0
|
存储 数据安全/隐私保护 计算机视觉
python 实现pacs功能 推送下拉影像
python 实现dcmtk关联pacs功能 推送下拉影像
232 0
python 实现pacs功能 推送下拉影像
|
算法 大数据 Python
Leedcode 每日一练 搜索二维矩阵Ⅰ Python实现
Leedcode 每日一练 搜索二维矩阵Ⅰ Python实现
126 2
Leedcode 每日一练 搜索二维矩阵Ⅰ Python实现
|
前端开发 Python
Leecode加法题目3个 每日练习 Python实现
Leecode加法题目3个 每日练习 Python实现
87 0
Leecode加法题目3个 每日练习 Python实现
|
iOS开发 Python
Python实现微信消息连续发送
Python实现微信消息连续发送
Python实现微信消息连续发送
python实现微信小游戏“飞机大战”
python实现微信小游戏“飞机大战”
python实现微信小游戏“飞机大战”
|
Python
Python分分钟实现图书管理系统(含代码)
Python分分钟实现图书管理系统(含代码)
319 0
|
JSON 算法 数据安全/隐私保护
Python:使用PyJWT实现JSON Web Tokens加密解密
Python:使用PyJWT实现JSON Web Tokens加密解密
251 0