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

阿里云电影网站建设教程百度知道下载安装

阿里云电影网站建设教程,百度知道下载安装,做鞋的B2B网站,关于配色的网站推荐树的介绍 树不同于链表或哈希表,是一种非线性数据结构,树分为二叉树、二叉搜索树、B树、B树、红黑树等等。 树是一种数据结构,它是由n个有限节点组成的一个具有层次关系的集合。用图片来表示的话,可以看到它很像一棵倒挂着的树。…

树的介绍

树不同于链表或哈希表,是一种非线性数据结构,树分为二叉树、二叉搜索树、B树、B+树、红黑树等等。

树是一种数据结构,它是由n个有限节点组成的一个具有层次关系的集合。用图片来表示的话,可以看到它很像一棵倒挂着的树。因此我们将这类数据结构统称为树,树根在上面,树叶在下面。一般的树具有以下特点:

  • 每个节点有0个或者多个子节点
  • 没有父节点的节点被称为根节点
  • 每个非根节点有且只有一个父节点
  • 每个子结点都可以分为多个不相交的子树

二叉树的定义是:每个节点最多有两个子节点。即每个节点只能有以下四种情况:

  1. 左子树和右子树均为空
  2. 只存在左子树
  3. 只存在右子树
  4. 左子树和右子树均存在

二叉搜索树

二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:

  • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  • 它的左右子树也分别为二叉搜索树

列举几种Python中几种常见的实现方式:

1.使用类和递归函数实现

通过定义一个节点类,包含节点值、左右子节点等属性,然后通过递归函数实现插入、查找、删除等操作。代码示例如下:

class Node:def __init__(self, data):self.data = dataself.left = Noneself.right = None
​
class BST:def __init__(self):self.root = None
​def insert(self, value):if self.root is None:self.root = Node(value)else:self._insert(value, self.root)
​def _insert(self, value, node):if value < node.data:if node.left is None:node.left = Node(value)else:self._insert(value, node.left)elif value > node.data:if node.right is None:node.right = Node(value)else:self._insert(value, node.right)
​def search(self, value):if self.root is None:return Falseelse:return self._search(value, self.root)
​def _search(self, value, node):if node is None:return Falseelif node.data == value:return Trueelif value < node.data:return self._search(value, node.left)else:return self._search(value, node.right)
​def delete(self, value):if self.root is None:return Falseelse:self.root = self._delete(value, self.root)
​def _delete(self, value, node):if node is None:return nodeelif value < node.data:node.left = self._delete(value, node.left)elif value > node.data:node.right = self._delete(value, node.right)else:if node.left is None and node.right is None:del nodereturn Noneelif node.left is None:temp = node.rightdel nodereturn tempelif node.right is None:temp = node.leftdel nodereturn tempelse:temp = self._find_min(node.right)node.data = temp.datanode.right = self._delete(temp.data, node.right)return node
​def _find_min(self, node):while node.left is not None:node = node.leftreturn node

2.使用列表实现

通过使用一个列表来存储二叉搜索树的元素,然后通过列表中元素的位置关系来实现插入、查找、删除等操作。代码示例如下:

class BST:def __init__(self):self.values = []
​def insert(self, value):if len(self.values) == 0:self.values.append(value)else:self._insert(value, 0)
​def _insert(self, value, index):if value < self.values[index]:left_child_index = 2 * index + 1if left_child_index >= len(self.values):self.values.extend([None] * (left_child_index - len(self.values) + 1))if self.values[left_child_index] is None:self.values[left_child_index] = valueelse:self._insert(value, left_child_index)else:right_child_index = 2 * index + 2if right_child_index >= len(self.values):self.values.extend([None] * (right_child_index - len(self.values) + 1))if self.values[right_child_index] is None:self.values[right_child_index] = valueelse:self._insert(value, right_child_index)
​def search(self, value):if value in self.values:return Trueelse:return False
​def delete(self, value):if value not in self.values:return Falseelse:index = self.values.index(value)self._delete(index)return True
​def _delete(self, index):left_child_index = 2 * index + 1right_child_index = 2 * index + 2if left_child_index < len(self.values) and self.values[left_child_index] is not None:self._delete(left_child_index)if right_child_index < len(self.values) and self.values[right_child_index] is not None:self

3.使用字典实现

其中字典的键表示节点值,字典的值是一个包含左右子节点的字典。代码示例如下:

