SlideShare una empresa de Scribd logo
1 de 54
CS 6213 – Advanced Data Structures
 Instructor

Prof. Amrinder Arora
amrinder@gwu.edu
Please copy TA on emails
Please feel free to call as well
 TA

Iswarya Parupudi
iswarya2291@gwmail.gwu.edu

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

2
Record / Struct

Basics

Arrays / Linked
Lists / Stacks /
Queues
Graphs / Trees
/ BSTs

Trie, B-Tree
CS 6213
Advanced

Splay Trees

Union Find

Generics
Applications
Multi Threading

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

3
 Objectives
 Deletions, Insertions, Searches, all in log n time

 Credits:
 Prof. Roger Crawfis (Ohio State)
 Sarah Buchanan (UCF)

 Rafee Memon (CMU)

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

4
 Requiring balancing at the root is

not enough.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

5
 Impose some properties (different properties based on different

implementations)
 Perform rotations (localized operations) that maintain BST

property and adjust the height
 Left Rotation, Right Rotation

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

6
Left-Rotate(T, x)

x



Right-Rotate(T, y)

y



ADS - AVL, Red Black Trees

y





x





CS6213 - Arora - L3

7
 Rotations are the basic tree-restructuring operation for almost all

balanced search trees.
 Rotation takes a BST and a node,
 Changes pointers to change the local structure, and
 Won’t violate the binary-search-tree property.

 Left rotation and right rotation are inverses.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

8
Left-Rotate (T, x)
1.
y  right[x] // Set y.
2.
right[x]  left[y] //Turn y’s left subtree into x’s right subtree.
3.
if left[y]  nil[T ]
4.
then p[left[y]]  x
5.
p[y]  p[x] // Link x’s parent to y.
6.
if p[x] = nil[T ]
7.
then root[T ]  y
Left-Rotate(T, x)
y
x
8.
else if x = left[p[x]]


Right-Rotate(T, y) x
y
9.
then left[p[x]]  y




10.
else right[p[x]]  y
11. left[y]  x // Put x on y’s left.
12. p[x]  y

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

9
 The pseudo-code for Left-Rotate assumes that
 right[x]  nil[T ], and
 root’s parent is nil[T ].
 Left Rotation on x, makes x the left child of y, and

the left subtree of y into the right subtree of x.
 Pseudocode for Right-Rotate is symmetric:
exchange left and right everywhere.
 Time: O(1) for both Left-Rotate and Right-Rotate,
since a constant number of pointers are modified.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

10
 Invented by Adelson-Velskii and Landis.
 A binary search tree with a balance condition.
 Easy to maintain.
 Ensures the depth is O(log n)

 Balance condition – for every node in the tree, the height of the

left and right subtrees can differ by at most 1.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

11
 Definition: A tree rooted at node v with L and R as left and right

subtrees is an AVL tree if:
 | height(L) - height(R)| ≤ 1
 L is an AVL tree; and
 R is an AVL tree.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

12
 Are these AVL trees?

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

13
5

7
8

2
1

4

7

8

2
1

3

4

3

5

 We note that both of them are still binary search

trees, although not both are AVL trees
ADS - AVL, Red Black Trees

CS6213 - Arora - L3

14
 Mathematical definition: A tree rooted at node n is an AVL tree if:
 |height(L) - height(R)| ≤ 1; and
 L is an AVL tree; and
 R is an AVL tree.

 Using this definition, how can we

check if a tree is a valid AVL tree?
 Definition is recursive,

so how about recursively?
 How about bottom up traversal?

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

15
 public boolean isAVL:

For a tree rooted at node n:
 Base case?
 Could check for leaf node, but for elegance…

 n is null → for simplicity, return valid

 Constraint?
 Check that heights of sub-trees are within 1 of each other (assume we

have a getHeight function)
 Recursive case?
 Check that left and right sub-trees are AVL trees

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

16
public boolean isAVL(Node n)
{
if (n == null) return true; // base case
int heightL = getHeight(n.leftChild);
int heightR = getHeight(n.rightChild);
int heightDiff = Math.abs(heightL – heightR);
return (isAVL(n.leftChild)
&& isAVL(n.rightChild)
&& heightDiff <= 1);
}

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

17
 When we do an Insertion
 Update all the balancing information for the nodes on the path back

to the root.
 As we are updating the balance information on the path up to the root, we

may find a node whose new balance violates the AVL condition.

 What if the insertion violated the AVL tree property?

 We do a simple modification to the tree known as a rotation.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

