red-black trees and aa treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · aa trees an aa...

26
Red-Black Trees and AA Trees (Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 1 / 26

Upload: others

Post on 21-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Red-Black Trees and AA Trees

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 1 / 26

Page 2: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Binary Tree Representation Of 2-3-4 Trees

Problems with 2-3-4 trees.

2- and 3-nodes waste space.Overhead of moving pairs and pointers when changing among 2-,3-, and 4-node use.Represented as a binary tree for improved space and timeperformance.

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 2 / 26

Page 3: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Representation of a 4-node

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 3 / 26

Page 4: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Representation of a 3-node

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 4 / 26

Page 5: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Representation of a 2-node

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 5 / 26

Page 6: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

An Example

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 6 / 26

Page 7: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Properties of Binary Tree Representation

Nodes and edges are colored.I The root is black.I Nonroot black node has a black edge from its parent.I Red node has a red edge from its parent.

Can deduce edge color from node color and vice versa.Need to keep either edge or node colors, not both.

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 7 / 26

Page 8: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Red Black Trees

Colored Nodes DefinitionBinary 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 blacknodesThe height of a red black tree that has n (internal) nodes isbetween log2(n + 1) and 2log2(n + 1).C++ STL implementationjava.util.TreeMap => red black tree

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 8 / 26

Page 9: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Red Black Trees

Colored Edges DefinitionBinary 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 blackpointers.

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 9 / 26

Page 10: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Top-Down Insert

Mimic 2-3-4 top-down algorithm.Split 4-nodes on the way down.

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 10 / 26

Page 11: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Root Is a 4-node

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 11 / 26

Page 12: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

4-node Left Child of 3-node

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 12 / 26

Page 13: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

4-node Left Child of 3-node

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 13 / 26

Page 14: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

4-node Middle Child of 3-node

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 14 / 26

Page 15: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

4-node Middle Child of 3-node

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 15 / 26

Page 16: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

4-node Right Child Of 3-node

One orientation of 3-node requires color flip.Other orientation requires RR rotation.

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 16 / 26

Page 17: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

AA Trees

An AA tree satisfies the properties of Red-Black trees plus onemore:

I Every node is colored either red or blackI The root is blackI If a node is red, both of its children are black.I Every path from a node to a null reference has the same number of

black nodesI Left children may NOT be red

Invented by A. Andersson in 1993.

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 17 / 26

Page 18: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Advantage of AA Trees

AA trees simplify the algorithmsI It eliminates half the restructuring casesI It simplifies deletion by removing an annoying case

F if an internal node has only one child, that child must be a red rightchild

F We can always replace a node with the smallest child in the rightsubtree (it will either be a leaf or have a red child)

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 18 / 26

Page 19: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Representing the Balance information

In each node we store a level. The level is defined by these rulesI If a node is a leaf, its level is 1I If a node is red, its level is the level of its parentI If a node is black, its level is one less than the level of its parent

The level is the number of left links to a null reference.

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 19 / 26

Page 20: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Links in an AA tree

A horizontal link is a connection between a node and a child withequal levels

I Horizontal links are right referencesI There cannot be two consecutive horizontal linksI Nodes at level 2 or higher must have two childrenI If a node has no right horizontal link, its two children are at the

same level

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 20 / 26

Page 21: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Example of an AA Tree

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 21 / 26

Page 22: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

Insertion

A new item is always inserted at the bottom levelIn the previous example, inserting 2 will create a horizontal leftlinkIn the previous example, inserting 45 generates consecutive rightlinksAfter inserting at the bottom level, we may need to performrotations to restore the horizontal link properties

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 22 / 26

Page 23: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

skew - remove left horizontal links

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 23 / 26

Page 24: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

split - remove consecutive horizontal links

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 24 / 26

Page 25: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

skew/split

A skew removes a left horizontal linkA skew might create consecutive right horizontal linksWe should first process a skew and then a split, if necessaryAfter a split, the middle node increases a level, which may create aproblem for the original parent

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 25 / 26

Page 26: Red-Black Trees and AA Treesccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-4g.pdf · AA Trees An AA tree satisfies the properties of Red-Black trees plus one more: I Every node is colored

An Example

(Red-Black Trees and AA Trees) Data Structures and Programming Spring 2017 26 / 26