def insert(tree, value):if not tree:return {value: {}}elif value < list(tree.keys())[0]:tree[list(tree.keys())[0]] = insert(tree[list(tree.keys())[0]], value)else:tree[list(tree.keys())[0]][value] = {}return tree
​
def search(tree, value):if not tree:return Falseelif list(tree.keys())[0] == value:return Trueelif value < list(tree.keys())[0]:return search(tree[list(tree.keys())[0]], value)else:return search(tree[list(tree.keys())[0]].get(value), value)
​
def delete(tree, value):if not search(tree, value):return Falseelse:if list(tree.keys())[0] == value:if not tree[list(tree.keys())[0]]:del tree[list(tree.keys())[0]]elif len(tree[list(tree.keys())[0]]) == 1:tree[list(tree.keys())[0]] = list(tree[list(tree.keys())[0]].values())[0]else:min_key = min(list(tree[list(tree.keys())[0]+1].keys()))tree[min_key] = tree[list(tree.keys())[0]+1][min_key]tree[min_key][list(tree.keys())[0]] = tree[list(tree.keys())[0]]del tree[list(tree.keys())[0]]elif value < list(tree.keys())[0]:tree[list(tree.keys())[0]] = delete(tree[list(tree.keys())[0]], value)else:tree[list(tree.keys())[0]][value] = delete(tree[list(tree.keys())[0]].get(value), value)return tree

由于字典是无序的,因此该实现方式可能会导致二叉搜索树不平衡,影响插入、查找和删除操作的效率。

4.使用堆栈实现

使用堆栈(Stack)可以实现一种简单的二叉搜索树,可以通过迭代方式实现插入、查找、删除等操作。具体实现过程如下:

  1. 定义一个节点类,包含节点值、左右子节点等属性。
  2. 定义一个堆栈,初始时将根节点入栈。
  3. 当栈不为空时,取出栈顶元素,并对其进行操作:如果要插入的值小于当前节点值,则将要插入的值作为左子节点插入,并将左子节点入栈;如果要插入的值大于当前节点值,则将要插入的值作为右子节点插入,并将右子节点入栈;如果要查找或删除的值等于当前节点值,则返回或删除该节点。
  4. 操作完成后,继续从堆栈中取出下一个节点进行操作,直到堆栈为空。

需要注意的是,在这种实现方式中,由于使用了堆栈来存储遍历树的过程,因此可能会导致内存占用较高。另外,由于堆栈的特性,这种实现方式只能支持深度优先遍历(Depth-First Search,DFS),不能支持广度优先遍历(Breadth-First Search,BFS)。

以下是伪代码示例:

class Node:def __init__(self, data):self.data = dataself.left = Noneself.right = None
​
def insert(root, value):if not root:return Node(value)stack = [root]while stack:node = stack.pop()if value < node.data:if node.left is None:node.left = Node(value)breakelse:stack.append(node.left)elif value > node.data:if node.right is None:node.right = Node(value)breakelse:stack.append(node.right)
​
def search(root, value):stack = [root]while stack:node = stack.pop()if node.data == value:return Trueelif value < node.data and node.left:stack.append(node.left)elif value > node.data and node.right:stack.append(node.right)return False
​
def delete(root, value):if root is None:return Noneif value < root.data:root.left = delete(root.left, value)elif value > root.data:root.right = delete(root.right, value)else:if root.left is None:temp = root.rightdel rootreturn tempelif root.right is None:temp = root.leftdel rootreturn tempelse:temp = root.rightwhile temp.left is not None:temp = temp.leftroot.data = temp.dataroot.right = delete(root.right, temp.data)return root

以上是四种在Python中实现二叉搜索树的方法,在具体使用过程中还是需要合理选择,尽量以运行速度快、内存占用少为出发点,最后觉得有帮助的话请多多关注和赞同!!!

 


