如何在Python中实现二进制搜索树
在本教程中,我们将学习如何在Python中使用二叉搜索树。注意,二叉树是一种非线性数据结构,而链表和数组是线性数据结构。
在这篇文章中,我们将
- 创建一棵新的树,有根键、节点和基础元素,也叫叶子节点。
- 确定这个算法的空间和时间复杂性。
- 讨论各种类型的二叉树。
前提条件
要继续学习本教程,你必须具备以下条件。
- 一个有助于运行我们的代码的IDE(集成开发环境)。
- 一些Python的基本知识。
二进制搜索树
二叉树是一个有限节点的集合,可以是空的,也可以包含几个元素。一个节点是由三个实体组成的。一个值,在左边和右边有两个指针。根节点是每个子树上的父元素。
它也可以被认为是树中最顶端的节点。附在父元素上的节点被称为子元素。另一方面,叶子节点是二叉树中的基础元素。
二进制搜索树的类型
二进制树的各种类型包括。
- 完整的二进制树。树的所有层次都被填满,根键有一个包含两个或没有节点的子树。
- 平衡的二进制树。叶子节点离根部不远,这更像是一个相对的度量。节点在树上可以超过一个级别。在搜索、插入和删除组件时,平衡树相当有效。
- 全二叉树。它在每个子树中都包含相同数量的节点,除了叶子节点。
创建一棵二叉树
我们需要以下Python语法来生成一棵二叉树。由于子树有类似的元素,所以需要一个递归函数。
class binary_tree:
def __init__(self, key) #function to insert data to our binary tree
self.leftchild = None #setting leftchild of the tree to add items
self.rightchild = None #setting rightchild of the tree to add items
self.key = key
class binary_tree:
def __init__(self): #generate a tree to hold values
self.root = None
#this is the end
删除一棵树
要删除一棵树,我们使用下面的代码。
def add(self, value): #function to add data items to the tree
if self.key is None:
self.key = data #begin adding elements to the binary tree
return
if self.key == value: # this will take care for duplicate nodes
return
if value < self.key: #if value of the key node is more than the leftchild accept values
if self.leftchild:
self.leftchild.add(value) #set values to the leftchild of the tree
else:
self.leftchild = binary_tree(value)
else: #values are more than the key value
if self.rightchild: #if on the rigghtchild of the tree
self.rightchild.add(value) #set values to rightchild
else:
self.rightchild = binary_tree(value) #values cannot be less then the root key
##End of search of our binary tree
在树中添加数据
为了向我们的树添加数据,我们使用下面的Python脚本。
root = binary_tree(50) # 50 is our root key and our object is root
elements = {20,56,3,4,7,10,17,20} #adds values to the tree
for i in elements:
root.add(i) #recursively adds values in the tree
root.search(10)
检查空结点
下面的check() 函数允许我们检查空节点。
def check(self,value): #check for empty values
if self.key is None: #if value is set to None
self.key = value
在树中搜索一个节点
如果我们想知道一个给定的节点是否存在,我们将把给定节点的数据与根节点中的数据进行比较。
首先,我们需要搜索根键是否与给定的数据相等。如果给定的节点在树中存在,我们可以打印一条信息。
如果数据小于根键,我们将在左子树上搜索,否则,我们看右子树。
def search(self, value):
if self.key == value: #check if value is equal to the key val
print("The node is present")
return
if value < self.key: #Here left subtree can be empty or it can contain one or more nodes
if self.leftchild: #this condition is true if left subtree exists
self.leftchild.search(value)
else:
print("The node is empty in the tree!")
else:
if self.rightchild:
self.rightchild.search(value) #search for all the values in the rightchild
return true
else:
print("The node is empty in the tree!") #print out empty rightchild nodes in the tree
下表总结了该算法的空间和时间复杂性。
二进制搜索树
| 平均数 | 最差情况 | |
|---|---|---|
| 空间 | O(n) | O(n) |
| 访问量 | O(log n) | O(n) |
| 搜索 | O(log n) | O(n) |
| 插入 | O(log n) | O(n) |
| 移除 | O(log n) | O(n) |
使用二进制搜索树的好处
- 它们允许快速查找、添加和删除树中的项目。
- 它可以用来实现元素的动态集合或查找表。
- 它们允许人们使用其键来寻找一个元素。
- 他们使用的对数时间复杂度为
k = log(n),其中k是查找、插入和删除时间,n是存储在树中的项目数量。这比线性搜索时间要好。
总结
在这篇文章中,我们学习了二进制搜索树的定义和不同类型。我们还讨论了如何创建一棵树,以及添加和删除数据。最后,我们研究了如何检查空节点。