Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either...

110
Trees Data Structures and Algorithms (60- 254)

Transcript of Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either...

Page 1: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

TreesData Structures and Algorithms (60-254)

Page 2: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

2

Recursive Definition of a Tree

A tree T is either empty or it consists of a root and zero or more nonempty subtrees T1,T2,…Tk each of whose roots are connected by an edge from the root of T.

nodes, verticesarcs, edges

Page 3: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

3

The terminology of trees

B

HGF

EDC

K

JI

A: Root of the treeB,C,D,E: Children of the rootB,C,D,E: SibblingsE is an ancestor of KK is a descendant of EF,G,C,H,I,K are the leaves of the treeThe depth of a node is the length of the path from the root to the nodeLength of path = # of edgesDepth of E =1Height of a node = length of the longest path from that node to a leafHeight of E = 2Height of tree = height of rootHeight of example tree is 3

A

Page 4: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

4

Applications

Trees are a ubiquitous data structure in Computer Science.The Unix directory structure is a tree.Allows user to store files in a logical/organized fashion. Issues:

How to implement tree structureHow to insert and delete nodes in a tree structureHow to find nodes in a tree structureHow to traverse the entire tree structureHow to organize structure for efficiency

These issues will be at the core of much of the discussion of trees.

Page 5: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

5

Binary Trees

A

B

ED

C

H

GF

Root

TRightTLeft

A binary tree T is either empty or it consists of a root and two (possibly empty) binary subtrees TLeft and TRight each of whose roots are connected by an edge from the root of T.

If the left (right) binary subtree is not empty, its root is called the left (right) child of the root of the tree.The degree of a node is the number of children it has.

Page 6: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

6

Problem

Show by induction that the number of degree 2 nodes (nT2) in any binary tree is 1 less than the number of leaves (nT0).Proof: The induction is on the number of nodes n in the binary tree. For n = 1, there is only the root node with 0 children. Hence, 1 leaf node and 0 nodes of degree 2.Suppose the claim is true for all binary trees with n or less nodes. Consider a binary tree with n+1 nodes. It has one of the following structures:

Let nL and nR be the number of nodes in the left and right subtrees respectively.

(i) (ii) (iii)

Page 7: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

7

Proof - continued

Since nL £ n and nR £ n, the claim is true for the left subtree in case (i) and the right subtree in case (ii). Since in both these cases the degree 2 nodes and the leaves belong to these subtrees alone, the claim holds for the entire tree.In case (iii), the claim is true for both subtrees. If we add the root to the count of degree 2 nodes of these trees, the claim follows.nL2 = nL0 -1 nR2 = nR0 -1 nT2 = nL2+nR2+1 (root is degree 2)

= nL0 - 1 + nR0 - 1 + 1 = nL0 +nR0 - 1 = nT0 – 1

Inductive Hypothesis

Inductive Step

Page 8: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

8

Exercise

Show by induction that a binary tree with n nodes has height at least ëlog nû.

Page 9: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

9

Operations that we would like to do on a binary tree

Find its heightFind its size (number of nodes)Visit its nodes in a systematic wayEtc.

Page 10: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

10

Height of a binary tree

Height(T) = 1 + max(Height(TL),Height(TR))Height(Æ) = -1 Write a recursive function based on the above definition. height(T)

if (T is empty) return –1 else return 1 + max(height(T.left),height(T.right))

Page 11: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

11

Size of a binary tree

Size(Æ) = 0

Size(T) = 1 + Size(TL) + Size(TR) Write a recursive function based on the above definition. size(T)

if (T is empty) return 0else return 1 + size(T.left) + size(T.right)

Page 12: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

12

Tree traversals

Many problems require that we visit the nodes of a binary tree structure in a systematic way. Three such ways are:

1. Preorder traversal2. Postorder traversal3. Inorder traveral

Page 13: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

13

Preorder traversal

1. Visit the root2. Visit the left subtree in preorder3. Visit the right subtree in preorder

For example, printing the labels of the nodes in the given tree using a preorder traversal gives: A,B,D,E,C,F,G,H

Page 14: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

14

Postorder traversal

1. Visit the left subtree in postorder2. Visit the right subtree in postorder3. Visit the root

For example, printing the labels of the nodes in the given tree using a postorder traversal gives: D,E,B,F,H,G,C,A

Page 15: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

15

Inorder traversal

1. Visit the left subtree in inorder2. Visit the root3. Visit the right subtree in inorder

For example, printing the labels of the nodes in the given tree using an inorder traversal gives: D,B,E,A,F,C,H,G

Page 16: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

16

Printing

