Red-Black trees are notorious for being nightmares of pointer manipulation. Instructors will show the theory, but won’t torture their students to implement one. Interviewers will avoid asking about it. They probably couldn’t do it themselves.

You should be vaguely familiar with how you might balance a tree. The details, however, are probably unnecessary for the purposes of an interview. – Gayle McDowell, Cracking the coding interview

If you’re proficient in a functional language, you owe it to yourself to implement a Red-Black tree. You’ll be one of the few people that can code a Red-Black tree on a whiteboard.

It will make you realize why people are so excited about the whole functional programming thing.


What is a Red-Black Tree?

A Red-Black tree is a balanced binary search tree. Every node is colored red or black. Three rules hold:

  1. No red node has a red child.
  2. Every path from the root to an empty node contains the same number of black nodes.
  3. An empty node is always black.

Draw a tree with these rules. Notice it’s always relatively-balanced. Try to draw one as unbalanced as possible. You won’t get far.

You can prove the maximum depth of a node is at most 2


Implementation

Let’s implement a set with a Red-Black tree. At minimum we’ll need a member function and an insertfunction.


Data

A tree can be empty, or it can be a node with two subtrees, a color, and an element.

data Tree a = Empty -- Empty does not need a color, it's always black. | T Color (Tree a) a (Tree a) data Color = R | B


Member

The member function searches for an element. It’s a binary search.

member :: Ord a => Tree a -> a -> Bool member (T _ left e right) x | x == e = True | x < e = member left x | x > e = member right x member Empty _ = False


Insert

The insert function uses the function build, which is a constructor that makes sure the node is balanced.

insert :: Ord a => a -> Tree a -> Tree a insert x s = let T _ a y b = ins s in T B a y b where ins s'@(T color a' y' b') | x < y' = build color (ins a') y' b' | x > y' = build color a' y' (ins b') | otherwise = s' ins Empty = T R Empty x Empty

There are four cases when build needs to adjust a node. It detects the case when a black parent has a red child with a red child. It shifts the nodes around to fix it. The solution is the same in every case. (Notice the right hand sides of build are the same).

build :: Color -> Tree a -> a -> Tree a -> Tree a build B (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d) build B (T R a x (T R b y c)) z d = T R (T B a x b) y (T B c z d) build B a x (T R (T R b y c) z d) = T R (T B a x b) y (T B c z d) build B a x (T R b y (T R c z d)) = T R (T B a x b) y (T B c z d) build color left x right = T color left x right

Afterwards

That’s it. You have a Red-Black tree.

If you want to learn more, read Purely Functional Data Structures by Chris Okasaki. I stole most of my implementation from this book. The build diagram is also from the book.




module RedBlackSet( empty, member , insert ) where data Tree a = Empty | T Color (Tree a) a (Tree a) data Color = R | B empty :: Ord a => Tree a empty = Empty member :: Ord a => Tree a -> a -> Bool member (T _ left e right) x | x == e = True | x < e = member left x | x > e = member right x member Empty _ = False insert :: Ord a => a -> Tree a -> Tree a insert x s = let T _ a y b = ins s in T B a y b where ins s'@(T color a' y' b') | x < y' = build color (ins a') y' b' | x > y' = build color a' y' (ins b') | otherwise = s' ins Empty = T R Empty x Empty build :: Color -> Tree a -> a -> Tree a -> Tree a build B (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d) build B (T R a x (T R b y c)) z d = T R (T B a 

转载于:https://www.cnblogs.com/kcbsbo/p/4785648.html

