The easy way to implement a Red-Black tree

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 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值