Red%20Black%20Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Red%20Black%20Trees

Description:

Red Black Trees Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path ... – PowerPoint PPT presentation

Number of Views:251
Avg rating:3.0/5.0
Slides: 50
Provided by: Preferr1714
Category:
Tags: 20black | 20trees | red

less

Transcript and Presenter's Notes

Title: Red%20Black%20Trees


1
Red Black Trees
  • Colored Nodes Definition
  • Binary search tree.
  • Each node is colored red or black.
  • Root and all external nodes are black.
  • No root-to-external-node path has two consecutive
    red nodes.
  • All root-to-external-node paths have the same
    number of black nodes

2
Red Black Trees
  • Colored Edges Definition
  • Binary search tree.
  • Child pointers are colored red or black.
  • Pointer to an external node is black.
  • No root to external node path has two consecutive
    red pointers.
  • Every root to external node path has the same
    number of black pointers.

3
Example Red-Black Tree
4
Properties
  • The height of a red black tree that has n
    (internal) nodes is between log2(n1) and
    2log2(n1).

5
Properties
  • Start with a red black tree whose height is h
    collapse all red nodes into their parent black
    nodes to get a tree whose node-degrees are
    between 2 and 4, height is gt h/2, and all
    external nodes are at the same level.

6
Properties
7
Properties
  • Let hgt h/2 be the height of the collapsed tree.
  • In worst-case, all internal nodes of collapsed
    tree have degree 2.
  • Number of internal nodes in collapsed tree gt
    2h-1.
  • So, n gt 2h-1
  • So, h lt 2 log2 (n 1)

8
Properties
  • At most 1 rotation and O(log n) color flips per
    insert/delete.
  • Priority search trees.
  • Two keys per element.
  • Search tree on one key, priority queue on other.
  • Color flip doesnt disturb priority queue
    property.
  • Rotation disturbs priority queue property.
  • O(log n) fix time per rotation gt O(log2n)
    overall time.

9
Properties
  • O(1) amortized complexity to restructure
    following an insert/delete.
  • C STL implementation
  • java.util.TreeMap gt red black tree

10
Insert
  • New pair is placed in a new node, which is
    inserted into the red-black tree.
  • New node color options.
  • Black node gt one root-to-external-node path has
    an extra black node (black pointer).
  • Hard to remedy.
  • Red node gt one root-to-external-node path may
    have two consecutive red nodes (pointers).
  • May be remedied by color flips and/or a rotation.

11
Classification Of 2 Red Nodes/Pointers
LLb
  • XYz
  • X gt relationship between gp and pp.
  • pp left child of gp gt X L.
  • Y gt relationship between pp and p.
  • p right child of pp gt Y R.
  • z b (black) if d null or a black node.
  • z r (red) if d is a red node.

12
XYr
  • Color flip.
  • Move p, pp, and gp up two levels.
  • Continue rebalancing if necessary.

13
LLb
  • Rotate.
  • Done!
  • Same as LL rotation of AVL tree.

14
LRb
  • Rotate.
  • Done!
  • Same as LR rotation of AVL tree.
  • RRb and RLb are symmetric.

15
Delete
  • Delete as for unbalanced binary search tree.
  • If red node deleted, no rebalancing needed.
  • If black node deleted, a subtree becomes one
    black pointer (node) deficient.

16
Delete A Black Leaf
  • Delete 8.

17
Delete A Black Leaf
py
y
  • y is root of deficient subtree.
  • py is parent of y.

18
Delete A Black Degree 1 Node
py
y
  • Delete 45.
  • y is root of deficient subtree.

19
Delete A Black Degree 2 Node
  • Not possible, degree 2 nodes are never deleted.

20
Rebalancing Strategy
  • If y is a red node, make it black.

21
Rebalancing Strategy
  • Now, no subtree is deficient. Done!

22
Rebalancing Strategy
  • y is a black root (there is no py).
  • Entire tree is deficient. Done!

23
Rebalancing Strategy
  • y is black but not the root (there is a py).
  • Xcn
  • y is right child of py gt X R.
  • Pointer to v is black gt c b.
  • v has 1 red child gt n 1.

24
Rb0 (case 1)
  • Color change.
  • Now, py is root of deficient subtree.
  • Continue!

25
Rb0 (case 2)
  • Color change.
  • Deficiency eliminated.
  • Done!

26
Rb1 (case 1)
  • LL rotation.
  • Deficiency eliminated.
  • Done!