The easy way to implement a Red-Black tree相关推荐

  1. 红黑树Red/Black Tree

    红黑树Red/Black Tree 建立二进制搜索树,我们得到红/黑树,旨在解决BST可能变得不平衡的问题.(BST[二叉搜索树],是对于任意的node x,如果node y是node x的左边的节点 ...

  2. 红黑树(Red Black Tree)详解

    红黑树 红黑树(Red Black Tree) 是一种自平衡二叉查找树,是在计算机科学中用到的一种数据结构,典型的用途是实现关联数组. 红黑树是在1972年由Rudolf Bayer发明的,当时被称为 ...

  3. JS 实现红黑树(Red Black Tree)

    文章目录 JS 实现红黑树(Red Black Tree) 前言 概念 树的定义 插入操作 疑问一:新增结点是红是黑 插入,简单记录 删除操作 删除结点 平衡结点 单边结点 叶子结点 结语 代码 JS ...

  4. 红黑树(Red Black Tree)的简单理解

    红黑树(Red Black Tree)的简单理解 前言介绍 AVL树的简单介绍 为什么需要AVL树? AVL树的调整过程 AVL树的缺点 红黑树与AVL相比 234树的简单介绍 234树的概念 234 ...

  5. ACM-ICPC 2018 青岛赛区网络预赛 B. Red Black Tree (LCA、二分)

    传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5807 题意: 给出一棵树,根节点为1.每条边有一个距离,树上有m个点为 ...

  6. 【二叉树进阶】红黑树(Red Black Tree) - 平衡二叉搜索树

    文章目录 一.红黑树的概念 二.红黑树的性质 2.1 红黑树和AVL树效率对比 三.红黑树的结构(KV模型) 四.红黑树的插入 4.1 插入节点 4.2 平衡化操作(难点) 4.2.1 情况一 4.2 ...

  7. 红黑树(Red Black Tree)超详细解析

    红黑树详解 什么是红黑树? ​ 红黑树,是一种二叉搜索树的特化,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black. 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确 ...

  8. LeetCode - Easy - 637. Average of Levels in Binary Tree

    Topic Tree Description https://leetcode.com/problems/average-of-levels-in-binary-tree/ Given the roo ...

  9. 数据结构--红黑树 Red Black Tree

    文章目录 1.概念 2.操作 2.1 左旋.右旋(围绕某个节点的左/右旋) 2.2 插入 2.3 删除 3. 代码 1.概念 二叉树在频繁动态增删后,可能退化成链表,时间复杂度由 O(lgn) 变成 ...

最新文章

  1. lol服务器显示未知错误,运行英雄联盟出现未知错误的处理方法
  2. 转:csdn怎么快速转载别人的文章
  3. java二维整型数组 各行和排序_java:已知我有一个二维数组,直接输入全部数据,怎么对数组的每行进行升序排序...
  4. java的字节码无法显示_【java】查看Java字节码文件内容的方法+使用javap找不到类 解决方法...
  5. linux下安装nodejs及cnpm
  6. gromacs 安装_GROMACS:粗粒化力场建立和模拟上线!
  7. 服务器增加驱动器,向存储空间直通添加服务器或驱动器
  8. 小程序毫秒级倒计时(适用于拼团秒杀功能)
  9. 获取cookies的简单代码(总结待续)
  10. UITableView方法详解
  11. python json解释器_Python JSON
  12. 计算机绘图说课视频,电气工程制图说课ppt课件
  13. pychart绘制中国地图用英文省名
  14. c语言身高和标准体重,c语言4-15 输出标准身高体重对照表
  15. 来一起学习脚本语言吧,简单,高效,解放双手,感受自由!
  16. 在脉脉匿名频道上看了这些公司的评价后,这里有几个结论
  17. 计算机取代人脑的英语作文,关于电脑和人脑差别的英语作文
  18. 新BOS2.0物流业务逻辑
  19. Scala:try match
  20. 福建计算机如何报名,福建2020年9月计算机考试如何报名

热门文章

  1. Forms Builder常用函数
  2. WinXP系统下安装SQL SERVER 2000
  3. Vue实践--v-model实现简易计算器
  4. 《密码与安全新技术专题》第1周作业
  5. 【多项式求逆】[BZOJ3456]城市规划
  6. C# 反射 设置字段值无效的解决办法
  7. 谁能答对这道题?如有兴趣,请留下算法,呵呵~
  8. [Vue.js] 路由 -- 前端路由
  9. Python异常:IndentationError: unexpected unindent
  10. Angular Service