18
 4 cases, but two are symmetrical with the other two
 1) Left outer subtree ↔ 4) Right outer subtree
 2) Left inner subtree ↔ 3) Right inner subtree

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

19
 Let us call the node that must be rebalanced α
 Since any node has at most 2 children, and a

height imbalance requires that α’s 2 subtrees’
height differ by 2, there are 4 violation cases:
1)
An insertion into the left subtree of the left
child of α.

2)

An insertion into the left subtree of the right
child of α.

4)

An insertion into the right subtree of the right
child of α.

α

An insertion into the right subtree of the left
child of α.

3)

α

ADS - AVL, Red Black Trees

α

α
CS6213 - Arora - L3

20


Outside cases (left-left or right-right), fixed by a single rotation:
1) An insertion into the left subtree of the left child of α.
4) An insertion into the right subtree of the right child of α.

6

6

α
8

2

2

7

α
1

5

7

8
9

4
3
ADS - AVL, Red Black Trees

CS6213 - Arora - L3

21


Inside cases (right-left or left-right), fixed by a double rotation:
2) An insertion into the right subtree of the left child of α.
3) An insertion into the left subtree of the right child of α.

7

6

α
9

2

2

7

α
1

6

8

9
8

4
5
ADS - AVL, Red Black Trees

CS6213 - Arora - L3

22
 We want X up a level and Z down a level.
 We want k2 as the new root.
 Pretend the tree is flexible, grab hold of k2, letting gravity take hold.
 k3 > k2 (by BST property), so k3 becomes k2’s right subtree.
 X and Z can remain attached to the same point,
 and Y becomes k3’s subtree to maintain BST property.

k

k

3

2
k

k
Z

2
k

1

3

X

1

X

k

Y

Y

Z

X
ADS - AVL, Red Black Trees

CS6213 - Arora - L3

23
 Symmetric case

k

k

2

1

k

X

k

1

k

3

2
k

Y

3

Z

Y

Z

Z

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

24
 Place k2 as the new root.
 This forces k1 to be k2’s left child and k3 to be its right child.
 Then the 4 subtrees A,B,C,D fall according to BST rules.

k

k

3

k

3

2

k

k

Z

1

1

k

X

2

Y

ADS - AVL, Red Black Trees

k

k

1

D

3

k

A

A

2

B

B

C

D

C

CS6213 - Arora - L3

25
 A Double Rotation is like 2 single rotations.
 Example: Left-right double rotation.
k

Single Left
Rotation at K1

3

k

D

2

k

k

2

A

3

k

D

1

k

1

B

C

A

B

C

k
2

k
1

ADS - AVL, Red Black Trees

k
3

CS6213 - Arora - L3

A

B

C

26

D
 In an insert there is at most one node that needs to be

rebalanced.
 But in a delete there may be multiple nodes to be rebalanced.
 Technically only one rebalance that happens at a node, but once that

happens it may affect the ancestral nodes.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

27
 If the node is a leaf, remove it.
 Retrace back up the tree starting with the parent of deleted node to

the root, adjusting the balance factor as needed.
 If it’s not a leaf, replace with the largest in its left subtree

(inorder predecessor) and remove that node.
 You could also replace with the smallest in its right subtree (inorder

successor)
 If the predecessor was not a leaf, you may need to adjust the
pointers.
 After deletion, retrace the path back up the tree, starting with

the parent of the replacement, to the root, adjusting the balance
factor as needed.
ADS - AVL, Red Black Trees

CS6213 - Arora - L3

28
32

16

8

48

24

40
28

36

56
44

52

60
58

ADS - AVL, Red Black Trees

62

CS6213 - Arora - L3

29
32

16

48

24

40
28

36

56
44

52

60

58

ADS - AVL, Red Black Trees

62

CS6213 - Arora - L3

30
32

24

16

48

28

40
36

56
44

52

60
58

ADS - AVL, Red Black Trees

62

CS6213 - Arora - L3

31
48

32

56

24

16

ADS - AVL, Red Black Trees

52

40
28

36

44

60
58

62

CS6213 - Arora - L3

32
 Actual Implementation for Binary Search Trees

 O(log n) average height – Why is that? (We didn’t prove it for

sure.)
 If we initially have an AVL tree:
 Insertion requires at most 2 rotations
 Deletion requires at most O(log n) rotations

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

33
 A variation of binary search trees to ensure that the tree is

