高度平衡树 -- AVL 树
Scheme 的表达, 优雅.
#lang scheme
( define nil '() )
( define ( root tree )( car tree ) )
( define ( left-tree tree )( cadr tree ) )
( define ( right-tree tree )( caddr tree ) )
( define ( height tree )
( cond [ ( null? tree ) 0 ]
[ else ( cadddr tree ) ] ) )
( define ( make-leaf elem )( list elem nil nil 1 ) )
( define ( make-avl-tree root left right )
( list root left right ( + 1 ( max ( height left )
( height right ) ) ) ) )
( define ( contains-elem?
elem tree )
( cond [ ( null? tree ) false ]
[ ( = elem ( root tree ) ) true ]
[ ( < elem ( root tree ) )
( contains-elem?
elem ( left-tree tree ) ) ]
[ ( > elem ( root tree ) )
( contains-elem? elem ( right-tree tree ) ) ] ) )
( define ( rotate-left-left tree )
( cond [ ( null? tree ) tree ]
[ else ( make-avl-tree ( root ( left-tree tree ) )
( left-tree ( left-tree tree ) )
( make-avl-tree ( root tree )
( right-tree ( left-tree tree ) )
( right-tree tree ) ) ) ] ) )
( define ( rotate-right-right tree )
( cond [ ( null? tree ) tree ]
[ else ( make-avl-tree ( root ( right-tree tree ) )
( make-avl-tree ( root tree )
( left-tree tree )
( left-tree ( right-tree tree ) ) )
( right-tree ( right-tree tree ) ) ) ] ) )
( define ( rotate-right-left tree )
( cond [ ( null?
tree ) tree ]
[ else ( make-avl-tree ( left-tree ( right-tree tree ) )
( make-avl-tree ( root tree )
( left-tree tree )
( left-tree ( left-tree ( right-tree tree ) ) ) )
( make-avl-tree ( root ( right-tree tree ) )
( right-tree ( left-tree ( right-tree tree ) ) )
( right-tree ( right-tree tree ) ) ) ) ] ) )
( define ( rotate-left-right tree )
( cond [ ( null?
tree ) tree ]
[ else ( make-avl-tree ( root ( right-tree ( left-tree tree ) ) )
( make-avl-tree ( root ( left-tree tree ) )
( left-tree ( left-tree tree ) )
( left-tree ( right-tree ( left-tree tree ) ) ) )
( make-avl-tree ( root tree )
( right-tree ( right-tree ( left-tree tree ) ) )
( right-tree tree ) ) ) ] ) )
( define ( balance-avl-tree tree )
( define ( factor tree )
( - ( height ( right-tree tree ) )
( height ( left-tree tree ) ) ) )
( let ( [ f ( factor tree ) ] )
( cond [ ( = f 2 )
( cond [ ( < ( factor ( right-tree tree ) ) 0 )
( rotate-right-left tree ) ]
[ else ( rotate-right-right tree ) ] ) ]
[ ( = f -2 )
( cond [ ( > ( factor ( left-tree tree ) ) 0 )
( rotate-left-right tree ) ]
[ else ( rotate-left-left tree ) ] ) ]
[ else tree ] ) ) )
( define ( insert-elem elem tree )
( define ( insert-in-son elem tree )
( cond [ ( null? tree )
( make-leaf elem ) ]
[ ( < elem ( root tree ) )
( let* ( [ newLeftTree ( insert-in-son elem ( left-tree tree ) ) ]
[ newAVLTree ( make-avl-tree ( root tree )
newLeftTree
( right-tree tree ) ) ] )
( balance-avl-tree newAVLTree ) ) ]
[ ( > elem ( root tree ) )
( let* ( [ newRightTree ( insert-in-son elem ( right-tree tree ) ) ]
[ newAVLTree ( make-avl-tree ( root tree )
( left-tree tree )
newRightTree ) ] )
( balance-avl-tree newAVLTree ) ) ]
[ else tree ] ) )
( cond [ ( contains-elem? elem tree ) tree ]
[ else ( insert-in-son elem tree ) ] ) )
( define ( delete-elem elem tree )
( define ( delete-left-most tree )
( cond [ ( left-empty? tree ) tree ]
[ else ( let* ( [ leftMost ( delete-left-most ( left-tree tree ) ) ]
[ newRightTree ( make-avl-tree ( root tree )
( right-tree leftMost )
( right-tree tree ) ) ] )
( make-avl-tree ( root leftMost )
nil
( balance-avl-tree newRightTree ) ) ) ] ) )
( define ( delete-in-son elem tree )
( cond [ ( < elem ( root tree ) )
( let* ( [ newLeftTree ( delete-in-son elem ( left-tree tree ) ) ]
[ newAVLTree ( make-avl-tree ( root tree )
newLeftTree
( right-tree tree ) ) ] )
( balance-avl-tree newAVLTree ) ) ]
[ ( > elem ( root tree ) )
( let* ( [ newRightTree ( delete-in-son elem ( right-tree tree ) ) ]
[ newAVLTree ( make-avl-tree ( root tree )
( left-tree tree )
newRightTree ) ] )
( balance-avl-tree newAVLTree ) ) ]
[ ( = elem ( root tree ) )
( cond [ ( and ( right-empty? tree )
( left-empty? tree ) )
nil ]
[ ( right-empty? tree )
( left-tree tree ) ]
[ ( left-empty? tree )
( right-tree tree ) ]
[ else ( let ( [ leftMost ( delete-left-most ( right-tree tree ) ) ] )
( make-avl-tree ( root leftMost )
( left-tree tree )
( right-tree leftMost ) ) ) ] ) ] ) )
( define ( left-empty? tree )( null?
( left-tree tree ) ) )
( define ( right-empty? tree )( null?
( right-tree tree ) ) )
( cond [ ( contains-elem?
elem tree )
( delete-in-son elem tree ) ]
[ else tree ] ) )
( define ( list->avl elems )
( define ( iter elems tree )
( cond [ ( null?
elems ) tree ]
[ else ( iter ( cdr elems )
( insert-elem ( car elems ) tree ) ) ] ) )
( cond [ ( null? elems ) '() ]
[ else ( let( [ avl ( make-leaf ( car elems ) ) ] )
( iter ( cdr elems ) avl ) ) ] ) )
高度平衡树 -- AVL 树的更多相关文章
- 【数据结构】平衡二叉树—AVL树
(百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...
- AVL树(一)之 图文解析 和 C语言的实现
概要 本章介绍AVL树.和前面介绍"二叉查找树"的流程一样,本章先对AVL树的理论知识进行简单介绍,然后给出C语言的实现.本篇实现的二叉查找树是C语言版的,后面章节再分别给出C++ ...
- AVL树的左旋右旋理解 (转)
AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增加和删除可能需要通过一次或多 ...
- 简单数据结构———AVL树
C - 万恶的二叉树 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB 64b ...
- 算法二叉搜索树之AVL树
最近学习了二叉搜索树中的AVL树,特在此写一篇博客小结. 1.引言 对于二叉搜索树而言,其插入查找删除等性能直接和树的高度有关,因此我们发明了平衡二叉搜索树.在计算机科学中,AVL树是最先发明的自平衡 ...
- AVL树,红黑树
AVL树 https://baike.baidu.com/item/AVL%E6%A0%91/10986648 在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高 ...
- AVL树的理解及自写AVL树
AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增加和删除可能需要通过一次或多 ...
- 红黑树与AVL树
概述:本文从排序二叉树作为引子,讲解了红黑树,最后把红黑树和AVL树做了一个比较全面的对比. 1 排序二叉树 排序二叉树是一种特殊结构的二叉树,可以非常方便地对树中所有节点进行排序和检索. 排序二叉树 ...
- AVL树的实现——c++
一.概念 AVL树是根据它的发明者G.M. Adelson-Velsky和E.M. Landis命名的.它是最先发明的自平衡二叉查找树,也被称为高度平衡树.相比于"二叉查找树",它 ...
随机推荐
- 小白入门篇:flex布局
--前言 因为这个星期写一个小的项目用到flex布局和grid布局,虽然这两种布局都是兼容性都有问题,但是别急,我觉的以后肯定是会发展并且流行起来的,毕竟google大法好,而且这两个布局真的比一般的 ...
- 洛谷——P1093 奖学金
P1093 奖学金 题目描述 某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金.期末,每个学生都有3门课的成绩:语文.数学.英语.先按总分从高到低排序,如果两个同学总分相 ...
- Guess Number Higher or Lower II -- LeetCode
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- Android 更新UI的两种方法——handler和runOnUiThread()
今天看到了一个runOnUiThread()方法用来更新UI,觉得很神奇!! 方法一:handler机制不说了. 方法二:利用Activity.runOnUiThread(Runnable)把更新ui ...
- 个人博客 V0.0.3 版本 ...
早就想弄个人博客网站,一直拖到现在...事情总是忙不完的,想能力提升的快,只能挤时间多练多写了,Keep On Unsleeping 以后原创的笔记都会在自己的博客网站写,博客园和简书就用来转发文章了 ...
- Dreamweaver CS5个性化设置
Dreamweaver CS5个性化设置 编辑 --> 转到行 显示工具栏(上面包含“新建.保存.保存全部”等按钮) 查看 --> 工具栏 --> 标准 也可右键单击显示菜单 延伸阅 ...
- Visio中如何画面积一样的形状
如图,刚开始我想着用辅助的那些线(对齐),实现不了,后来想想直接复制就行了...........
- delphi 浮点数转换成十六进制字符串的方法
我们在研究封包技术时,经常会碰到将浮点数转换成十六进制形式.比如在游戏中人物的座标,经常就用浮点数来表示.怎么将浮点数转换成十六进制字符串形式呢?下面我将写出其在DELPHI中的方法. 先 ...
- Jsp中如何在<c:forEach>标签内获取集合的长度
利用jstl标签functions的prefix属性的length属性值 1.首先在jsp页面导入jstl function标签 <%@ taglib prefix="fn" ...
- 机器学习算法实践:朴素贝叶斯 (Naive Bayes)(转载)
前言 上一篇<机器学习算法实践:决策树 (Decision Tree)>总结了决策树的实现,本文中我将一步步实现一个朴素贝叶斯分类器,并采用SMS垃圾短信语料库中的数据进行模型训练,对垃圾 ...