博客
关于我
AVL和红黑树的一些概念
阅读量:362 次
发布时间:2019-03-04

本文共 684 字,大约阅读时间需要 2 分钟。

在这里插入图片描述

平衡二叉树

二三树
AVL树
B树
红黑树

如何保证一棵树的平衡?

二叉搜索树的查询效率只与高度有关,与结点的个数无关

(1)AVL

  1. 平衡因子 Balance Factor,绝对值小于1
    是它的左子树的高度减去它的右子树的高度(有时相反)
    balance factor = {-1,0,1}
  2. 通过选择的操作进行平衡
  3. 所有叶子结点的平衡因子为 0
    在这里插入图片描述

旋转操作:

  1. 左旋

    a. 右右子树——》左旋
    在这里插入图片描述

  2. 右旋

    a. 左左子树——》右旋
    b.

  3. 左右旋

    a. 左右子树——》左右旋

在这里插入图片描述

在这里插入图片描述

  1. 右左旋
    a. 右左子树——》右左旋

在这里插入图片描述

在这里插入图片描述

为什么要引入AVL?

因为二叉搜索树会出现极端的情况,如果二叉搜索树的结点为1、2、3、4、5,则都为右结点,相当于一个链表了,则优势全无。

平衡因子怎么定的?

查询的时间复杂度等于树的深度

在这里插入图片描述

不足:结点需要存储额外信息,且调整次数频繁

(2)近似平衡二叉树——红黑树 Red-black Tree

红黑树是一种近似平衡二叉搜索树,它能够确保任何一个结点的左右子树的高度差小于两倍。具体来说,红黑树是满足如下条件的二叉搜索树:

  1. 每个结点要么是红色,要么是黑色
  2. 根节点是黑色
  3. 每个叶结点(NIL结点,空结点)是黑色的
  4. 不能有相邻的两个红色结点
  5. 从任一结点到其每个叶子的所有路径都包含相同数目的黑色结点

在这里插入图片描述

从根到叶子的最长的可能路径不多于最短的可能路径的两倍长

AVL和红黑树的对比

在这里插入图片描述

AVL查询更好

红黑树提供了更快的插入和删除的操作,AVL旋转更多
AVL存储的额外信息更多,需要更多的内存,红黑树需要存储的节点个数更少
如果读操作多 AVL;插入操作和查询操作比较多 红黑树

转载地址:http://fder.baihongyu.com/

你可能感兴趣的文章
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>