Sei sulla pagina 1di 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Trees
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.
Root

T1

T2

Tk

nodes, vertices arcs, edges


Scott Goodwin & Luis Rueda, 2003 1 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

The terminology of trees


A: Root of the tree B C D E B,C,D,E: Children of the root B,C,D,E: Sibblings F G H I E is an ancestor of K K is a descendant of E F,G,C,H,I,K are the leaves of the tree The depth of a node is the length of the path from the root to the node Length of path = # of edges Depth of E =1 Height of a node = length of the longest path from that node to a leaf Height of E = 2 Height of tree = height of root Height of example tree is 3
Scott Goodwin & Luis Rueda, 2003 2 of 104

J K

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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 structure How to insert and delete nodes in a tree structure How to find nodes in a tree structure How to traverse the entire tree structure How to organize structure for efficiency These issues will be at the core of much of the discussion of trees.
Scott Goodwin & Luis Rueda, 2003 3 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Binary Trees 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.
Root B D E F H A C G

TLeft

TRight

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.
Scott Goodwin & Luis Rueda, 2003 4 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Problem Show by induction that the number of degree 2 nodes in any binary tree is 1 less than the number of leaves. 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:
(i) (ii) (iii)

Let nL and nR be the number of nodes in the left and right subtrees respectively.
Scott Goodwin & Luis Rueda, 2003 5 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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 = n T0 1
Scott Goodwin & Luis Rueda, 2003 6 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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

Scott Goodwin & Luis Rueda, 2003

7 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Operations that we would like to do on a binary tree (a) (b) (c) (d) Find its height Find its size (number of nodes) Visit its nodes in a systematic way Etc.

Scott Goodwin & Luis Rueda, 2003

8 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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))

Scott Goodwin & Luis Rueda, 2003

9 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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 0 else return 1 + size(T.left) + size(T.right)

Scott Goodwin & Luis Rueda, 2003

10 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Tree traversals Many problems require that we visit the nodes of a binary tree structure in a systematic way. Three such ways are: a) Preorder traversal b) Postorder traversal c) Inorder traveral

Scott Goodwin & Luis Rueda, 2003

11 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Preorder traversal 1. Visit the root 2. Visit the left subtree in preorder 3. Visit the right subtree in preorder
A B D E F H C G

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

Scott Goodwin & Luis Rueda, 2003

12 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Postorder traversal 1. Visit the left subtree in postorder 2. Visit the right subtree in postorder 3. Visit the root
A B D E F H C G

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

Scott Goodwin & Luis Rueda, 2003

13 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Inorder traversal 1. Visit the left subtree in inorder 2. Visit the root 3. Visit the right subtree in inorder
B D E

A C F H G

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

Scott Goodwin & Luis Rueda, 2003

14 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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
Scott Goodwin & Luis Rueda, 2003 15 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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.
Scott Goodwin & Luis Rueda, 2003 16 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Note: In the above definition, we can replace with . Example:


16 11 10 1 2 4 5 6 9 8

Array Representation of a heap: 11 9 10 5 6 8 1 2 4 16 0 1 2 3 4 5 6 7 8 9 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?

Scott Goodwin & Luis Rueda, 2003

17 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Input: Indices i and j of 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) 12 0 10 1 20 2 19 3 8 4 7 5 15 6
19 12 10 8 7 20 15

Algorithm Heapify

Heapify(1,6): causes 10 to sink 12 0 19 1 20 2 10 3 8 4 7 5 15 6


10 Scott Goodwin & Luis Rueda, 2003 12 19 8 7 20 15

18 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Heapify(0,6): causes 12 to sink 20 0 19 1 12 2 10 3 8 4 7 5 15 6


10 20 19 8 7 12 15

Heapify(2,6): causes 12 to continue sinking 20 0 19 1 15 2 10 3 8 4 7 5 12 6


10 20 19 8 7 15 12

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.
Scott Goodwin & Luis Rueda, 2003 19 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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 0 Heapify(i, n-1) Initial array: 12 0 10 1 20 2 19 3 8 4 7 5 15 6
19 12 10 8 7 20 15

