Monday, April 16, 2018 Announcements… For Today…

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Quiz3! Midterm! Assignment2! (most) Quiz4! Today’s special: 4 for 1.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
CSE 326: Data Structures AVL Trees
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
AVL Trees ITCS6114 Algorithms and Data Structures.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
SELF-BALANCING SEARCH TREES Chapter 9. Self-Balancing Search Trees  The performance of a binary search tree is proportional to the height of the tree.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
CSIT 402 Data Structures II
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 23 Red Black Tree Chapter 10 of textbook
AA Trees.
File Organization and Processing Week 3
Week 7 - Friday CS221.
Red Black Trees
CSIT 402 Data Structures II
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CO4301 – Advanced Games Development Week 10 Red-Black Trees continued
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red Black Trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Lecture 25 Splay Tree Chapter 10 of textbook
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees Motivations
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Self-Balancing Search Trees
Wednesday, April 18, 2018 Announcements… For Today…
Friday, April 13, 2018 Announcements… For Today…
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Instructor: Lilian de Greef Quarter: Summer 2017
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and Black.
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Algorithms and Data Structures Lecture VIII
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
Self-Balancing Search Trees
CO4301 – Advanced Games Development Week 5 Red-Black Trees
CSC 143 Binary Search Trees.
ITCS6114 Algorithms and Data Structures
Richard Anderson Spring 2016
Red-black tree properties
CSE 373 Data Structures Lecture 8
326 Lecture 9 Henry Kautz Winter Quarter 2002
CS202 - Fundamental Structures of Computer Science II
Self-Balancing Search Trees
Presentation transcript:

Monday, April 16, 2018 Announcements… For Today… Self-Balancing Search Trees Announcements… Lab 09 – Pokemon Reviews due Tonight Lab 10 – Quicksort due Tonight Bonus AVL Lab and Quicksort Reviews due Friday Final Exam Richard's Building April 20-25th. Exam review. Taxes due Tomorrow Direct emails to cs235ta@cs.byu.edu (w/netid) For Today… 11.3 Red-Black Trees

Attendance Quiz #40 Self-Balancing Search Trees

11.3 Red-Black Trees Insertion into a Red-Black Tree Removal from a Red-Black Tree Performance of a Red-Black Tree 11.3, pgs. 643-656

Red-Black Trees Self-Balancing Search Trees Rudolf Bayer developed the Red-Black tree as a special case of his B-tree. Leo Guibas and Robert Sedgewick later refined the concept and introduced the color convention. Each node of the binary tree has an extra bit (red or black) which are used to ensure the tree remains approximately balanced during insertions and deletions. Balance is preserved by always satisfying certain properties, which collectively constrain how unbalanced the tree can become in the worst case. When the tree is modified, the new tree is efficiently rearranged and repainted to restore the coloring properties. The balancing of the tree is not perfect, but it is good enough to allow it to guarantee searching in O(log n) time.

Red-Black Trees A Red-Black tree maintains the following invariants: Self-Balancing Search Trees A Red-Black tree maintains the following invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1

Red-Black Trees Height is determined by counting only black nodes. Self-Balancing Search Trees Height is determined by counting only black nodes. A Red-Black tree is always balanced because the root node’s left and right subtrees must be the same height. By the standards of the AVL tree this tree is out of balance and would be considered a Left-Right tree. However, by the standards of the Red-Black tree, it is balanced, because there are two black nodes (counting the root) in any path from the root to a leaf. 11 2 14 5 8 7 1