printPreorder(T) print T.data // visit root if (T.left is not empty) printPreorder(T.left) // visit left if (T.right is not empty) printPreorder(T.right) // visit right printPostorder(T) if (T.left is not empty) printPostorder(T.left) // visit left if (T.right is not empty) printPostorder(T.right) // visit right print T.data // visit root printInorder(T) if (T.left is not empty) printInorder(T.left) // visit left print T.data // visit root if (T.right is not empty) printInorder(T.right) // visit right

Page 17: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

17

Heapsort

We can define another sorting algorithm that uses a binary tree.Definition:An (essentially) complete binary tree is a binary tree that is complete at all levels except possibly the last.Examples:

Definition: A heap is an essentially complete binary tree with the additional property: The key stored at any node is the keys stored at its children, if any.

Page 18: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

18

Replacing by

Example:

Array Representation of a heap:

Key values placed in array level-by-level and left to right.Children of node at index i placed at indices 2i+1 and 2i+2.Given, an array of n elements, how can we heapify them?

16 11 9 10 5 6 8 1 2 4 0 1 2 3 4 5 6 7 8 9

Page 19: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

19

Algorithm Heapify

Input: Indices i and j. An array A[0..n-1] of key values.

Output: A[i] sinks to appropriate place in A[i..j]

Procedure Heapify(i, j)

if A[i] is a leaf node then return

Set A[k] ¬ max{A[2i+1],A[2i+2]} // k is index of max.

where 2i+1 and 2i+2 j

if (A[i] < A[k]) then Exchange(A[i],A[k])

Heapify(k, j)

Page 20: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

20

Example

12 10 20 19 8 7 15 0 1 2 3 4 5 6

10 20

19 8 7 15

Heapify(1,6): causes 10 to sink

12 19 20 10 8 7 15 0 1 2 3 4 5 6

19 20

10 8 7 15

Heapify(0,6): causes 12 to sink

20 19 12 10 8 7 15 0 1 2 3 4 5 6

19 12

10 8 7 15

Page 21: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

21

Example

Heapify(2,6): causes 12 to continue sinking

20 19 15 10 8 7 12 0 1 2 3 4 5 6

19 15

10 8 7 12

Page 22: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

22

Heapify and BuildHeap

Heapify(0,n-1) is O(log n) Heapify(i,n-1) only fixes element i

To build the entire heap, we must apply heapify to all of the elements in turn.

Page 23: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

23

Algorithm BuildHeap

Input: An array A[0..n-1] of key values.Output: An array A[0..n-1] that satisfies the heap property.Procedure BuildHeap(A)

for i n div 2 - 1 downto 0Heapify(i, n-1)

Initial array:

12 10 20 19 8 7 15 0 1 2 3 4 5 6

10 20

19 8 7 15

i: 7 div 2 – 1 = 2BuildHeap -Heapify(2,6): no

change

Page 24: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

24

BuildHeap Example12 10 20 19 8 7 15 0 1 2 3 4 5 6

10 20

19 8 7 15 Heapify(1,6): causes 10 to

sink 12 19 20 10 8 7 15 0 1 2 3 4 5 6

19 20

10 8 7 15 Heapify(0,6): causes 12 to sink

20 19 12 10 8 7 15 0 1 2 3 4 5 6

19 12

10 8 7 15

Heapify(2,6): causes 12 to continue sinking

20 19 15 10 8 7 12 0 1 2 3 4 5 6

19 15

10 8 7 12

Page 25: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

25

Algorithm Heapsort

Input: An array A[0..n-1] of key-values.Output: A sorted array A[0..n-1] of key-values. BuildHeap(A)for i n-1 downto 1

Exchange(A[0],A[i])Heapify(0,i-1)

Page 26: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

26

Example

Page 27: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

27

Example Continued

Page 28: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

28

Page 29: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

29

Page 30: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

30

Page 31: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

31

Complexity of Heapsort

BuildHeap is O(n) : True but difficult to prove!!

Heapify is O(log n)

Heapify is called n times in heapsort.

Therefore, Heapsort is O(n log n)

Page 32: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

32

Binary Search Trees

Definition:A Binary Search Tree (BST) is a binary tree that satisfiesthe property that for every node x in the BST,• x.Key > every key in x.Left (left subtree), and• x.Key < every key in x.Right (right subtree).A node of a binary search tree may be represented with 5 fields:

For simplicity, we usually omit the data field in examples.Also, the parent field can often be omitted depending on theintended application and/or the particular implementation.

Page 33: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

33

An example of a BST

BST property ⇒Inorder traversal of a BST visits the nodes in sorted order:2,3,4,5,7,8 (ascending order)

Page 34: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

34

1. Searching for a node with a given key, k:

TreeSearch(x,k) // x is the node where search startsif (x == NULL or k == x.Key) return xelse if (k < x.Key) return TreeSearch(x.Left,k)else return TreeSearch(x.Right,k)

Page 35: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

35

2. Minimum and Maximum keys

TreeMin(x)while (x.Left != NULL)

x = x.Leftreturn x;

TreeMax(x)while (x.Right != NULL)

x = x.Rightreturn x;

Page 36: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

36

3. Successor of a key

Definition: The successor of a node x is the node with the smallest key greater than x.Key

TreeSuccessor(x)if (x.Right != NULL)

return TreeMin(x.Right)else

p = x.Parentwhile (p != NULL and x == p.Right)

x = pp = x.Parent

return p

Page 37: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

37

Example: Successor of 13?

Note that 13 is the maximum key in the BST rooted at 6

Page 38: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

38

4. Predecessor of a key

Definition: The predecessor of a node x is the node with thelargest key smaller than x.Key.

TreePredecessor(x)if (x.Left != NULL)

return TreeMax(x.Left)else

p = x.Parentwhile (p != NULL and x == p.Left)

x = pp = x.Parent

return p

Page 39: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

39

Example: Predecessor of 13?

Note that 13 is the minimum key in the BST rooted at 16

Page 40: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

40

Insertion

Node TreeInsert(x, k)if (x == NULL)

Create new node t with key kelse if (k < x.Key)

x.Left ← TreeInsert(x.Left, k)else if (k > x.Key)

x.Right ← TreeInsert(x.Right, k)else

Throw exception “Existing key!”return t

Page 41: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

41

Example: Insert key 13 into existing BST

Page 42: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

42

Deletion

Assumption: The node to be deleted is given.Case 1: Node has no childrenSimply remove the node.Example: Delete 13

Page 43: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

43

Deletion

Case 2: Node has one childReplace node with its child.Example: Delete 13

Page 44: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

44

Deletion

Case 3: Node has two childrenReplace node with minimum key min right subtree and recursivelydelete m in right subtree.Example: Delete 13

Page 45: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

45

Balanced Search Trees

Definition:

Binary Search Trees with a worst-case height of O(log n) are called balanced trees

We can guarantee O(log n) performance for each search tree operation for balanced trees.

We will discuss two kinds of balanced trees(a) AVL trees(b) Red-black trees

Page 46: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

46

AVL Trees

Named after its discoverers:Adelson-Velskii and Landis, 1962

Definition: An empty binary tree is an AVL tree. If T is anon-empty binary tree with TL and TR as its leftand right sub-trees, then T is an AVL tree iff(a) TL and TR are AVL trees and(b) | hL − hR | ≤ 1 where hL and hR are the heights ofTL and TR respectively.

Page 47: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

47

AVL search tree

Definition: An AVL search tree is a binary search tree that is also an AVL treeHenceforth, by an AVL tree we shall mean an AVL search tree.

balance = height of left subtree – height of right subtree

Node of an AVL tree:

Page 48: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

48

Examples of AVL search treesBalance factor = left balance– right balance

Page 49: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

49

Height of an AVL tree

Let Nh be the minimum number of nodes in an AVL tree of height h.

Then Nh = Nh−1+ Nh−2 +1, N0=1, N1=2assuming the worst-case scenario in which one sub-tree hasheight h-1 and the other h-2The solution to the recurrence given above is:

Nh = Fh+3 -1 ,

where Fn is the nth Fibonacci number

Page 50: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

50

Page 51: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

51

Insertion into an AVL tree

Inserting key 32 causes the tree tobecome unbalanced

Page 52: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

52

Imbalance due to:Insertion into left sub-tree of left child of a node

Page 53: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

53

Balanced by LL-rotation

Page 54: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

54

Tree balanced by LL rotation

Page 55: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

55

Pseudocode: LL rotation

Input: Binary Node k2Procedure withLeftChild( k2)

Set k1 ← k2.Leftif (k2 is Left child)

k2.Parent.Left ← k1else

k2.Parent.Right ← k1k2.Left ← k1.Rightk1.Right ← k2// Also, update Parent fields!return k1

Page 56: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

56

Imbalance due toInsertion into right sub-tree of right child of a node

Page 57: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

57

Balanced by RR-rotation

Page 58: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

58

Example of RR rotation: Insert 45Tree unbalanced after insertion

Page 59: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

59

After the RR rotation:

Page 60: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

60

Pseudocode: RR rotation

Input: Binary Node k1Procedure withRightChild( k1)

Set k2 ← k1.Rightif (k1 is Left child)

k1.Parent.Left ← k2else

k2.Parent.Right ← k2k1.Right ← k2.Leftk2.Left ← k1// Update Parent fields too!return k2

Page 61: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

61