Heapify(2,6): no change

Scott Goodwin & Luis Rueda, 2003

20 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

12 0

10 1

20 2

19 3

8 4

7 5

15 6
19

12 10 8 7 20 15

Heapify(1,6): causes 10 to sink 19 20 10 8 12 0 1 2 3 4 Heapify(0,6): causes 12 to sink 20 0 19 1 12 2 10 3 8 4 7 5 7 5 15 6


10 12 19 8 7 20 15

15 6
10

20 19 8 7 12 15

Heapify(2,6): causes 12 to continue sinking 20 0 19 1 15 2 10 3 8 4 7 5 12 6


10 Scott Goodwin & Luis Rueda, 2003 20 19 8 7 15 12

21 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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) Initial array: 10 20 19 8 12 0 1 2 3 4 BuildHeap: produces array below 20 0 19 1 15 2 10 3 8 4 7 5 7 5 15 6
19 12 10 8 7 20 15

Algorithm Heapsort

12 6
10

20 19 8 7 15 12

Scott Goodwin & Luis Rueda, 2003

22 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Exchange(A[0],A[6]): produces array below 19 15 12 0 1 2 Heapify(0,5): produces 19 12 15 0 1 2 Heapify(1,5): no change 10 3 10 3 8 4 8 4 7 5 7 5 20 6


10 12 19 8 7 15 20

20 6
10

19 12 8 7 15 20

Exchange(A[0],A[5]): produces 7 0 12 1 15 2 10 3 8 4 19 5 20 6
10 7 12 8 19 15 20

Scott Goodwin & Luis Rueda, 2003

23 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Heapify(0,4): produces 12 7 15 0 1 2 Heapify(2,4): no change

10 3

8 4

19 5

20 6
10

15 12 8 19 7 20

Exchange(A[0],A[4]): produces 12 7 10 15 8 0 1 2 3 4 Heapify(0,3): produces 8 7 12 0 1 2 Heapify(1,3): produces 10 7 12 0 1 2


Scott Goodwin & Luis Rueda, 2003

19 5 19 5 19 5

20 6
10

8 12 15 19 7 20

10 3 8 3

15 4 15 4

20 6
10

12 8 15 19 7 20

20 6
8

12 10 15 19 7 20

24 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Exchange(A[0],A[3]) produces 10 7 12 15 8 0 1 2 3 4 Heapify(0,2): produces 8 7 12 10 0 1 2 3 Heapify(1,2): nochange Exchange(A[0],A[2]) produces 8 10 12 7 0 1 2 3 Heapify(0,1): produces 7 10 12 8 0 1 2 3 Heapify(1,1): no change
Scott Goodwin & Luis Rueda, 2003

19 5 19 5 19 5 19 5

20 6
12

8 10 15 19 7 20

15 4 15 4 15 4

20 6
12

10 8 15 19 7 20

20 6
12

7 8 15 19 10 20

20 6
12

8 7 15 19 10 20

25 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Exchange(A[0],A[1]): produces 8 10 12 15 7 0 1 2 3 4 Heapify(0,0): no change Data is sorted.

19 5

20 6
12

7 8 15 19 10 20

Scott Goodwin & Luis Rueda, 2003

26 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Complexity of Heapsort
BuildHeap is O(n) : True but difficult hard to prove it!! Heapify is O(log n) Heapify is called n times in heapsort. Therefore, Heapsort is O(n log n)

Scott Goodwin & Luis Rueda, 2003

27 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Binary Search Trees


Definition: A Binary Search Tree (BST) is a binary tree that satisfies the 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: Left Key Data Parent Right For simplicity, we usually omit the data field in examples. Also, the parent field can often be omitted depending on the intended application and/or the particular implementation.
Scott Goodwin & Luis Rueda, 2003 28 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

An example of a BST
5 3 2 4 7 8

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

Scott Goodwin & Luis Rueda, 2003

29 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Querying a BST
1. Searching for a node with a given key, k: TreeSearch(x,k) // x is the node where search starts if (x == NULL or k == x.Key) return x else if (k < x.Key) return TreeSearch(x.Left,k) else return TreeSearch(x.Right,k)

