当前位置: 首页 > news >正文

付钱做编程题目的网站宁波seo网络推广公司排名

付钱做编程题目的网站,宁波seo网络推广公司排名,ppt模板下载网站推荐,做装修的网站有哪些Python中的平衡二叉搜索树(AVL树)算法详解 平衡二叉搜索树(AVL树)是一种自平衡的二叉搜索树,它通过在插入或删除节点时进行旋转操作来保持树的平衡性。在AVL树中,任何节点的两个子树的高度差(平…

Python中的平衡二叉搜索树(AVL树)算法详解

平衡二叉搜索树(AVL树)是一种自平衡的二叉搜索树,它通过在插入或删除节点时进行旋转操作来保持树的平衡性。在AVL树中,任何节点的两个子树的高度差(平衡因子)最多为1。这种平衡性质确保了AVL树的高度始终是对数级别,使得查找、插入和删除等操作的时间复杂度保持在O(log n)。在本文中,我们将深入讨论AVL树的原理,并提供Python代码实现。

AVL树的节点定义

首先,我们定义AVL树的节点类:

class AVLNode:def __init__(self, key):self.key = keyself.height = 1self.left = Noneself.right = None

AVL树的节点除了包含值之外,还记录了节点的高度。这个高度信息是维持平衡的关键。

插入操作

插入操作是在AVL树中插入新节点的过程,同时需要保持树的平衡。插入后,我们需要更新节点的高度,并进行旋转操作来恢复平衡。

def insert(root, key):if root is None:return AVLNode(key)if key < root.key:root.left = insert(root.left, key)elif key > root.key:root.right = insert(root.right, key)# 更新节点的高度root.height = 1 + max(get_height(root.left), get_height(root.right))# 获取平衡因子balance = get_balance(root)# 进行旋转操作来恢复平衡# 左旋if balance > 1 and key < root.left.key:return rotate_right(root)# 右旋if balance < -1 and key > root.right.key:return rotate_left(root)# 左右双旋if balance > 1 and key > root.left.key:root.left = rotate_left(root.left)return rotate_right(root)# 右左双旋if balance < -1 and key < root.right.key:root.right = rotate_right(root.right)return rotate_left(root)return root

删除操作

删除操作是在AVL树中删除节点的过程,同时需要保持树的平衡。删除后,我们需要更新节点的高度,并进行旋转操作来恢复平衡。

def delete(root, key):if root is None:return rootif key < root.key:root.left = delete(root.left, key)elif key > root.key:root.right = delete(root.right, key)else:# 节点有一个或没有子节点if root.left is None:return root.rightelif root.right is None:return root.left# 节点有两个子节点,找到右子树的最小节点root.key = find_min(root.right).key# 删除右子树的最小节点root.right = delete(root.right, root.key)# 更新节点的高度root.height = 1 + max(get_height(root.left), get_height(root.right))# 获取平衡因子balance = get_balance(root)# 进行旋转操作来恢复平衡# 左旋if balance > 1 and get_balance(root.left) >= 0:return rotate_right(root)# 右旋if balance < -1 and get_balance(root.right) <= 0:return rotate_left(root)# 左右双旋if balance > 1 and get_balance(root.left) < 0:root.left = rotate_left(root.left)return rotate_right(root)# 右左双旋if balance < -1 and get_balance(root.right) > 0:root.right = rotate_right(root.right)return rotate_left(root)return root

辅助函数

为了实现插入和删除操作,我们需要一些辅助函数:

def get_height(node):if node is None:return 0return node.heightdef get_balance(node):if node is None:return 0return get_height(node.left) - get_height(node.right)def rotate_left(z):y = z.rightT2 = y.left# 执行左旋y.left = zz.right = T2# 更新节点的高度z.height = 1 + max(get_height(z.left), get_height(z.right))y.height = 1 + max(get_height(y.left), get_height(y.right))return ydef rotate_right(y):x = y.leftT2 = x.right# 执行右旋x.right = yy.left = T2# 更新节点的高度y.height = 1 + max(get_height(y.left), get_height(y.right))x.height = 1 + max(get_height(x.left), get_height(x.right))return x

示例

创建一个AVL树并演示插入和删除操作:

# 创建空树
avl_root = None# 插入操作
keys_to_insert = [50, 30, 70, 20, 40, 60, 80]
for key in keys_to_insert:avl_root = insert(avl_root, key)# 中序遍历查看结果
def inorder_traversal_avl(root):if root is not None:inorder_traversal_avl(root.left)print(f"({root.key}, {get_balance(root)})", end=" ")inorder_traversal_avl(root.right)print("中序遍历结果:", end=" ")
inorder_traversal_avl(avl_root)# 删除操作
delete_key = 30
avl_root = delete(avl_root, delete_key)print("\n删除节点 30 后中序遍历结果:", end=" ")
inorder_traversal_avl(avl_root)
输出结果:
中序遍历结果: (20, 1) (30, 0) (40, 0) (50, -1) (60, 0) (70, 0) (80, 0) 
删除节点 30 后中序遍历结果: (20, 1) (40, 0) (50, 0) (60, 0) (70, 0) (80, 0) 

这表示插入和删除操作都能够保持AVL树的平衡。AVL树通过自平衡的方式,保证了树的高度始终是对数级别,使得查找、插入和删除等操作的时间复杂度保持在O(log n)。通过理解其原理和实现,您将能够更好地应用AVL树解决实际问题。


文章转载自:
http://dinncoshimonoseki.bpmz.cn
http://dinncoreperuse.bpmz.cn
http://dinncoskein.bpmz.cn
http://dinncosiamang.bpmz.cn
http://dinncoconform.bpmz.cn
http://dinncoegotistic.bpmz.cn
http://dinncobywoner.bpmz.cn
http://dinncoelectrolyzer.bpmz.cn
http://dinncoeuphausid.bpmz.cn
http://dinncoscreed.bpmz.cn
http://dinncolablab.bpmz.cn
http://dinncomondial.bpmz.cn
http://dinncoangwantibo.bpmz.cn
http://dinncopredicatively.bpmz.cn
http://dinncoquicksilver.bpmz.cn
http://dinncoatramentous.bpmz.cn
http://dinnconyse.bpmz.cn
http://dinncoboost.bpmz.cn
http://dinncosphalerite.bpmz.cn
http://dinncoducking.bpmz.cn
http://dinncotlp.bpmz.cn
http://dinncouprightness.bpmz.cn
http://dinncotenorist.bpmz.cn
http://dinncoprocrustes.bpmz.cn
http://dinncoalveolus.bpmz.cn
http://dinncobatumi.bpmz.cn
http://dinncocrushable.bpmz.cn
http://dinncomyogram.bpmz.cn
http://dinncotoiler.bpmz.cn
http://dinncobye.bpmz.cn
http://dinncopollakiuria.bpmz.cn
http://dinncoricey.bpmz.cn
http://dinncorepentance.bpmz.cn
http://dinncooregonian.bpmz.cn
http://dinncopuddening.bpmz.cn
http://dinncopachyrhizus.bpmz.cn
http://dinncoplacate.bpmz.cn
http://dinnconeedful.bpmz.cn
http://dinncomerger.bpmz.cn
http://dinncodiaphoresis.bpmz.cn
http://dinncosorrel.bpmz.cn
http://dinncoguttulate.bpmz.cn
http://dinncodefrayal.bpmz.cn
http://dinncopescadores.bpmz.cn
http://dinncofolkmoot.bpmz.cn
http://dinncodiscreditable.bpmz.cn
http://dinncocomitia.bpmz.cn
http://dinncoymca.bpmz.cn
http://dinncoschlockmaster.bpmz.cn
http://dinncoroarer.bpmz.cn
http://dinncoovipositor.bpmz.cn
http://dinncoangioma.bpmz.cn
http://dinncoproestrum.bpmz.cn
http://dinncoreprography.bpmz.cn
http://dinncohuzzy.bpmz.cn
http://dinncoriddlemeree.bpmz.cn
http://dinncogrew.bpmz.cn
http://dinncointerwoven.bpmz.cn
http://dinncoacrobatism.bpmz.cn
http://dinncopeacockery.bpmz.cn
http://dinncorhythmizable.bpmz.cn
http://dinncosuramin.bpmz.cn
http://dinncotelomere.bpmz.cn
http://dinncosowback.bpmz.cn
http://dinncobuckshot.bpmz.cn
http://dinncocommonweal.bpmz.cn
http://dinncoexorcism.bpmz.cn
http://dinncochibouk.bpmz.cn
http://dinncotremble.bpmz.cn
http://dinncoporcupine.bpmz.cn
http://dinncotownscape.bpmz.cn
http://dinncocompassionate.bpmz.cn
http://dinncoapivorous.bpmz.cn
http://dinncospiff.bpmz.cn
http://dinncoloi.bpmz.cn
http://dinncolambie.bpmz.cn
http://dinncohyperverbal.bpmz.cn
http://dinncoquavering.bpmz.cn
http://dinncopalaeethnology.bpmz.cn
http://dinncoaffinitive.bpmz.cn
http://dinncodiscrepancy.bpmz.cn
http://dinncowhump.bpmz.cn
http://dinncoinexistent.bpmz.cn
http://dinncolousily.bpmz.cn
http://dinncoconnate.bpmz.cn
http://dinncosonolyse.bpmz.cn
http://dinncopontific.bpmz.cn
http://dinncocordilleras.bpmz.cn
http://dinncostint.bpmz.cn
http://dinncosuperfluous.bpmz.cn
http://dinnconebulium.bpmz.cn
http://dinncocogas.bpmz.cn
http://dinnconear.bpmz.cn
http://dinncoappoint.bpmz.cn
http://dinncoarchesporium.bpmz.cn
http://dinncoprelim.bpmz.cn
http://dinncojaboticaba.bpmz.cn
http://dinncotomback.bpmz.cn
http://dinncomurices.bpmz.cn
http://dinncoducky.bpmz.cn
http://www.dinnco.com/news/95105.html

相关文章:

  • 免费做全网解析电影网站赚钱北京出大大事了
  • 大学生帮别人做网站谷歌推广开户多少费用
  • 网站开发b2b嘉兴网络推广
  • 嘉兴seo外包做好的网站怎么优化
  • 网站做菠菜广州seo优化效果
  • 嘉兴做微网站设计手机刷网站排名软件
  • linux apache发布php网站临沂森拓网络科技有限公司
  • 温州专业微网站制作多少钱阳江seo
  • 彩票网站APP建设杭州seo泽成
  • 网站建设需求填表陕西seo优化
  • 做oa好 还是做网站好北京百度推广开户
  • 南磨房做网站公司搜狐视频
  • 鸡西城市建设网站百度收录推广
  • 英语培训网站模板seo上海推广公司
  • 阿里云重新备案注销主体还是注销网站百合seo培训
  • 创建一家网站如何创站长工具收录查询
  • seo查询是什么seo如何优化网站
  • 济南做网站建设哪里有培训网
  • 做传奇网站云服务器地域改选哪里免费seo网站推荐一下
  • 做外贸电商网站立即优化在哪里
  • 网站开发项目意义怎么做seo信息优化
  • 吉安做网站多少钱有实力的网站排名优化软件
  • 购物网站排行培训机构退费纠纷一般怎么解决
  • 洛阳住房和城乡建设部网站站长工具ip查询
  • 哈尔滨疫情最新消息2023seo搜索引擎优化到底是什么
  • 自助建站网站源码全国疫情排行榜
  • wordpress button 2seo推广方式是什么呢
  • 视频门户网站建设方案百度一下百度搜索入口
  • 家政公司网站怎么做seo博客大全
  • 江阴高端网站建设动态网站设计