0% found this document useful (0 votes)
34 views48 pages

Lec3 PDF

Uploaded by

RAj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views48 pages

Lec3 PDF

Uploaded by

RAj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Lecture 3

Instructor: Subrahmanyam Kalyanasundaram

13th August 2019


Last Class

We saw Binary Search Trees and the following:


Last Class

We saw Binary Search Trees and the following:


1. Search
2. Insert
3. Succ (also Pred)
Last Class

We saw Binary Search Trees and the following:


1. Search
2. Insert
3. Succ (also Pred)

4. Today we see, Delete


Successor

The Succ(val) procedure is as follows:


I Find the node which stores val. Refer to this node as “node”.
I Two cases:
Case 1: node has a right child.
Case 2: node does not have a right child.
Example

39

20 41

11 35 40 56

2 23 61

21 30 112

28
Delete

The Delete(val) procedure is as follows:


Find the node that has value val.
Delete

The Delete(val) procedure is as follows:


Find the node that has value val.
Three cases:
1. node has 0 children. (trivial)
2. node has 1 child. (splice)
3. node has 2 children:
I Find successor node X with value x.
I Splice X out of the tree.
I Replace val with x.
I Delete node X .
Example

39

20 41

Delete
11 35 40 56

2 23 61

21 30 112

28 33
Example

39

20 41

Delete
11 35 40 56

NULL NULL
2 23 61

21 30 112

28 33
Example

39

20 41

11 35

40 56

2 23 61

21 30 112

28 33
Example

39

20 41

11 35 56

2 23 61

21 30 112

28 33
Example

39

20 41

11 35 40 56

Delete
2 23 61

21 30 112

28 33
Example

39

20 41

11 35 40 56

2 23

61

21 30 112

28 33
Example

39

20 41

Delete

11 35 40 56

2 23 61

21 30 112

28 33
Example

39

20 41

11

35 40 56

2 23 61

21 30 112

28 33
Example

39

20 41

11 35 40 56

2 23 61
Delete

21 30 112

28 33
Example

39

20 41

11 35 40 56

2 23 61

21 30 112

Successor
28 33
Example

39

20 41

11 35 40 56

2 23 61

21 33 112
Replace

28 33
Example

39

20 41

11 35 40 56

2 23 61

21 33 112
Delete

28

33
Example

39

Delete
20 41

11 35 40 56

2 23 61

21 30 112

28 33
Example

39

20 41

11 35 40 56

2 23 61

21 30 112

Successor
28 33
Example

39

Replace
21 41

11 35 40 56

2 23 61

21 30 112

28 33
Example

39

21 41

11 35 40 56

2 23 61


Delete
21 30 112

28 33
Example

Delete 39

20 51

11 35 41 56

2 23 44 61

21 30 43 49 112

28 33
Example

39

20 51

11 35 41 56

Successor
2 23 44 61

21 30 43 49 112

28 33
Example
Replace
41

20 51

11 35 41 56

2 23 44 61

21 30 43 49 112

28 33
Example

41

20 51

Delete

11 35

41 56

2 23 44 61

21 30 43 49 112

28 33
Example

41

20 51

11 35 44 56

2 23 43 49 61

21 30 112

28 33
Running Time

Worst case running times for a BST of height h:


I Insert – O(h).
I Succ – O(h).
I Search – O(h).
I Delete – O(h).
The height of a BST depends on the input sequence and can be n
after inserting n elements in the worst case.
Balancing a BST

The biggest drawback of BSTs are that they can be quite


“unbalanced”.

One way to measure if a tree is balanced is to look at the difference


between the longest path from root to leaf and the shortest path
from root to leaf.
Balancing a BST

The biggest drawback of BSTs are that they can be quite


“unbalanced”.

One way to measure if a tree is balanced is to look at the difference


between the longest path from root to leaf and the shortest path
from root to leaf.

We want to make sure this difference does not get too large.
Balancing a BST

Balancing a BST is done by making structural changes to the