Scott Goodwin & Luis Rueda, 2003

30 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

2. Minimum and Maxium keys TreeMin(x) while (x.Left != NULL) x = x.Left return x; TreeMax(x) while (x.Right != NULL) x = x.Right return x;

Scott Goodwin & Luis Rueda, 2003

31 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

3. Successor and Predecessor 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.Parent while (p != NULL and x == p.Right) x=p p = x.Parent return p

Scott Goodwin & Luis Rueda, 2003

32 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Example: Successor of 13?


15 6 3 2 4 7 13 9 17 18 20

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

Scott Goodwin & Luis Rueda, 2003

33 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Definition: The predecessor of a node x is the node with the largest key smaller than x.Key. TreePredecessor(x) if (x.Left != NULL) return TreeMax(x.Left) else p = x.Parent while (p != NULL and x == p.Left) x=p p = x.Parent return p

Scott Goodwin & Luis Rueda, 2003

34 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Example: Predecessor of 13?


17 6 3 2 4 16 14 13 15 18 19 20

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

Scott Goodwin & Luis Rueda, 2003

35 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Insertion
TreeInsert(x, k) if (x == NULL) Create new node with key k Establish the corresponding links else if (k < x.Key) TreeInsert(x.Left, k) else if (k > x.Key) TreeInsert(x.Right, k) else return Key is in BST
3

12 6 7 13 15 17 18 19

Example: Insert key 13 into existing BST

Scott Goodwin & Luis Rueda, 2003

36 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Deletion
Case 1: Node has no children Simply remove the node. Example: Delete 13:
12 6 3 7 13 15 17 18 19

Scott Goodwin & Luis Rueda, 2003

37 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Case 2: Node has one child Replace node with its child. Example: Delete 13
9 6 3 7 10 11 12 13 3 6 7 10 9 11 12

Scott Goodwin & Luis Rueda, 2003

38 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Case 3: Node has two children Replace node with minimum key m in right subtree and recursively delete m in right subtree. Example: Delete 13
13 6 3 7 14 15
Scott Goodwin & Luis Rueda, 2003

14 6 3 7 14 15 14 16 17 18 19

18 16 17 19 3

6 7 15 16

18 19 17

39 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Balanced Search Trees


Definition: Binary Search Tree 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

Scott Goodwin & Luis Rueda, 2003

40 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

AVL Trees Named after its discoverers: Adelson, Velskii and Landis, 1962 Definition: An empty binary tree is an AVL tree. If T is a non-empty binary tree with T L and T R as its left and right sub-trees, then T is an AVL tree iff (a) T L and T R are AVL trees and (b) | hL hR | 1 where hL and hR are the heights of T L and T R respectively.

Scott Goodwin & Luis Rueda, 2003

41 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Definition: An AVL search tree is a binary search tree that is also an AVL tree Henceforth, by an AVL tree we shall mean an AVL search tree. Node of an AVL tree: balance balance = height of left subtree height of right subtree

Scott Goodwin & Luis Rueda, 2003

42 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Examples of AVL search trees 30 1 = 0 1 0 5 0 35 20 0 = 1 1 Fig.2: The number outside each node is its balance factor
Scott Goodwin & Luis Rueda, 2003

40

1 = 0 (1)

0 0 = 0 15 0 12

25 0 18 = 1 (1)

1=10

30 0
43 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Height of an AVL tree Let N h be the minimum number of nodes in an AVL tree of height h. Then N h =N h 1 + N h2 +1, N 0 =1, N1=2 assuming the worst-case scenario in which one sub-tree has height h -1 and the other h - 2 The solution to the recurrence given above is: N h = F h + 3 -1 , where F n is the nth Fibonacci number

Scott Goodwin & Luis Rueda, 2003

44 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Thus if n is the number of nodes in an AVL tree of height h n > Nh = Fh+3 - 1 where
=
h+3
5 1

5 +1 2

h < 1.44 log(n+2) - 1.328 h = O(log n)


