Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Red-Black Trees: Data Structure and Algorithms, Study notes of Data Representation and Algorithm Design

An introduction to Red-Black Trees, a type of self-balancing binary search tree. Topics covered include the definitions and properties of Red-Black Trees, tree rotations, and insertion and deletion algorithms. The document also includes examples and theorems to illustrate the concepts.

What you will learn

  • What is the goal of top-down deletion in a Red-Black Tree?
  • What is a Red-Black Tree?
  • What are the properties of a Red-Black Tree?
  • How does bottom-up deletion handle violations of Red-Black Tree properties?
  • How does bottom-up insertion work in a Red-Black Tree?

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

claire67
claire67 🇬🇧

4.6

(5)

265 documents

1 / 40

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Red-Black Trees
Based on materials by Dennis Frey, Yun Peng,
Jian Chen, and Daniel Hood
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Red-Black Trees: Data Structure and Algorithms and more Study notes Data Representation and Algorithm Design in PDF only on Docsity!

Red-Black Trees

Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Advanced Data Structures

n CS 206 covered basic data structures q Lists, binary search trees, heaps, hash tables n CS 246 will introduce you to some advanced data structures and their use in applications q Red-Black Trees: a type of self-balancing BST q KD-Trees: a type of space partitioning tree q Graphs: represents a set of entities and relations n Over the next few weeks, we will discuss these data structures, starting today with Red-Black Trees

Review of Tree Rotations: Zig-Zig

(Node and Parent are Same Side)

Rotate P around G, then X around P

Review of Tree Rotations: Zig-Zag

(Node and Parent are Different Sides)

Rotate X around P, then X around G

Red-Black Trees

n Definition: A red-black tree is a binary search tree in which: q Every node is colored either Red or Black. q Each NULL pointer is considered to be a Black “node”. q If a node is Red, then both of its children are Black. q Every path from a node to a NULL contains the same number of Black nodes. q By convention, the root is Black n Definition: The black-height of a node X in a red-black tree is the number of Black nodes on any path to a NULL, not counting X.

A Red-Black Tree with NULLs shown Black-Height of the tree (the root) = 3 Black-Height of node “X” = 2 X

Black Height of the tree? Black Height of X? X

Theorem 1 – Any red-black tree with root x , has n ≥ 2 bh(x)

- 1 nodes, where bh(x) is the black height of node x. Proof: by induction on height of x.

Theorem 3 – In a red-black tree, no path from any node, X, to a NULL is more than twice as long as any other path from X to any other NULL. Proof: By definition, every path from a node to any NULL contains the same number of Black nodes. By Theorem 2, a least ½ the nodes on any such path are Black. Therefore, there can no more than twice as many nodes on any path from X to a NULL as on any other path. Therefore the length of every path is no more than twice as long as any other path.

Theorem 4 – A red-black tree with n nodes has height h ≤ 2 lg( n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 2, bh(x) ≥ h/ From Theorem 1, n ≥ 2 bh(x)

  • 1 Therefore n ≥ 2 h/
  • 1 n + 1 ≥ 2 h/ lg(n + 1) ≥ h/ 2lg(n + 1) ≥ h

Bottom –Up Insertion

n Insert node as usual in BST n Color the node Red n What Red-Black property may be violated? q Every node is Red or Black? q NULLs are Black? q If node is Red, both children must be Black? q Every path from node to descendant NULL must contain the same number of Blacks?

Bottom Up Insertion

n Insert node; Color it Red; X is pointer to it n Cases 0: X is the root -- color it Black 1: Both parent and uncle are Red -- color parent and uncle Black, color grandparent Red. Point X to grandparent and check new situation. 2 (zig-zag): Parent is Red, but uncle is Black. X and its parent are opposite type children -- color grandparent Red, color X Black, rotate left(right) on parent, rotate right(left) on grandparent 3 (zig-zig): Parent is Red, but uncle is Black. X and its parent are both left (right) children -- color parent Black, color grandparent Red, rotate right(left) on grandparent

X

P

G

U

S

X

P

G

S

U

Case 2 – Zig-Zag Double Rotate X around P; X around G Recolor G and X

X

P

G U S P X G S

U

Case 3 – Zig-Zig Single Rotate P around G Recolor P and G