27
Rb1 (case 2)
  • LR rotation.
  • Deficiency eliminated.
  • Done!

28
Rb2
  • LR rotation.
  • Deficiency eliminated.
  • Done!

29
Rr(n)
  • n of red children of vs right child w.

Rr(2)
30
Rr(0)
  • LL rotation.
  • Done!

31
Rr(1) (case 1)
  • LR rotation.
  • Deficiency eliminated.
  • Done!

32
Rr(1) (case 2)
  • Rotation.
  • Deficiency eliminated.
  • Done!

33
Rr(2)
  • Rotation.
  • Deficiency eliminated.
  • Done!

34
B-Trees
  • Large degree B-trees used to represent very large
    dictionaries that reside on disk.
  • Smaller degree B-trees used for internal-memory
    dictionaries to overcome cache-miss penalties.

35
AVL Trees
  • n 230 109 (approx).
  • 30 lt height lt 43.
  • When the AVL tree resides on a disk, up to 43
    disk access are made for a search.
  • This takes up to (approx) 4 seconds.
  • Not acceptable.

36
Red-Black Trees
  • n 230 109 (approx).
  • 30 lt height lt 60.
  • When the red-black tree resides on a disk, up to
    60 disk access are made for a search.
  • This takes up to (approx) 6 seconds.
  • Not acceptable.

37
m-way Search Trees
  • Each node has up to m 1 pairs and m children.
  • m 2 gt binary search tree.

38
4-Way Search Tree
k gt 35
k lt 10
10 lt k lt 30
30 lt k lt 35
39
Maximum Of Pairs
  • Happens when all internal nodes are m-nodes.
  • Full degree m tree.
  • of nodes 1 m m2 m3 mh-1
  • (mh 1)/(m 1).
  • Each node has m 1 pairs.
  • So, of pairs mh 1.

40
Capacity Of m-Way Search Tree
41
Definition Of B-Tree
  • Definition assumes external nodes (extended m-way
    search tree).
  • B-tree of order m.
  • m-way search tree.
  • Not empty gt root has at least 2 children.
  • Remaining internal nodes (if any) have at least
    ceil(m/2) children.
  • External (or failure) nodes on same level.

42
2-3 And 2-3-4 Trees
  • B-tree of order m.
  • m-way search tree.
  • Not empty gt root has at least 2 children.
  • Remaining internal nodes (if any) have at least
    ceil(m/2) children.
  • External (or failure) nodes on same level.
  • 2-3 tree is B-tree of order 3.
  • 2-3-4 tree is B-tree of order 4.

43
B-Trees Of Order 5 And 2
  • B-tree of order m.
  • m-way search tree.
  • Not empty gt root has at least 2 children.
  • Remaining internal nodes (if any) have at least
    ceil(m/2) children.
  • External (or failure) nodes on same level.
  • B-tree of order 5 is 3-4-5 tree (root may be
    2-node though).
  • B-tree of order 2 is full binary tree.

44
Minimum Of Pairs
  • n of pairs.
  • of external nodes n 1.
  • Height h gt external nodes on level h 1.

1
1
gt 2
2
gt 2ceil(m/2)
3
gt 2ceil(m/2)h-1
h 1
n 1 gt 2ceil(m/2)h-1, h gt 1
45
Minimum Of Pairs
n 1 gt 2ceil(m/2)h-1, h gt 1
  • m 200.

gt 199
2
gt 19,999
3
gt 2 106 1
4
gt 2 108 1
5
h lt log ceil(m/2) (n1)/2 1
46
Choice Of m
  • Worst-case search time.
  • (time to fetch a node time to search node)
    height
  • (a bm c log2m) h
  • where a, b and c are constants.

47
Insert
8
4
15 20
1 3
5 6
30 40
9
16 17
Insertion into a full leaf triggers bottom-up
node splitting pass.
48
Split An Overfull Node
m a0 p1 a1 p2 a2 pm am
  • ai is a pointer to a subtree.
  • pi is a dictionary pair.

ceil(m/2)-1 a0 p1 a1 p2 a2 pceil(m/2)-1
aceil(m/2)-1
m-ceil(m/2) aceil(m/2) pceil(m/2)1
aceil(m/2)1 pm am
  • pceil(m/2) plus pointer to new node is inserted
    in parent.

49
Insert
8
4
15 20
1 3
5 6
30 40
9
16 17
  • Insert a pair with key 2.
  • New pair goes into a 3-node.
Write a Comment
User Comments (0)
About PowerShow.com