Scott Goodwin & Luis Rueda, 2003 45 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Insertion into an AVL tree 2 30 0 5 35 1 32 0 Fig.3: Inserting key 32 causes the tree to become unbalanced 40 2

Scott Goodwin & Luis Rueda, 2003

46 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Imbalance due to: Insertion into left sub-tree of left child of a node k2 k1 C B
1 level 1 level

A Fig. 4

Scott Goodwin & Luis Rueda, 2003

47 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Balanced by LL-rotation k1 k2

Fig. 5

Scott Goodwin & Luis Rueda, 2003

48 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Results in: 30 1 0 5 32 0 35 0 40 0

Tree balanced by LL rotation

Scott Goodwin & Luis Rueda, 2003

49 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Pseudocode: LL rotation Input: Binary Node k2 Procedure withLeftChild( k2) Set k1 k2.Left if (k2 is Left child) k2.Parent.Left k1 else k2.Parent.Right k1 k2.Left k1.Right k1.Right k2 // Also, update Parent fields! return k1
Scott Goodwin & Luis Rueda, 2003 50 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Imbalance due to Insertion into right sub-tree of right child of a node k1 k2 C B A Fig. 6

Scott Goodwin & Luis Rueda, 2003

51 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Balanced by RR-rotation k2 k1

Fig. 7

Scott Goodwin & Luis Rueda, 2003

52 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Example of RR rotation: Insert 45 30 2 0 5 35 2 40 1 45 0 Tree unbalanced after insertion

Scott Goodwin & Luis Rueda, 2003

53 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

After the RR rotation: 30 1 0 5 35 0 40 0 45 0

Scott Goodwin & Luis Rueda, 2003

54 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Pseudocode: RR rotation Input: Binary Node k1 Procedure withRightChild( k1) Set k2 k1.Right if (k1 is Left child) k1.Parent.Left k2 else k2.Parent.Right k2 k1.Right k2.Left k2.Left k1 // Update Parent fields too! return k2
Scott Goodwin & Luis Rueda, 2003 55 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Imbalance caused by Insertion into right sub-tree of the left child of a node k3 k1 k1 k3

Before insertion

After insertion

Scott Goodwin & Luis Rueda, 2003

56 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Balanced by LR-rotation k3 k1 k2 D A B Before C After A B C D k1 k2 k3

Scott Goodwin & Luis Rueda, 2003

57 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Example of LR rotation:
8 4 2 5 6

12 16

10

14

Insertion of 5 causes the imbalance at node 8

12 6 4 2 5 8 14 10 16

Balanced by LR rotation

Scott Goodwin & Luis Rueda, 2003

58 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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:
8 6 4 2 5 10 14

12 16

RR-rotation

Scott Goodwin & Luis Rueda, 2003

59 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

12 6 4 2 5 8 14 10 16

LL-rotation

Scott Goodwin & Luis Rueda, 2003

60 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Imbalance caused by Insertion 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

Scott Goodwin & Luis Rueda, 2003

61 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

An example of RL rotation:
5 4 8 10 12

Insert 7

5 4 8 7 10 12

Scott Goodwin & Luis Rueda, 2003

62 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

After the RL rotation: 8 5 4 7 10 12

Scott Goodwin & Luis Rueda, 2003

63 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

5 4 8 7
5 LL rotation 4 RR rotation 5 4 7 8 10 12
64 of 104

10 12

RL unbalanced at 5

8
7 10 12

Scott Goodwin & Luis Rueda, 2003

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Pseudocode: LR rotation Input: Binary Node k3 Procedure doubleWithLeftChild( k3) Set k3.Left withRightChild(k3.Left) return withLeftChild(k3)

Pseudocode: RL rotation Input: Binary Node k3 Procedure doubleWithRightChild( k3) Set k3.Right withLeftChild(k3.Right) return withRightChild(k3)

Scott Goodwin & Luis Rueda, 2003

65 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Balance:
5 4 3 2 8 9 10

unbalanced at 4
heights are the same !!!
2 5 3 4 8 9 10

LL rotation

Scott Goodwin & Luis Rueda, 2003