somewhat balanced.
 Height is O(lg n), where n is the number of nodes.

 Operations take O(lg n) time in the worst case.
 Red-Black Tree Properties:
 Every node is either red or black.
 The root is black.
 Every leaf (nil) is black.
 If a node is red, then both its children are black.
 For each node, all paths from the node to descendant leaves contain

the same number of black nodes.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

34
 Binary search tree + 1 bit per node: the attribute color, which is

either red or black.
 All other attributes of BSTs are inherited:

 key, left, right, and p.

 All empty trees (leaves) are colored black.
 We use a single sentinel, nil, for all the leaves of red-black tree T, with

color[nil] = black.
 The root’s parent is also nil[T]

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

35
26
Remember: every internal
node has two children, even
though nil leaves are not
usually shown.
41

17

30

47

38

50

nil[T]
ADS - AVL, Red Black Trees

CS6213 - Arora - L3

36
 Height of a node:
 h(x) = number of edges in a longest path to a leaf.

 Black-height of a node x, bh(x):
 bh(x) = number of black nodes (including nil[T ])

on the path from x to leaf, not counting x.
 Black-height of a red-black tree is the black-height of its root.
 By Property 5, black height is well defined.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

37
h=4
26 bh=2

 Height of a node:

h(x) = # of edges in a
longest path to a
leaf.
 Black-height of a node bh(x) = #

17

h=1
bh=1

h=3
41 bh=2

of black nodes on path from x to
leaf, not counting x.
 How are they related?
 bh(x)

h=2 30
bh=1

≤ h(x) ≤ 2 bh(x)

h=1
bh=1
38

ADS - AVL, Red Black Trees

nil[T]

h=2
47 bh=1
h=1 50
bh=1

CS6213 - Arora - L3

38
Consider a node x in an RB tree: The longest descending path from
x to a leaf has length h(x), which is at most twice the length of the
shortest descending path from x to a leaf.
Proof:

# black nodes on any path from x = bh(x) (prop
5)
 # nodes on shortest path from x, s(x). (prop 1)
But, there are no consecutive red (prop 4),
and we end with black (prop 3), so h(x) ≤ 2
bh(x).
Thus, h(x) ≤ 2 s(x). QED
ADS - AVL, Red Black Trees

CS6213 - Arora - L3

39
 Lemma 13.1: A red-black tree with n internal nodes has height at

most
2 lg(n+1).

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

40
1.
2.
3.
4.
5.

Every node is either red or black.
The root is black.
Every leaf (nil) is black.
If a node is red, then both its children are black.
For each node, all paths from the node to
descendant leaves contain the same number of
black nodes.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

41
 Insertion must preserve all red-black properties.
 Should an inserted node be colored Red? Black?
 Basic steps:
 Use Tree-Insert from BST (slightly modified) to insert a node x into T.
 Procedure RB-Insert(x).

 Color the node x red.
 Fix the modified tree by re-coloring nodes and performing rotation to

preserve RB tree property.
 Procedure RB-Insert-Fixup.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

42
 Problem: we may have one pair of consecutive reds where we did

the insertion.
 Solution: rotate it up the tree and away…
 Three cases have to be handled…

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

43
p[p[z]]

new z
C

C

p[z]

y
A

D
z





A


D





B





z is a right child here.
Similar steps if z is a left child.



B




 p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red.
 Make p[z] and y black
 Make p[p[z]] red

 now z and p[z] are not both red.

 restores property 5.

 The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels).
ADS - AVL, Red Black Trees

CS6213 - Arora - L3

44
B

C

p[z]

z


A


A

 y

B



C







 Make p[z] black and p[p[z]] red.
 Then right rotate on p[p[z]]. Ensures property 4 is maintained.
 No longer have 2 reds in a row.
 p[z] is now black
ADS - AVL, Red Black Trees

 no more iterations.
CS6213 - Arora - L3

45
C

C

p[z]

p[z]
 y

A

 y

B

z


z

B






 Left rotate around p[z], p[z] and z switch roles



A


 now z is a left child, and both

z and p[z] are red.
 This now becomes case 2.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

46
 Symmetric to cases 2 and 3 in that parent of z is right child.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

47
 O(lg n) time to get through RB-Insert up to the call of RB-Insert-

Fixup.
 Within RB-Insert-Fixup:
 Each iteration takes O(1) time.
 Each iteration but the last moves z up 2 levels.

 O(lg n) levels

 O(lg n) time.

 Thus, insertion in a red-black tree takes O(lg n) time.
 Note: there are at most 2 rotations overall.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