underlying tree.
Rotations are operations on nodes of a BST. They are of two
variants:
1. Left Rotate.
2. Right Rotate.
Left Rotate

X Y

Left Rotate

A X C
Y

B C A B
Example

41 41

20 51 20 51

ft
Le

11 35 41 56 11 35 44 56

2 23 44 61 2 23 41 49 61

21 30 43 49 112 21 30 43 112

28 33 28 33
Abstract Data Type

Set
maintains a set of elements from the universe

A set has the following functions:


I Insert(x) – Insert x into the set.
I Search(x) – Return True if x is an element of the set.
I Succ(x) – Returns the smallest value larger than x.
I Pred(x) – Returns the largest value smaller than x.
I GetMax() – Returns the largest value in the set.
I GetMin() – Returns the smallest value in the set.
I IsEmpty() – Returns True if and only if the set is empty.
I Delete(x) – Remove x from the set.
Data Structures for Set

Many choices of data structure to implement set:


I Array
I Sorted Array
I Heap
I Binary Search Tree
I Balanced Binary Search Trees
Data Structures for Set

Many choices of data structure to implement set:


I Array
I Sorted Array
I Heap
I Binary Search Tree
I Balanced Binary Search Trees
We now study a balanced Binary Search Tree called Red-Black
Trees.
Data Structure

Red-Black Trees
Red-Black Trees (RBT) are Binary Search Trees that balance
themselves!
Data Structure

Red-Black Trees
Red-Black Trees (RBT) are Binary Search Trees that balance
themselves! RBTs have the following properties:
1. All nodes are colored either Red or Black.
2. The root node is black.
3. The leaf nodes (NIL) are black.
4. Both children of a red node are black.
5. For any node, all paths from the node to the descendant leaves
have the same number of black nodes.
Example
18
1. Every node is colored
either Red or Black.
2. The root node is black. 16 27
3. The leaf nodes (NIL)
are black.
13 17 23 34
4. Both children of a red
node are colored NIL NIL NIL NIL NIL NIL NIL

black. 1
5. For any node, all
NIL NIL
paths from the node
to the descendant
leaves have the same
number of black
nodes.
A Red-Black Tree supports all procedures of a BST:
I Insert(val) – Inserts val into the RBT rooted at node.
I Search(val) – Returns True of val exists in the BST rooted at
node. False otherwise.
I Succ(val) – Returns the smallest element greater than val in
the RBT.
I Pred(val) – Returns the largest element lesser than val in the
RBT.
I Delete(val) – Deletes val from the RBT.
A Red-Black Tree supports all procedures of a BST:
I Insert(val) – Inserts val into the RBT rooted at node.
I Search(val) – Returns True of val exists in the BST rooted at
node. False otherwise.
I Succ(val) – Returns the smallest element greater than val in
the RBT.
I Pred(val) – Returns the largest element lesser than val in the
RBT.
I Delete(val) – Deletes val from the RBT.
The procedures in green are implemented exactly like in a BST.
Black-Height
The black-height of a node X is the number of black colored nodes
encountered on a path starting from X to any leaf (excluding X
itself).
18

16 27

13 17 23 34

NIL NIL NIL NIL NIL NIL NIL

NIL NIL

The black-height of the node with value 13 is 1.


The black-height of the root node is 2.
The black height of an red-black tree is the black height of its root.
Observations

Claim
A red-black tree with black-height β has height at most 2β.
Claim
A red-black tree with black-height β has height at most 2β.
Proof sketch:
I Try to construct the longest possible path with at most
β many black nodes.
I Property 4 will force you to color every alternate node
black.
Observations

Theorem
A red-black tree with n internal nodes has height at most
2 log(n + 1).
Observations

Theorem
A red-black tree with n internal nodes has height at most
2 log(n + 1).

Proof sketch
I Show that for any node X with black-height β, the
number of internal nodes in the subtree rooted at X is at
least 2β − 1.
I Conclude that with n internal nodes, the black-height
must be at most log(n + 1).
I Use previous claim that height is at most twice the
black-height to conclude the Theorem.

You might also like