66 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Balance:
5 1 8 9 10 12

height = 2

RR rotation height = 2
1 5 8

9 10 12

Note: the height of the tree is restored to its original value before RR rotation
67 of 104

Scott Goodwin & Luis Rueda, 2003

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Balance this:
1

5 9 8 7 8 5 1 7 9 10 10

Insert 7

RR rotation

Note: The imbalance due to insertion is corrected by a single or a double rotation. The height of the tree becomes the height of the sub-tree rooted at the node where the imbalance is restored.
Scott Goodwin & Luis Rueda, 2003 68 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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

Scott Goodwin & Luis Rueda, 2003

69 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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 the balance factor of the ancestors of q. Balance at q has changed to +1 or 1 height of subtrees at q is unchanged, so the balance of all the ancestors of q remains unchanged Balance at q is 2 tree needs to be balanced at q

C2:

C3:

Scott Goodwin & Luis Rueda, 2003

70 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Fig. Case C1, delete root Case C2, delete node circled in green Case C3, delete node circled in blue

Scott Goodwin & Luis Rueda, 2003

71 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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 A R if deletion has taken from the right sub-tree of A We discuss the R-type imbalance

Scott Goodwin & Luis Rueda, 2003

72 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

An R-type imbalance with balance factor 2 we had one of the following three situations (configurations) before deletion R0-type A B BL h 0 BR h Before
Scott Goodwin & Luis Rueda, 2003

1 AR h => BL h B

A 0 BR h After

2 , AR h-1

73 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

R1-type A B BL h 1 BR h-1 Before 1 AR h => B BL h After A 1 BR h-1 2 , AR h-1

Scott Goodwin & Luis Rueda, 2003

74 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

R-1 type A B BL h-1 CL -1 C b CR 1 AR h => B BL h-1 CL A -1 C b CR 2 , AR h-1

Before
Scott Goodwin & Luis Rueda, 2003

After
75 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Restoration R0 rotation B BL h -1 A BR h 1 , AR h-1

Scott Goodwin & Luis Rueda, 2003

76 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

R1-rotation

B BL h

0 A BR h-1 0 , AR h-1

Scott Goodwin & Luis Rueda, 2003

77 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

R-1 Rotation C B BL h-1 0 A CL CR , AR h-1

Similarly for an L-type of imbalance

Scott Goodwin & Luis Rueda, 2003

78 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Red-Black Trees Definition: A red-black tree is a binary search tree with the following properties: P1: P2: P3: P4: Each node is colored red or black The root is colored black A red node can only have black children Every path from a node to a null reference must contain the same number of black nodes

Scott Goodwin & Luis Rueda, 2003

79 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Example 30 15 10 5 40 20 50 45 60 65 80 70 85 90

Fig.1 A red-black tree


Scott Goodwin & Luis Rueda, 2003 80 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Note: It is important to know that we are talking about paths to a null node This excludes a tree like
B
B B B B B

In a tree like this:


B B

B
B B B B

All trees are null nodes The theorem is about such a tree
81 of 104

Scott Goodwin & Luis Rueda, 2003

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Theorem 1. If every path from the root to a null node B contains B black nodes then the # of black nodes is > 2 1 Proof By induction on B

Theorem 2. The height h of a red black tree with n nodes is at most 2log(n+1)-1 Proof

n 2 1 2
B

h+1 2

h +1 2 log(n +1) h 2 log(n +1) 1


Scott Goodwin & Luis Rueda, 2003

h +1 log( n + 1) 2

82 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Important conclusion h=O(log n) for a red-black tree with n nodes

Scott Goodwin & Luis Rueda, 2003

83 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Insertion into a red-black tree Insert node as a leaf Restore properties P1-P4, bottom-up OK? Observations Inserted node must be colored red Red-black property P3 is violated if it has a red parent

Scott Goodwin & Luis Rueda, 2003

84 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Case 1: Inserted node has a red parent, which has a black sibling Single rotation restoration G P X A B Fig.2
Scott Goodwin & Luis Rueda, 2003

S C D E A X

