binary search tree python_二叉查找树(binary search tree)——python实现
二叉查找树(binary search tree)
顾名思义二叉查找树中每个节点至多有两个子节点,并且还对存储于每个节点中的关键字值有个小小的要求,
即只要这个节点有左节点或右节点,那么其关键字值总的
大于其左节点的关键字值,
小于其右节点的关键字值,如下图:
因为树的结构特性,很适合使用递归的方式去操作,下面的实现中均是以递归的方式实现:
下面仅给出了python的实现,一是因为代码太长,二是python的实现是我对着C语言实现改过来的,基本没什么差别; 主要实现的方法有:
遍历:
前序:preorder()——理根节点→处理左子树→处理右子树
中序:inorder()——处理左子树→处理根节点→处理右子树
后序:postorder()——处理左子树→处理右子树→处理根节点
插入:
insert(key)——将关键字值为key的节点插入到适当的位置(注释里面的是非递归实现)
删除:
delete(key)——将关键字值为key的节点从树中删掉(注释中给了说明)
获取高度:
height()
获取树中的节点数:
count()
查找:
find(key)——查找关键字值为key的节点(二叉查找树的一个重要应用就是在查找中)
获取树中最大key值节点和最key值小节点:
find_max()
find_min()
;;;
class tree_node:
def __init__(self, key = None, left = None, right = None):
self.key = key
self.left = left
self.right = right
class binary_search_tree:
def __init__(self):
self.root = None
def preorder(self):
print 'preorder: ',
self.__preorder(self.root)
def __preorder(self, root):
if not root:
return
print root.key,
self.__preorder(root.left)
self.__preorder(root.right)
def inorder(self):
print 'inorder: ',
self.__inorder(self.root)
def __inorder(self, root):
if not root:
return
self.__inorder(root.left)
print root.key,
self.__inorder(root.right)
def postorder(self):
print 'postorder: ',
self.__postorder(self.root)
def __postorder(self, root):
if not root:
return
self.__postorder(root.left)
self.__postorder(root.right)
print root.key,
def insert(self, key):
'''recursion'''
self.root = self.__insert(self.root, key)
def __insert(self, root, key):
if not root:
root = tree_node(key)
else:
if key < root.key:
root.left = self.__insert(root.left, key)
elif key > root.key:
root.right = self.__insert(root.right, key)
else:
print key, 'is already in tree'
return root
##non-recursion
## def insert(self, key):
## if not self.root:
## self.root = tree_node(key)
## else:
## cur = self.root
## while True:
## if key < cur.key:
## if not cur.left:
## cur.left = tree_node(key)
## break
## cur = cur.left
## elif key > cur.key:
## if not cur.right:
## cur.right = tree_node(key)
## break
## cur = cur.right
## else:
## print key, 'in tree'
## break
def height(self):
return self.__height(self.root)
def __height(self, root):
if not root:
return -1
left_height = self.__height(root.left)
right_height = self.__height(root.right)
#return 1+(left_height if left_height>right_height else right_height)#这种方式是自己写的,后面两种高大上的是网上偷学的^_^
#return 1+[left_height,right_height][left_height
return 1+(left_height>right_height and [left_height] or [right_height])[0]
def count(self):
'''elements in tree'''
return self.__count(self.root)
def __count(self, root):
if not root:
return 0
return 1+self.__count(root.left)+self.__count(root.right)
def delete(self, key):
self.root = self.__delete(self.root, key)
##
##删除操作:
##首先找到删除的节点,
##1. 如果左右子树都不为空,则找到右子树中最小的节点min,用min.key代替删除节点的key,然后再到右子
## 树中删除min节点,因为min没有左节点,所以删除它的话只需要用它的右节点代替它(如果有右节点);
##2. 如果左子树或者右子树不为空,则直接代替掉
##3. 如果左右均空即叶子节点,直接删掉
def __delete(self, root, key):
if not root:
print 'not find key: ', key
elif key < root.key:
root.left = self.__delete(root.left, key)
elif key > root.key:
root.right = self.__delete(root.right, key)
elif root.left and root.right: #found
right_min = self.__find_min(self.root.right)
root.key = right_min.key
root.right = self.__delete(root.right, right_min.key)
elif root.left:
root = root.left
elif root.right:
root = root.right
else:
root = None #python的GC会在没有引用指向对象的时候销毁对象
return root
def find(self, key):
node = self.__find(self.root, key)
if not node:
print 'not found'
return node
def __find(self, root, key):
if not root:
return None
if key < root.key:
return self.__find(root.left, key)
elif key > root.key:
return self.__find(root.right, key)
else:
return root
def find_min(self):
return self.__find_min(self.root)
def __find_min(self, root):
if not root.left:
return root
return self.__find_min(root.left)
def find_max(self):
return self.__find_max(self.root)
def __find_max(self, root):
if not root.right:
return root
return self.__find_max(root.right)
def main():
import random
root = binary_search_tree()
for i in random.sample([j for j in range(1, 100)], 5):
root.insert(i)
print 'insert: '
root.insert(78)
root.insert(101)
root.insert(14)
root.preorder()
root.inorder()
root.postorder()
print 'height: ', root.height()
print 'count: ', root.count()
print 'min: ', root.find_min().key
print 'max: ', root.find_max().key
print 'delete: '
root.delete(101)
root.delete(12)
root.preorder()
root.inorder()
root.postorder()
print root.find(71)
print root.find(78)
print 'height: ', root.height()
print 'count: ', root.count()
print 'min: ', root.find_min().key
print 'max: ', root.find_max().key
if __name__ == '__main__':
main()
binary search tree python_二叉查找树(binary search tree)——python实现相关推荐
- 二叉排序树(Binary Sort Tree) 又称为二叉查找树(Binary Search Tree) - (代码、分析)
目录: 代码: 分析: 代码: BSTree.h #ifndef _BSTREE_H_ #define _BSTREE_H_typedef void BSTree;//定义二叉树类型 typedef ...
- 『数据结构与算法』解读树(Tree)和二叉树(Binary Tree)!
『数据结构与算法』解读树(Tree)和二叉树(Binary Tree)! 文章目录 一. 树 1.1. 树的定义 1.2. 树的基本术语 1.3. 树的性质 二. 二叉树 2.1. 二叉树的定义 2. ...
- mysql 怎么创建B Tree索引_MySQL为什么选择B+Tree做索引
MySQL为什么选择B+Tree? 首先理解MySQL索引的几个原则 是为了加速对表中数据行的检索而创建的一种分散存储的数据结构. 工作机制 如上图:以id创建索引,索引数据结构里存储了索引键(关键字 ...
- Android运行时候报错:android.view.InflateException: Binary XML file line #19: Binary XML file lin
Android运行时候报错:android.view.InflateException: Binary XML file line #19: Binary XML file lin 这个问题自己大致在 ...
- android.view.InflateException: Binary XML file line #7: Binary XML file line #7
错误如下 11-21 08:19:44.040 3608-3608/com.leon.oldrecyclerview E/AndroidRuntime: FATAL EXCEPTION: main ...
- android Binary XML file line #1: Binary XML file line #1: Error inflating class x 问题详解
话不多少,上错误堆栈: Process: com.mci.smagazine, PID: 25065java.lang.RuntimeException: Unable to start activi ...
- mysql binary blob区别_SQL中binary 和 varbinary的区别 blob
binary 和 varbinary 固定长度 (binary) 的或可变长度 (varbinary) 的 binary 数据类型. binary [ ( n ) ] 固定长度的 n 个字节二进制数据 ...
- python中search和match的区别_Python中正则表达式match()、search()函数及match()和search()的区别详解...
match()和search()都是python中的正则匹配函数,那这两个函数有何区别呢? match()函数只检测RE是不是在string的开始位置匹配, search()会扫描整个string查找 ...
- android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating
android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating cla ...
最新文章
- 状压DP Hiho-1044 状态压缩
- 数据结构与算法:16 Leetcode同步练习(六)
- 没有内幕交易:Coinbase完成了比特币现金调查
- UA OPTI512R 傅立叶光学导论22 透镜成像与傅立叶变换
- 建造者模式(Builder Pattern)(转自TerryLee)
- c++抽象类在多继承中的应用
- 云计算产值将超3000亿美元 亚马逊微软谷歌居三甲
- 2021计算机应用基础形考答案模块2,国家开放大学计算机应用基础模块2形考答案-20210603091431.docx-原创力文档...
- js网页文件资源加载器
- Atlassian 修复严重的 Jira 认证绕过漏洞
- [LAMP]——mod_security和mod_evasive模块的安装
- Linux服务器安全登录设置
- linux r语言 安装包下载,R语言安装程序包(示例代码)
- 直播声音代码html,如何实现在直播中播放音频文件
- eth转入地址_ETH智能合约靶机 审计学习攻略
- LRO (大量接收减负)
- win10电脑360调用不到JAVA,windows10系统下360浏览器打不开网页如何解决
- 数据中台与业务中台是什么关系?_光点科技
- java smtp.126.com_Java采用SMTP协议发送邮件 | 学步园
- 并行计算:openMP(一)—— parallel,for,sections指令的用法
热门文章
- python集合与字典区别_Python中的字典与集合
- python课程主要介绍哪些内容_Python课程详细介绍
- bigdecimal比较是否相等_java基础教程之字符串的介绍,比较重要的一个知识点【四】...
- python解释器 pip安装_pip安装Python库时的问题及解决方法总结
- html css js实现快递单打印_JS与HTML、CSS实现2048小游戏(六)
- python计算一元一次方程的根_5-2 一元二次方程
- python中raw_input未定义_python之NameError: name 'raw_input' is not defined
- u盘安装ubuntu_简单实用的ubuntu18.04安装
- nessus rpm 安装_CentOS8.0下查看已安装的软件位置
- java获取eureka_获取Eureka服务列表的各种场景