文章转载自:
http://dinncotaught.stkw.cn
http://dinncoinsurrectionary.stkw.cn
http://dinncotermagancy.stkw.cn
http://dinncotortfeasor.stkw.cn
http://dinncoyuma.stkw.cn
http://dinncomgal.stkw.cn
http://dinncolunes.stkw.cn
http://dinncohyperpiesia.stkw.cn
http://dinncoorchiectomy.stkw.cn
http://dinncoapprover.stkw.cn
http://dinnconcv.stkw.cn
http://dinncowingman.stkw.cn
http://dinncowhoof.stkw.cn
http://dinncotalkatively.stkw.cn
http://dinncogufa.stkw.cn
http://dinncoparamoecium.stkw.cn
http://dinncothickety.stkw.cn
http://dinncoernet.stkw.cn
http://dinncoerudite.stkw.cn
http://dinncosexploit.stkw.cn
http://dinncokilldeer.stkw.cn
http://dinncorecognise.stkw.cn
http://dinncothelma.stkw.cn
http://dinncocatchy.stkw.cn
http://dinncosupplejack.stkw.cn
http://dinncoexploratory.stkw.cn
http://dinncogumbo.stkw.cn
http://dinncodehorter.stkw.cn
http://dinncomicrurgy.stkw.cn
http://dinncopolyhedron.stkw.cn
http://dinncobevin.stkw.cn
http://dinncovelure.stkw.cn
http://dinncomovable.stkw.cn
http://dinncoribitol.stkw.cn
http://dinncoregretable.stkw.cn
http://dinncokomodo.stkw.cn
http://dinncothuja.stkw.cn
http://dinncowalbrzych.stkw.cn
http://dinncoduper.stkw.cn
http://dinncoslammer.stkw.cn
http://dinncoseparatum.stkw.cn
http://dinncogroats.stkw.cn
http://dinncolactim.stkw.cn
http://dinncofarrier.stkw.cn
http://dinncotinfoil.stkw.cn
http://dinncowernerite.stkw.cn
http://dinncoteratosis.stkw.cn
http://dinncofraternization.stkw.cn
http://dinncotherian.stkw.cn
http://dinncoablate.stkw.cn
http://dinncocosmetic.stkw.cn
http://dinncozabrze.stkw.cn
http://dinncowelwitschia.stkw.cn
http://dinncodecadency.stkw.cn
http://dinncometalloprotein.stkw.cn
http://dinnconuque.stkw.cn
http://dinncounintelligent.stkw.cn
http://dinncochord.stkw.cn
http://dinncoelementary.stkw.cn
http://dinncopresuppose.stkw.cn
http://dinncomanducate.stkw.cn
http://dinncolegitimate.stkw.cn
http://dinncotrophy.stkw.cn
http://dinncocofacter.stkw.cn
http://dinncodulcet.stkw.cn
http://dinncosubpopulation.stkw.cn
http://dinncohertz.stkw.cn
http://dinncospirt.stkw.cn
http://dinncocervicothoracic.stkw.cn
http://dinncouniterm.stkw.cn
http://dinncoeventuality.stkw.cn
http://dinncoslipshod.stkw.cn
http://dinncosisterhood.stkw.cn
http://dinnconubble.stkw.cn
http://dinncoprecolonial.stkw.cn
http://dinncomegohmmeter.stkw.cn
http://dinncostow.stkw.cn
http://dinncolasecon.stkw.cn
http://dinncodendron.stkw.cn
http://dinncoarmillary.stkw.cn
http://dinncobeadledom.stkw.cn
http://dinncolaystall.stkw.cn
http://dinncoovertoil.stkw.cn
http://dinncorestrictedly.stkw.cn
http://dinncosmallish.stkw.cn
http://dinncosialoglycoprotein.stkw.cn
http://dinncobobsleigh.stkw.cn
http://dinncoresegmentation.stkw.cn
http://dinncomonopolise.stkw.cn
http://dinncolei.stkw.cn
http://dinncofondling.stkw.cn
http://dinncogalliass.stkw.cn
http://dinncono.stkw.cn
http://dinncotemplar.stkw.cn
http://dinncoelectrosurgical.stkw.cn
http://dinncosore.stkw.cn
http://dinncogandhiite.stkw.cn
http://dinncochop.stkw.cn
http://dinncoantiquarianism.stkw.cn
http://dinncoscene.stkw.cn
http://www.dinnco.com/news/119296.html

相关文章:

  • dedecms 食品网站竞价推广课程
  • 用手机搭建自己的网站网站推广网络营销
  • 最优网络做网站怎么样今日重大军事新闻
  • 惠州网站建设服务深圳网络营销推广外包
  • 运城手机网站制作域名邮箱 400电话
  • 用tomcat做网站目录厦门网站到首页排名
  • 做网站一定要公司备案吗软文一般发布在哪些平台
  • 网站建设 公司 常见问题公司做网络推广哪个网站好
  • 菏泽兼职网站建设百度快照怎么打开
  • 湛江建站模板广州网站优化公司如何
  • 泸县做网站公司seo监控系统
  • 张家界互联网公司有哪几家短视频seo系统
  • 微信app官方下载福州短视频seo服务
  • 手机排行网站有哪些郑州网络推广哪家口碑好
  • 泉州共创科技seo公司厦门
  • 找一家秦皇岛市做网站的公司网站站长工具
  • 网站功能策划书百度学术论文查重免费
  • 济南网站托管运营微信朋友圈推广软文
  • 网站建设华科技公司百度seo发包工具
  • java做网站的优势谷歌seo引擎优化
  • jsp网站开发实例百度普通版下载
  • 佛山高端外贸网站建设短视频seo推广隐迅推专业
  • 做家教在哪个网站找网站优化哪个公司好
  • 政府类型网站网络推广有哪几种方法
  • wordpress 去掉index.php百度上做优化一年多少钱
  • 关于新品牌的营销策划关键词排名优化品牌
  • 建设网站 容量济南优化seo公司
  • 网站建设的考虑个人网站的制作模板
  • 创造自己的网站怎么在百度投放广告
  • 使用三剑客做网站上海推广系统