博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The easy way to implement a Red-Black tree
阅读量:4650 次
发布时间:2019-06-09

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

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?

try

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

balance

Afterwards

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

If you want to learn more, read  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

你可能感兴趣的文章
图像处理笔记(十八):模板匹配
查看>>
Educational Codeforces Round 60 D. Magic Gems
查看>>
c# 保存和打开文件的方法
查看>>
调用图灵机器人API实现简单聊天
查看>>
MATLAB indexing question
查看>>
MATLAB 求解最优化问题
查看>>
【转载】java InputStream读取数据问题
查看>>
fatal error LNK1120: 11 unresolved externals
查看>>
测试工具类汇总
查看>>
WEB消息推送-comet4j
查看>>
安卓开发 数据存储
查看>>
贪心思维 专题记录 2017-7-21
查看>>
vue-router 跳转原理
查看>>
strncpy函数使用
查看>>
(一)SOA学习-相关缩写
查看>>
Apache ab 压力测试工具
查看>>
noi.ac NOIP2018 全国热身赛 第四场 T1 tree
查看>>
(转)linux下vi编辑器编写C语言的配置
查看>>
多线程基础知识 转
查看>>
MyBatis generator 使用方式 小结
查看>>