Imbalance caused byInsertion into right sub-tree of the left child of a node

Page 62: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

62

Balanced by LR-rotation

Page 63: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

63

Example of LR rotation:

Page 64: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

64

Remark: An LR rotation is often called a double rotation because it can be viewed as an RR rotation, followed by an LL rotation

On the example:After the RR-rotation

Page 65: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

65

After the LL-rotation

Page 66: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

66

Imbalance caused byInsertion into left sub-tree of right child

Balance is restored by an RL rotation

Details are the same as in the case of a LR rotation

Page 67: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

67

An example of RL rotation:

Page 68: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

68

After the RL rotation:

Page 69: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

69

Page 70: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

70

Pseudocode: LR rotation

Input: Binary Node k3Procedure doubleWithLeftChild( k3)

Set k3.Left ← withRightChild(k3.Left)return withLeftChild(k3)

Page 71: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

71

Pseudocode: RL rotation

Input: Binary Node k3Procedure doubleWithRightChild( k3)

Set k3.Right ← withLeftChild(k3.Right)return withRightChild(k3)

Page 72: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

72

Balance:

Page 73: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

73

Balance:

Page 74: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

74

Page 75: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

75

Deletion from an AVL tree

We delete a node just as in a binary search tree• Let q be the parent of the physically deleted node.• We might need to go all the way from q to the root in order to balance the tree and restore AVL-balance

Page 76: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

76

Three cases arise:

C1: Balance at q has changed to 0 height has decreased⇒by 1 (of the tree rooted at q) and we need to check thebalance factor of the ancestors of q.

C2: Balance at q has changed to +1 or –1 height of subtrees⇒at q is unchanged, so the balances of all theancestors of q remain unchanged

C3: Balance at q is ±2 tree needs to be balanced at ⇒ q

Page 77: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

77

Three cases: example

Case C1, delete rootCase C2, delete node circled in greenCase C3, delete node circled in blue

Page 78: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

78

Types of Imbalance

Let A be the first node on the path from q to the root where the balance has changed to ±2

The imbalance is of type

L if deletion has taken from the left sub-tree of AR if deletion has taken from the right sub-tree of A

We discuss the R-type imbalance

Page 79: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

79

Page 80: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

80

Page 81: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

81

Page 82: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

82

Page 83: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

83

Page 84: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

84

Page 85: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

85

Page 86: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

86

A Red-Black Tree

Page 87: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

87

Page 88: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

88

Page 89: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

89

Important conclusion

For a red-black tree with n nodes

Page 90: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

90

Insertion into a red-black tree (Method 1)Insert node as a leafRestore properties P1-P4, bottom-upOK?ObservationsInserted node must be colored redRed-black property P3 is violated if it has a red parent6 cases. We’ll study 3 of them:

Inserted node is in the “left” subtree of G.

Page 91: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

91

Case 1: Inserted node has a red parent, which has a black siblingSub-case 1.1: X is an outer child

Single rotation restoration (LL)

Page 92: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

92

Sub-case 1.2: X is an inner child

Double rotation restoration (LR)

Page 93: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

93

Case 2: Inserted node has a red parent, which has a red sibling.

Single rotation restoration: LL

Page 94: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

94

More on Case 2

This case is problematic because:P could have a red parent, andwe need to propagate the restoration up to the root

Page 95: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

95

Page 96: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

96

Page 97: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

97

Page 98: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

98

Page 99: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

99

Top-down insertion

Rule: Walk down the tree (starting from root).If we hit a node X with (two) red children,then flip the color of X and its children.

Claim. If the parent of X is red,Then X’s parent’s sibling cannot be red, andthe single rotation (LL) orthe double rotation (LR) can be applied.

Page 100: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

100

Page 101: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

101

Page 102: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

102

Page 103: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

103

Deletion from a red-black tree

Deletion is as the case of a binary search tree, but we need to guarantee that the color of the physically deleted node is red

Start from the root…

As we search down the tree,we maintain the invariant that

the color of the current node X is red

Page 104: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

104

Page 105: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

105

Let X be the current nodeLet P be its parentLet T be its siblingAlways ensure that, P is red

⇒ both X and T are black … why?

Two cases:Case 1: X has 2 black children

Page 106: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

106

Case 1.1: T has 2 black children

Page 107: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

107

Case 1.2: T has an outer child that is red

Page 108: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

108

Case 1.3: T has an inner red child

Page 109: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

109

Case 2: X has a red child

Page 110: Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.

110

Deletion from a red-black tree

Note that the rotation is the main case when X has two black children, always when X red.

When X has a red child we have to move one level down on the search path (whether the child along the search path is red, the rotation is both).

Note that in Case 1.2, if both children of T are red we can still apply the case.