Insertion into a Red-Black Tree Self-Balancing Search Trees The algorithm follows the same recursive search process used for all binary search trees to reach the insertion point. When a leaf position is found, the new item is inserted and initially given the color red. If the parent is black, we are done; otherwise there is some rearranging to do. We introduce three situations ("cases") that may occur when a node is inserted; more than one can occur after an insertion. Parent and uncle are red (easy to balance.) Parent is red and has no uncle (1 rotation fixes balance.) Parent is red and has no uncle (1 rotation doesn't fix balance.) Move up tree and fix balance as needed.

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30 35 If a parent is red, and its sibling is also red, they can both be changed to black, and the grandparent to red

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30 35 If a parent is red, and its sibling is also red, they can both be changed to black, and the grandparent to red

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30 35 The root can be changed to black and still maintain invariant 4

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30 35 The root can be changed to black and still maintain invariant 4

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30 35 Balanced tree

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 35 If a parent is red (with no sibling), it can be changed to black, and the grandparent to red

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 35 If a parent is red (with no sibling), it can be changed to black, and the grandparent to red

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 35 There is one black node on the right and none on the left, which violates invariant 4

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 35 Rotate left around the grandparent to correct this

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 30 20 35 Rotate left around the grandparent to correct this

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 30 20 35 Balanced tree

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 25 If a parent is red (with no sibling), it can be changed to black, and the grandparent to red

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 25 If a parent is red (with no sibling), it can be changed to black, and the grandparent to red

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 25 A rotation left does not fix the violation of #4

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 30 20 25 A rotation left does not fix the violation of #4

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 25 Back-up to the beginning (don't perform rotation or change colors)

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 25 Rotate right about the parent so that the red child is on the same side of the parent as the parent is to the grandparent

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 25 30 Rotate right about the parent so that the red child is on the same side of the parent as the parent is to the grandparent

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 25 30 NOW, change colors

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 25 30 NOW, change colors

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 25 30 and rotate left . . .

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 25 20 30 and rotate left . . .

Insertion into a Red-Black Tree Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 25 20 30 Balanced tree

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 4

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 CASE 1 If a parent is red, and its sibling is also red, they can both be changed to black, and the grandparent to red 4

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 CASE 1 If a parent is red, and its sibling is also red, they can both be changed to black, and the grandparent to red 4

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 The problem has now shifted up the tree 4

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 CASE 3 We cannot change 2 to black because its sibling 14 is already black (both siblings have to be red unless there is no sibling) to do the color change 4

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 CASE 3 We need to rotate left around 2 so that the red child is on the same side of the parent as the parent is to the grandparent 4

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 7 14 5 1 8 2 CASE 3 We need to rotate left around 2 so that the red child is on the same side of the parent as the parent is to the grandparent 4

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 7 14 5 1 8 2 CASE 3 Change colors 4

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 7 14 5 1 8 2 CASE 3 Change colors 4

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 7 14 5 1 8 2 CASE 3 Rotate right around 11 to restore the balance 4

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 7 2 11 5 1 14 8 CASE 3 4 Rotate right around 11 to restore the balance

Insertion into a Red-Black Tree Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 7 2 11 5 1 14 8 4 Balanced tree

Red-Black Tree Example Self-Balancing Search Trees Build a Red-Black tree for the words in "The quick brown fox jumps over the lazy dog"

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. The quick

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. The quick brown CASE 3 Rotate so that the child is on the same side of its parent as its parent is to the grandparent

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. The brown quick CASE 3 Change colors

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. The brown quick CASE 3 Change colors

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. The brown quick CASE 3 Rotate left

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox CASE 1 fox's parent and its parent's sibling are both red. Change colors.

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox CASE 1 fox's parent and its parent's sibling are both red. Change colors.

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox CASE 1 We can change brown's color to black and not violate #4

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox CASE 1 We can change brown's color to black and not violate #4

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox jumps

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox jumps CASE 3 Rotate so that red child is on same side of its parent as its parent is to the grandparent

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick jumps fox CASE 3 Change fox's parent and grandparent colors

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick jumps fox CASE 3 Change fox's parent and grandparent colors

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick jumps fox CASE 3 Rotate right about quick

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick CASE 3 Rotate right about quick

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over CASE 1 Change colors of parent, parent's sibling and grandparent

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over CASE 1 Change colors of parent, parent's sibling and grandparent

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over the No changes needed

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over the lazy

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over the lazy CASE 1 Because over and the are both red, change parent, parent's sibling and grandparent colors

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over the lazy CASE 2

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over the lazy CASE 2

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. jumps brown quick The fox over the lazy CASE 2

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. jumps brown quick The fox over the dog lazy

Red-Black Tree Example Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. jumps brown quick The fox over the dog lazy Balanced tree

Red-Black Tree Class Implementation Self-Balancing Search Trees

Red-Black Tree Class Implementation Self-Balancing Search Trees

Red-Black Tree Insertion Algorithm Self-Balancing Search Trees The insertion algorithm can be implemented with a data structure that has a pointer to the parent of each node. The following algorithm detects the need for fix-ups from the grandparent level. Also, whenever a black node with two red children is detected on the way down the tree, it is changed to red and the children are changed to black; any resulting problems can be fixed on the way back up.

Red-Black Tree Insertion Algorithm Self-Balancing Search Trees

The insert Starter Function Self-Balancing Search Trees template<typename Item_Type> bool Red_Black_Tree<Item_Type>::insert(const Item_Type& item) { if (this->root == NULL) RBNode<Item_Type>* new_root = new RBNode<Item_Type>(item); new_root->is_red = false; this->root = new_root; return true; } else // Call the recursive insert function. bool return_value = insert(this->root, item); // Force the root to be black set_red(this->root, false); return return_value;

The insert Starter Function Self-Balancing Search Trees

The Function is_red Self-Balancing Search Trees

Removal from a Red-Black Tree Self-Balancing Search Trees Remove a node only if it is a leaf or has only one child. Otherwise, the node containing the inorder predecessor of the value being removed is removed. If the node removed is red, nothing further is done. If the node removed is black and has a red child, then the red child takes its place and is colored black. If a black leaf is removed, the black height becomes unbalanced. A programming project at the end of the chapter describes other cases.

Performance of a Red-Black Tree Self-Balancing Search Trees The upper limit on the height for a Red-Black tree is 2 log2 n + 2 which is still O(log n). As with AVL trees, the average performance is significantly better than the worst-case performance. Empirical studies show that the average cost of searching a Red-Black tree built from random values is 1.002 log2 n. Red-Black trees and AVL trees both give performance close to the performance of a complete binary tree. Red-Black Trees vs. AVL Trees Insertion and removal are faster for Red-Black trees. Retrieval is faster for AVL trees.

Pros and Cons of AVL Trees Self-Balancing Search Trees Argument for AVL trees: Search is O(log n) since AVL trees are always balanced. Insertion and deletions are also O(log n). The height balancing adds no more than a constant factor to the speed of insertion. Arguments against using AVL trees: Difficult to program & debug; more space for balance factor. Asymptotically faster but rebalancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). May be OK to have O(n) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees).

Pros and Cons of AVL Trees Self-Balancing Search Trees