P G BC D S E
85 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Double rotation restoration G P A B X D C S E A P BC D X G S E

Fig.3

Scott Goodwin & Luis Rueda, 2003

86 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Case 2. Inserted node has a red parent, which has a red sibling G P X A B C D S E A X BC D
87 of 104

P G S E

Fig.4
Scott Goodwin & Luis Rueda, 2003

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

More on Case 2.

This case is problematic because P could have a red parent and we propagate the restoration to the root

Scott Goodwin & Luis Rueda, 2003

88 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Examples: Red-black trees.


30 15 10 5 20 50 40 35 55 60 65 80 70 85 90

Fig. 5

Changed to black
60 40 35 50 55 65

Insert 35, color is red, Red parent, sibling of parent also red, LL rotation involving key 40 and 50
Scott Goodwin & Luis Rueda, 2003

Now after rotation parent of 40 is red with black children


89 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

70 60 40 35 50 55 65 80 85 90

LL Rotation involving the keys 60 and 70 60 It is colored red because we want to present the # 40 70 of black nodes on the path 60-70-85 35 50 65 85
55
Scott Goodwin & Luis Rueda, 2003

80

90
90 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

30 15 10 5 3 40 20 50 55 60 65 80 70 85 90

Fig. 6

Insert 3. Inserted node has red parent with no sibling. LL rotation involving 5 & 10
15 5 3
Scott Goodwin & Luis Rueda, 2003

20 10

Note: on slide 85 case 2. It would not make if the sibling of P did not exist, as is the case. The same adjustment holds. This take care of everthing.
91 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

30 15 10 5 40 20 50 55 60 65 80 82 70 85 90

Fig. 7

Insert 82. Parent has red sibling


85 80 82 90 80 82 85 90

LR rotation

Scott Goodwin & Luis Rueda, 2003

92 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Top-down insertion Rule: If we hit a node with red children, we flip the color of the node & the children. If the nodes parent is red, then the nodes parents sibling cannot be red and we can apply the single rotation of Fig.2 or the double rotation of Fig.3

Claim.

Scott Goodwin & Luis Rueda, 2003

93 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Insert 45 into the tree of Fig.1 At node 50, we do a color flip 30 70 60 50 40 55 65 80 85 90

Fig. 8

Scott Goodwin & Luis Rueda, 2003

94 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Then do a single rotation at node 70 30 60 50 40 55 65 70

Fig. 9
Scott Goodwin & Luis Rueda, 2003 95 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Continue to descend 30 60 50 40 45 55

Fig. 10
Scott Goodwin & Luis Rueda, 2003

96 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Deletion from a red-black tree Deletion is as the case of a binary search tree, with the 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

Scott Goodwin & Luis Rueda, 2003

97 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Sentinel node

T Given red-black tree We add a sentinel node, color it red and make the given red-black tree the right sub-tree of the sentinel node.
Scott Goodwin & Luis Rueda, 2003 98 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Let X be the current node Let P be its parent Let T be its sibling Always ensure that, P is red both X & T are black why? Two cases: Case 1: X has 2 black children

Scott Goodwin & Luis Rueda, 2003

99 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Case 1.1: P X

T has 2 black children

T X

P T

Fig. Flip colors


Scott Goodwin & Luis Rueda, 2003 100 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Case 1.2: P X

T has an outer child that is red

T R => P

T R

X Fig. Single rotation on P: RR-rotation


Scott Goodwin & Luis Rueda, 2003 101 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Case 1.3:

T has an inner red child P

X R

T => P

R T

Fig. Double rotation on P (RL = LL + RR)


Scott Goodwin & Luis Rueda, 2003 102 of 104

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

Case 2:

X has a red child

Move one level down, obtaining a new X, T, P. If X is black, do a rotation as in the example below X X T P X B C P T B Fig. Single rotation continue from X
103 of 104

Scott Goodwin & Luis Rueda, 2003

60-254 Algorithms and Data Structures W03

Ch. 6 Trees

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.

Scott Goodwin & Luis Rueda, 2003

104 of 104

Potrebbero piacerti anche