48
 Deletion, like insertion, should preserve all the RB properties.
 First do regular BST deletion
 Find the node
 If not a leaf, find the predecessor
 If predecessor is a node with a single child, then adjust the pointers

 Now, we need to fix any violations of RB properties that may

result.
 The properties that may be violated depends on the color of the

deleted node.
 Red – We are lucky.
 Black? We need to consider 3 cases

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

49
 The parent is red, and the children of sibling node are black
Recolor parent to black and sibling to red

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

50
 The parent is black, and the children of sibling node are black

 Unlike Case 1, we cannot finish in one move, but the problem can

be shifted “upwards”. B is now “double black”.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

51
 The parent is red, and the children of sibling node are not both black
 We can do a rotation

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

52
 Deletion of elements in array A

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

53
 Review ArrayList.java source code of Java.

ADS - AVL, Red Black Trees

CS6213 - Arora - L3

54

Más contenido relacionado

La actualidad más candente (20)

Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Red black 1
Red black 1Red black 1
Red black 1
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Red black trees
Red black treesRed black trees
Red black trees
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Tree
TreeTree
Tree
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Trees
TreesTrees
Trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Red black trees presentation
Red black trees presentationRed black trees presentation
Red black trees presentation
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Avl tree
Avl treeAvl tree
Avl tree
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 

Destacado (20)

2 Edición de celdas
2 Edición de celdas2 Edición de celdas
2 Edición de celdas
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Avl trees
Avl treesAvl trees
Avl trees
 
A04630104
A04630104A04630104
A04630104
 
Experimental Investigation on Use of Methyl Ester Kusum Oil and Its Blends In...
Experimental Investigation on Use of Methyl Ester Kusum Oil and Its Blends In...Experimental Investigation on Use of Methyl Ester Kusum Oil and Its Blends In...
Experimental Investigation on Use of Methyl Ester Kusum Oil and Its Blends In...
 
Advance data structure
Advance data structureAdvance data structure
Advance data structure
 
Red black trees
Red black treesRed black trees
Red black trees
 
Chap05alg
Chap05algChap05alg
Chap05alg
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
B tree short
B tree shortB tree short
B tree short
 
Mergesort
MergesortMergesort
Mergesort
 
Data Structures : AVL Trees
Data Structures : AVL TreesData Structures : AVL Trees
Data Structures : AVL Trees
 
Digital Search Tree
Digital Search TreeDigital Search Tree
Digital Search Tree
 
Application of tries
Application of triesApplication of tries
Application of tries
 
Treatments of Mental Illnesses
Treatments of Mental IllnessesTreatments of Mental Illnesses
Treatments of Mental Illnesses
 
Avl trees
Avl treesAvl trees
Avl trees
 
Scan scheduling 50 1
Scan scheduling 50 1Scan scheduling 50 1
Scan scheduling 50 1
 
Trie Data Structure
Trie Data StructureTrie Data Structure
Trie Data Structure
 
C look scheduling 51 1
C look scheduling 51 1C look scheduling 51 1
C look scheduling 51 1
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 

Similar a Binary Search Trees - AVL and Red Black

AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptxTrad5
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementationUmang Gupta
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsAmrinder Arora
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & OperationsEditor IJCTER
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresAmrinder Arora
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
NeuroSoL (International Symposium on Biomedical Imaging'17)
NeuroSoL (International Symposium on Biomedical Imaging'17)NeuroSoL (International Symposium on Biomedical Imaging'17)
NeuroSoL (International Symposium on Biomedical Imaging'17)TamalBatabyal
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresAmrinder Arora
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avlSSE_AndyLi
 
Algo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.pptAlgo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.pptHebaSamy22
 

Similar a Binary Search Trees - AVL and Red Black (20)

AVL_Trees.ppt
AVL_Trees.pptAVL_Trees.ppt
AVL_Trees.ppt
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their Representations
 
Av ltrees
Av ltreesAv ltrees
Av ltrees
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
 
Red black trees
Red black treesRed black trees
Red black trees
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data Structures
 
4. avl
4. avl4. avl
4. avl
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
Avl trees
Avl treesAvl trees
Avl trees
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Avltrees
AvltreesAvltrees
Avltrees
 
NeuroSoL (International Symposium on Biomedical Imaging'17)
NeuroSoL (International Symposium on Biomedical Imaging'17)NeuroSoL (International Symposium on Biomedical Imaging'17)
NeuroSoL (International Symposium on Biomedical Imaging'17)
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data Structures
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
 
Lect 22 Zaheer Abbas
Lect 22 Zaheer AbbasLect 22 Zaheer Abbas
Lect 22 Zaheer Abbas
 
Algebra
AlgebraAlgebra
Algebra
 
Algo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.pptAlgo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.ppt
 

Más de Amrinder Arora

Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Amrinder Arora
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchAmrinder Arora
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Amrinder Arora
 
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaArima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaAmrinder Arora
 
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Amrinder Arora
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Amrinder Arora
 
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Amrinder Arora
 
Online algorithms in Machine Learning
Online algorithms in Machine LearningOnline algorithms in Machine Learning
Online algorithms in Machine LearningAmrinder Arora
 
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisEuclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisAmrinder Arora
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part IIAmrinder Arora
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1Amrinder Arora
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsAmrinder Arora
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 

Más de Amrinder Arora (20)

Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
 
NP-Completeness - II
NP-Completeness - IINP-Completeness - II
NP-Completeness - II
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
 
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaArima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
 
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
 
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
 
Online algorithms in Machine Learning
Online algorithms in Machine LearningOnline algorithms in Machine Learning
Online algorithms in Machine Learning
 
NP completeness
NP completenessNP completeness
NP completeness
 
Algorithmic Puzzles
Algorithmic PuzzlesAlgorithmic Puzzles
Algorithmic Puzzles
 
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisEuclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 

Último

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 

Último (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

Binary Search Trees - AVL and Red Black

  • 1. CS 6213 – Advanced Data Structures
  • 2.  Instructor Prof. Amrinder Arora amrinder@gwu.edu Please copy TA on emails Please feel free to call as well  TA Iswarya Parupudi iswarya2291@gwmail.gwu.edu ADS - AVL, Red Black Trees CS6213 - Arora - L3 2
  • 3. Record / Struct Basics Arrays / Linked Lists / Stacks / Queues Graphs / Trees / BSTs Trie, B-Tree CS 6213 Advanced Splay Trees Union Find Generics Applications Multi Threading ADS - AVL, Red Black Trees CS6213 - Arora - L3 3
  • 4.  Objectives  Deletions, Insertions, Searches, all in log n time  Credits:  Prof. Roger Crawfis (Ohio State)  Sarah Buchanan (UCF)  Rafee Memon (CMU) ADS - AVL, Red Black Trees CS6213 - Arora - L3 4
  • 5.  Requiring balancing at the root is not enough. ADS - AVL, Red Black Trees CS6213 - Arora - L3 5
  • 6.  Impose some properties (different properties based on different implementations)  Perform rotations (localized operations) that maintain BST property and adjust the height  Left Rotation, Right Rotation ADS - AVL, Red Black Trees CS6213 - Arora - L3 6
  • 7. Left-Rotate(T, x) x  Right-Rotate(T, y) y  ADS - AVL, Red Black Trees y   x   CS6213 - Arora - L3 7
  • 8.  Rotations are the basic tree-restructuring operation for almost all balanced search trees.  Rotation takes a BST and a node,  Changes pointers to change the local structure, and  Won’t violate the binary-search-tree property.  Left rotation and right rotation are inverses. ADS - AVL, Red Black Trees CS6213 - Arora - L3 8
  • 9. Left-Rotate (T, x) 1. y  right[x] // Set y. 2. right[x]  left[y] //Turn y’s left subtree into x’s right subtree. 3. if left[y]  nil[T ] 4. then p[left[y]]  x 5. p[y]  p[x] // Link x’s parent to y. 6. if p[x] = nil[T ] 7. then root[T ]  y Left-Rotate(T, x) y x 8. else if x = left[p[x]]   Right-Rotate(T, y) x y 9. then left[p[x]]  y     10. else right[p[x]]  y 11. left[y]  x // Put x on y’s left. 12. p[x]  y ADS - AVL, Red Black Trees CS6213 - Arora - L3 9
  • 10.  The pseudo-code for Left-Rotate assumes that  right[x]  nil[T ], and  root’s parent is nil[T ].  Left Rotation on x, makes x the left child of y, and the left subtree of y into the right subtree of x.  Pseudocode for Right-Rotate is symmetric: exchange left and right everywhere.  Time: O(1) for both Left-Rotate and Right-Rotate, since a constant number of pointers are modified. ADS - AVL, Red Black Trees CS6213 - Arora - L3 10
  • 11.  Invented by Adelson-Velskii and Landis.  A binary search tree with a balance condition.  Easy to maintain.  Ensures the depth is O(log n)  Balance condition – for every node in the tree, the height of the left and right subtrees can differ by at most 1. ADS - AVL, Red Black Trees CS6213 - Arora - L3 11
  • 12.  Definition: A tree rooted at node v with L and R as left and right subtrees is an AVL tree if:  | height(L) - height(R)| ≤ 1  L is an AVL tree; and  R is an AVL tree. ADS - AVL, Red Black Trees CS6213 - Arora - L3 12
  • 13.  Are these AVL trees? ADS - AVL, Red Black Trees CS6213 - Arora - L3 13
  • 14. 5 7 8 2 1 4 7 8 2 1 3 4 3 5  We note that both of them are still binary search trees, although not both are AVL trees ADS - AVL, Red Black Trees CS6213 - Arora - L3 14
  • 15.  Mathematical definition: A tree rooted at node n is an AVL tree if:  |height(L) - height(R)| ≤ 1; and  L is an AVL tree; and  R is an AVL tree.  Using this definition, how can we check if a tree is a valid AVL tree?  Definition is recursive, so how about recursively?  How about bottom up traversal? ADS - AVL, Red Black Trees CS6213 - Arora - L3 15
  • 16.  public boolean isAVL: For a tree rooted at node n:  Base case?  Could check for leaf node, but for elegance…  n is null → for simplicity, return valid  Constraint?  Check that heights of sub-trees are within 1 of each other (assume we have a getHeight function)  Recursive case?  Check that left and right sub-trees are AVL trees ADS - AVL, Red Black Trees CS6213 - Arora - L3 16
  • 17. public boolean isAVL(Node n) { if (n == null) return true; // base case int heightL = getHeight(n.leftChild); int heightR = getHeight(n.rightChild); int heightDiff = Math.abs(heightL – heightR); return (isAVL(n.leftChild) && isAVL(n.rightChild) && heightDiff <= 1); } ADS - AVL, Red Black Trees CS6213 - Arora - L3 17
  • 18.  When we do an Insertion  Update all the balancing information for the nodes on the path back to the root.  As we are updating the balance information on the path up to the root, we may find a node whose new balance violates the AVL condition.  What if the insertion violated the AVL tree property?  We do a simple modification to the tree known as a rotation. ADS - AVL, Red Black Trees CS6213 - Arora - L3 18
  • 19.  4 cases, but two are symmetrical with the other two  1) Left outer subtree ↔ 4) Right outer subtree  2) Left inner subtree ↔ 3) Right inner subtree ADS - AVL, Red Black Trees CS6213 - Arora - L3 19
  • 20.  Let us call the node that must be rebalanced α  Since any node has at most 2 children, and a height imbalance requires that α’s 2 subtrees’ height differ by 2, there are 4 violation cases: 1) An insertion into the left subtree of the left child of α. 2) An insertion into the left subtree of the right child of α. 4) An insertion into the right subtree of the right child of α. α An insertion into the right subtree of the left child of α. 3) α ADS - AVL, Red Black Trees α α CS6213 - Arora - L3 20
  • 21.  Outside cases (left-left or right-right), fixed by a single rotation: 1) An insertion into the left subtree of the left child of α. 4) An insertion into the right subtree of the right child of α. 6 6 α 8 2 2 7 α 1 5 7 8 9 4 3 ADS - AVL, Red Black Trees CS6213 - Arora - L3 21
  • 22.  Inside cases (right-left or left-right), fixed by a double rotation: 2) An insertion into the right subtree of the left child of α. 3) An insertion into the left subtree of the right child of α. 7 6 α 9 2 2 7 α 1 6 8 9 8 4 5 ADS - AVL, Red Black Trees CS6213 - Arora - L3 22
  • 23.  We want X up a level and Z down a level.  We want k2 as the new root.  Pretend the tree is flexible, grab hold of k2, letting gravity take hold.  k3 > k2 (by BST property), so k3 becomes k2’s right subtree.  X and Z can remain attached to the same point,  and Y becomes k3’s subtree to maintain BST property. k k 3 2 k k Z 2 k 1 3 X 1 X k Y Y Z X ADS - AVL, Red Black Trees CS6213 - Arora - L3 23
  • 24.  Symmetric case k k 2 1 k X k 1 k 3 2 k Y 3 Z Y Z Z ADS - AVL, Red Black Trees CS6213 - Arora - L3 24
  • 25.  Place k2 as the new root.  This forces k1 to be k2’s left child and k3 to be its right child.  Then the 4 subtrees A,B,C,D fall according to BST rules. k k 3 k 3 2 k k Z 1 1 k X 2 Y ADS - AVL, Red Black Trees k k 1 D 3 k A A 2 B B C D C CS6213 - Arora - L3 25
  • 26.  A Double Rotation is like 2 single rotations.  Example: Left-right double rotation. k Single Left Rotation at K1 3 k D 2 k k 2 A 3 k D 1 k 1 B C A B C k 2 k 1 ADS - AVL, Red Black Trees k 3 CS6213 - Arora - L3 A B C 26 D
  • 27.  In an insert there is at most one node that needs to be rebalanced.  But in a delete there may be multiple nodes to be rebalanced.  Technically only one rebalance that happens at a node, but once that happens it may affect the ancestral nodes. ADS - AVL, Red Black Trees CS6213 - Arora - L3 27
  • 28.  If the node is a leaf, remove it.  Retrace back up the tree starting with the parent of deleted node to the root, adjusting the balance factor as needed.  If it’s not a leaf, replace with the largest in its left subtree (inorder predecessor) and remove that node.  You could also replace with the smallest in its right subtree (inorder successor)  If the predecessor was not a leaf, you may need to adjust the pointers.  After deletion, retrace the path back up the tree, starting with the parent of the replacement, to the root, adjusting the balance factor as needed. ADS - AVL, Red Black Trees CS6213 - Arora - L3 28
  • 29. 32 16 8 48 24 40 28 36 56 44 52 60 58 ADS - AVL, Red Black Trees 62 CS6213 - Arora - L3 29
  • 30. 32 16 48 24 40 28 36 56 44 52 60 58 ADS - AVL, Red Black Trees 62 CS6213 - Arora - L3 30
  • 31. 32 24 16 48 28 40 36 56 44 52 60 58 ADS - AVL, Red Black Trees 62 CS6213 - Arora - L3 31
  • 32. 48 32 56 24 16 ADS - AVL, Red Black Trees 52 40 28 36 44 60 58 62 CS6213 - Arora - L3 32
  • 33.  Actual Implementation for Binary Search Trees  O(log n) average height – Why is that? (We didn’t prove it for sure.)  If we initially have an AVL tree:  Insertion requires at most 2 rotations  Deletion requires at most O(log n) rotations ADS - AVL, Red Black Trees CS6213 - Arora - L3 33
  • 34.  A variation of binary search trees to ensure that the tree is somewhat balanced.  Height is O(lg n), where n is the number of nodes.  Operations take O(lg n) time in the worst case.  Red-Black Tree Properties:  Every node is either red or black.  The root is black.  Every leaf (nil) is black.  If a node is red, then both its children are black.  For each node, all paths from the node to descendant leaves contain the same number of black nodes. ADS - AVL, Red Black Trees CS6213 - Arora - L3 34
  • 35.  Binary search tree + 1 bit per node: the attribute color, which is either red or black.  All other attributes of BSTs are inherited:  key, left, right, and p.  All empty trees (leaves) are colored black.  We use a single sentinel, nil, for all the leaves of red-black tree T, with color[nil] = black.  The root’s parent is also nil[T] ADS - AVL, Red Black Trees CS6213 - Arora - L3 35
  • 36. 26 Remember: every internal node has two children, even though nil leaves are not usually shown. 41 17 30 47 38 50 nil[T] ADS - AVL, Red Black Trees CS6213 - Arora - L3 36
  • 37.  Height of a node:  h(x) = number of edges in a longest path to a leaf.  Black-height of a node x, bh(x):  bh(x) = number of black nodes (including nil[T ]) on the path from x to leaf, not counting x.  Black-height of a red-black tree is the black-height of its root.  By Property 5, black height is well defined. ADS - AVL, Red Black Trees CS6213 - Arora - L3 37
  • 38. h=4 26 bh=2  Height of a node: h(x) = # of edges in a longest path to a leaf.  Black-height of a node bh(x) = # 17 h=1 bh=1 h=3 41 bh=2 of black nodes on path from x to leaf, not counting x.  How are they related?  bh(x) h=2 30 bh=1 ≤ h(x) ≤ 2 bh(x) h=1 bh=1 38 ADS - AVL, Red Black Trees nil[T] h=2 47 bh=1 h=1 50 bh=1 CS6213 - Arora - L3 38
  • 39. Consider a node x in an RB tree: The longest descending path from x to a leaf has length h(x), which is at most twice the length of the shortest descending path from x to a leaf. Proof: # black nodes on any path from x = bh(x) (prop 5)  # nodes on shortest path from x, s(x). (prop 1) But, there are no consecutive red (prop 4), and we end with black (prop 3), so h(x) ≤ 2 bh(x). Thus, h(x) ≤ 2 s(x). QED ADS - AVL, Red Black Trees CS6213 - Arora - L3 39
  • 40.  Lemma 13.1: A red-black tree with n internal nodes has height at most 2 lg(n+1). ADS - AVL, Red Black Trees CS6213 - Arora - L3 40
  • 41. 1. 2. 3. 4. 5. Every node is either red or black. The root is black. Every leaf (nil) is black. If a node is red, then both its children are black. For each node, all paths from the node to descendant leaves contain the same number of black nodes. ADS - AVL, Red Black Trees CS6213 - Arora - L3 41
  • 42.  Insertion must preserve all red-black properties.  Should an inserted node be colored Red? Black?  Basic steps:  Use Tree-Insert from BST (slightly modified) to insert a node x into T.  Procedure RB-Insert(x).  Color the node x red.  Fix the modified tree by re-coloring nodes and performing rotation to preserve RB tree property.  Procedure RB-Insert-Fixup. ADS - AVL, Red Black Trees CS6213 - Arora - L3 42
  • 43.  Problem: we may have one pair of consecutive reds where we did the insertion.  Solution: rotate it up the tree and away…  Three cases have to be handled… ADS - AVL, Red Black Trees CS6213 - Arora - L3 43
  • 44. p[p[z]] new z C C p[z] y A D z   A  D   B   z is a right child here. Similar steps if z is a left child.  B    p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red.  Make p[z] and y black  Make p[p[z]] red  now z and p[z] are not both red.  restores property 5.  The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels). ADS - AVL, Red Black Trees CS6213 - Arora - L3 44
  • 45. B C p[z] z  A  A  y B  C      Make p[z] black and p[p[z]] red.  Then right rotate on p[p[z]]. Ensures property 4 is maintained.  No longer have 2 reds in a row.  p[z] is now black ADS - AVL, Red Black Trees  no more iterations. CS6213 - Arora - L3 45
  • 46. C C p[z] p[z]  y A  y B z  z B     Left rotate around p[z], p[z] and z switch roles  A   now z is a left child, and both z and p[z] are red.  This now becomes case 2. ADS - AVL, Red Black Trees CS6213 - Arora - L3 46
  • 47.  Symmetric to cases 2 and 3 in that parent of z is right child. ADS - AVL, Red Black Trees CS6213 - Arora - L3 47
  • 48.  O(lg n) time to get through RB-Insert up to the call of RB-Insert- Fixup.  Within RB-Insert-Fixup:  Each iteration takes O(1) time.  Each iteration but the last moves z up 2 levels.  O(lg n) levels  O(lg n) time.  Thus, insertion in a red-black tree takes O(lg n) time.  Note: there are at most 2 rotations overall. ADS - AVL, Red Black Trees CS6213 - Arora - L3 48
  • 49.  Deletion, like insertion, should preserve all the RB properties.  First do regular BST deletion  Find the node  If not a leaf, find the predecessor  If predecessor is a node with a single child, then adjust the pointers  Now, we need to fix any violations of RB properties that may result.  The properties that may be violated depends on the color of the deleted node.  Red – We are lucky.  Black? We need to consider 3 cases ADS - AVL, Red Black Trees CS6213 - Arora - L3 49
  • 50.  The parent is red, and the children of sibling node are black Recolor parent to black and sibling to red ADS - AVL, Red Black Trees CS6213 - Arora - L3 50
  • 51.  The parent is black, and the children of sibling node are black  Unlike Case 1, we cannot finish in one move, but the problem can be shifted “upwards”. B is now “double black”. ADS - AVL, Red Black Trees CS6213 - Arora - L3 51
  • 52.  The parent is red, and the children of sibling node are not both black  We can do a rotation ADS - AVL, Red Black Trees CS6213 - Arora - L3 52
  • 53.  Deletion of elements in array A ADS - AVL, Red Black Trees CS6213 - Arora - L3 53
  • 54.  Review ArrayList.java source code of Java. ADS - AVL, Red Black Trees CS6213 - Arora - L3 54