balanced tree (avl tree & red-black tree)

63
Balanced Tree AVL Tree & RED-BLACK Tree By Samrin Ahmed Riya ID : 011142021 Sanzida Akter ID : 011142032

Upload: united-international-university

Post on 16-Feb-2017

75 views

Category:

Engineering


4 download

TRANSCRIPT

Page 1: Balanced Tree (AVL Tree & Red-Black Tree)

Balanced TreeAVL Tree

&RED-BLACK

Tree

BySamrin Ahmed Riya

ID : 011142021Sanzida Akter

ID : 011142032

Page 2: Balanced Tree (AVL Tree & Red-Black Tree)

Balanced Tree Node based binary search tree Automatically balance it’s height in the face of arbitrary item

insertions and deletions

Fig : Balanced Tree

Page 3: Balanced Tree (AVL Tree & Red-Black Tree)

AVL Tree

Page 4: Balanced Tree (AVL Tree & Red-Black Tree)

AVL Tree

A special kind of binary search tree Self balancing tree Height of right sub tree ˞ ˞ height of left sub tree ≤ 1

Features :

Georgy Adelson-Velsky and Evgenii Landis' tree Named after the inventors (1962)

Page 5: Balanced Tree (AVL Tree & Red-Black Tree)

Examples

Page 6: Balanced Tree (AVL Tree & Red-Black Tree)

AVL Tree or not

YESEach left sub-tree has height 1 greater than each right sub-tree

NOLeft sub-tree has height 3, but right sub-tree has height 1

Page 7: Balanced Tree (AVL Tree & Red-Black Tree)

Operations

Insertion

Deletion

Traversal

Searching

Page 8: Balanced Tree (AVL Tree & Red-Black Tree)

Rotation for Balancing

Page 9: Balanced Tree (AVL Tree & Red-Black Tree)

It is performed as in binary search trees. For insertions, one rotation is sufficient. Sometimes it needs two rotations.

Insertion

Page 10: Balanced Tree (AVL Tree & Red-Black Tree)

Insertion(0,0)1

Insert 1Elements :1 2 3 6 15 -2 -5 -8

Page 11: Balanced Tree (AVL Tree & Red-Black Tree)

Insertion

(0,0)2

(0,1)1Insert 2Elements :

1 2 3 6 15 -2 -5 -8

Page 12: Balanced Tree (AVL Tree & Red-Black Tree)

Insertion

Rotation needed

(0,0)3

(0,1)2

(0,2)1Insert 3Elements :

1 2 3 6 15 -2 -5 -8

Page 13: Balanced Tree (AVL Tree & Red-Black Tree)

1 (0,0)3

(1,1)2Insert 3

(0,0)

InsertionElements :1 2 3 6 15 -2 -5 -8

Page 14: Balanced Tree (AVL Tree & Red-Black Tree)

1 (0,1)3

(1,2)2Insert 6

(0,0)

(0,0)6

InsertionElements :1 2 3 6 15 -2 -5 -8

Page 15: Balanced Tree (AVL Tree & Red-Black Tree)

1 (0,2)3

(1,3)2Insert 15

(0,0)

(0,1)6

(0,0)15

Rotation needed

InsertionElements :1 2 3 6 15 -2 -5 -8

Page 16: Balanced Tree (AVL Tree & Red-Black Tree)

1 (1,1)6

(1,2)2Insert 15

(0,0)

(0,0)153 (0,0)

InsertionElements :1 2 3 6 15 -2 -5 -8

Page 17: Balanced Tree (AVL Tree & Red-Black Tree)

1 (1,1)6

(2,2)2Insert -2

(1,0)

(0,0)153 (0,0)-2 (1,0)

InsertionElements :1 2 3 6 15 -2 -5 -8

Page 18: Balanced Tree (AVL Tree & Red-Black Tree)

1 (1,1)6

(3,2)2Insert -5

(2,0)

(0,0)153 (0,0)-2 (1,0)

-5 (0,0)

Rotation needed

InsertionElements :1 2 3 6 15 -2 -5 -8

Page 19: Balanced Tree (AVL Tree & Red-Black Tree)

-2 (1,1)6

(2,2)2Insert -5

(1,1)

(0,0)1 3(0,0)

-5 (0,0)

(0,0)

(0,0)15

InsertionElements :1 2 3 6 15 -2 -5 -8

Page 20: Balanced Tree (AVL Tree & Red-Black Tree)

-2 (1,1)6

(3,2)2Insert -8

(2,1)

(0,0)1 3(0,0)

-5 (1,0)

(0,0)

(0,0)15

-8 (0,0)

InsertionElements :1 2 3 6 15 -2 -5 -8

Page 21: Balanced Tree (AVL Tree & Red-Black Tree)

Deletion Deletion can make the tree unbalanced One rotation is needed for rebalancing Sometimes it needs two rotations

Page 22: Balanced Tree (AVL Tree & Red-Black Tree)

Insertion & Deletion (Algorithms)

LeftRotationAVL (x: BinTree) { x := ( x. rightChild .key ,

(x.key , x. leftChild , x. rightChild . leftChild ) , x. rightChild . rightChild );

}

RightRotation (x: BinTree) { x := ( x. leftChild .key ,

x. leftChild . leftChild , (x.key , x. leftChild . rightChild , x. rightChild ) );

}

Page 23: Balanced Tree (AVL Tree & Red-Black Tree)

Insertion & Deletion (Algorithms) RightLeftRotation (x: BinTree) {

x := ( x. rightChild . leftChild .key , ( x.key , x. leftChild , x. rightChild . leftChild . leftChild ) , ( x. rightChild .key , x. rightChild . leftChild . rightChild , x. rightChild . rightChild ) ); }

LeftRightRotation(x: BinTree) { x := ( x. leftChild . rightChild .key ,

( x. leftChild .key , x. leftChild . leftChild , x. leftChild . rightChild . leftChild ) , ( x.key , x. leftChild . rightChild . rightChild , x. rightChild ) ); }

Page 24: Balanced Tree (AVL Tree & Red-Black Tree)

Traversal Maintains preorder, inorder and postorder traversal Depends on the height of the tree

Page 25: Balanced Tree (AVL Tree & Red-Black Tree)

Traversal (Algorithms)Preorder Traversal

void preorder(node *t) { if (t != NULL) {

printf(“%d ”, t->element); preorder(t->leftChild); preorder(t->rightChild);

} }

Page 26: Balanced Tree (AVL Tree & Red-Black Tree)

Traversal (Algorithms)Inorder Traversal

void inorder(node *t) { if (t != NULL) {

inorder(t->leftChild); printf(“%d ”, t->element); inorder(t->rightChild);

} }

Page 27: Balanced Tree (AVL Tree & Red-Black Tree)

Traversal (Algorithms)Postorder Traversal

void postorder(node *t) { if (t != NULL) {

postorder(t->leftChild); /* L */ postorder(t->rightChild); /* R */ printf(“%d ”, t->element); /* V */

} }

Page 28: Balanced Tree (AVL Tree & Red-Black Tree)

Searching Similar to normal unbalanced binary search tree. Successful searches are limited by the height of the tree. Unsuccessful searching time is very close to the height of the

tree.

Page 29: Balanced Tree (AVL Tree & Red-Black Tree)

AVL Tree

Page 30: Balanced Tree (AVL Tree & Red-Black Tree)

Applications of AVL Tree Used in many search applications where data is constantly

entering/leaving. To security concerns and to parallel code. Creating new types of data structures.

Page 31: Balanced Tree (AVL Tree & Red-Black Tree)

Red-Black Tree

Page 32: Balanced Tree (AVL Tree & Red-Black Tree)

• A balancing binary search tree.

• A data structure requires an extra one bit color field in each node which is red or black.

• Leonidas J. Guibas and Robert Sedgewick derived the red-black tree from the symmetric binary B-tree.

Introduction

Page 33: Balanced Tree (AVL Tree & Red-Black Tree)

Example of Red-Black Tree

Page 34: Balanced Tree (AVL Tree & Red-Black Tree)

• The root and leaves (NIL’s) are black. • A RED parent never has a RED child.• in other words: there are never two successive RED nodes in a path

• Every path from the root to an empty subtree contains the same number of BLACK nodes• called the black height

• We can use black height to measure the balance of a red-black tree.

Properties of Red-Black Tree

Page 35: Balanced Tree (AVL Tree & Red-Black Tree)

Average

Space O(n)

Search O(log2 n)

Traversal O(n)

Insertion O(log2 n)

Deletion O(log2 n)

Red-black tree Operations

Page 36: Balanced Tree (AVL Tree & Red-Black Tree)

• Basic operation for changing tree structure is called rotation:

Red-Black Trees: Rotation

Page 37: Balanced Tree (AVL Tree & Red-Black Tree)

x

y

y

x

• x keeps its left child• y keeps its right child• x’s right child becomes y’s left child• x’s and y’s parents change

A B

C A

B C

Red-Black Trees: Rotation

Page 38: Balanced Tree (AVL Tree & Red-Black Tree)

Rotation Example• Rotate left about 9:

12

5 9

7

8

11

Page 39: Balanced Tree (AVL Tree & Red-Black Tree)

Rotation Example• Rotate left about 9:

5 12

7

9

118

Page 40: Balanced Tree (AVL Tree & Red-Black Tree)

LEFT-ROTATE(T, x) y ← x->right x->right← y->left y->left->p ← x y->p ← x->p

if x->p = Null then T->root ← y

else if x = x->p->left then x->p->left ← y else x->p->right ← y

y->left ← xx->p ← y

RIGHT-ROTATE(T, x) y ← x->left x->left← y->right y->right->p ← x y->p ← x->p

if x->p = Null then T->root ← y else if x = x->p->right then x->p->right ← y else x->p->left ← y

y->right ← xx->p ← y

Runtime : O(1) for Both.

Rotation Algorithm

Page 41: Balanced Tree (AVL Tree & Red-Black Tree)

Red-Black Trees: Insertion

• Insertion: the basic idea• Insert x into tree, color x red• Only r-b property 3 might be violated (if p[x] red)• If so, move violation up tree until a place is found

where it can be fixed• Total time will be O(log n)

Page 42: Balanced Tree (AVL Tree & Red-Black Tree)

Red-Black Insertion: Case 1

B

x

● Case 1: “uncle” is red● In figures below, all ’s are equal-black-height

subtrees

C

A D

C

A D

y

new x

Same action whether x is a left or a right child

B

x case 1

Page 43: Balanced Tree (AVL Tree & Red-Black Tree)

Red-Black Insertion: Case 2

B

x

● Case 2:■ “Uncle” is black■ Node x is a right child

● Transform to case 3 via a left-rotation

CA

CBy

A

x

case 2

y

Transform case 2 into case 3 (x is left child) with a left rotationThis preserves property 4: all downward paths contain same number of black nodes

Page 44: Balanced Tree (AVL Tree & Red-Black Tree)

Red-Black Insertion: Case 3● Case 3:

■ “Uncle” is black■ Node x is a left child

● Change colors; rotate right

BAx

case 3CB

A

x

y C

Perform some color changes and do a right rotationAgain, preserves property 4: all downward paths contain same number of black nodes

Page 45: Balanced Tree (AVL Tree & Red-Black Tree)

Red-Black Insert: Cases 4-6• Cases 1-3 hold if x’s parent is a left child

• If x’s parent is a right child, cases 4-6 are symmetric (swap left for right)

Page 46: Balanced Tree (AVL Tree & Red-Black Tree)

Insertion Example

Insert 6547

7132

93

Page 47: Balanced Tree (AVL Tree & Red-Black Tree)

Insertion Example

Insert 6547

7132

65 93

Page 48: Balanced Tree (AVL Tree & Red-Black Tree)

Insert 6547

7132

65 93

Insert 82

Insertion Example

Page 49: Balanced Tree (AVL Tree & Red-Black Tree)

82

Insert 65 47

7132

65 93

Insert 82

Insertion Example

Page 50: Balanced Tree (AVL Tree & Red-Black Tree)

82

Insert 6547

7132

65 93

Insert 82

65

71

93

change nodes’ colors

Insertion Example

Page 51: Balanced Tree (AVL Tree & Red-Black Tree)

9365

71

82

Insert 65

47

32

Insert 82

Insert 87

87

Insertion Example

Page 52: Balanced Tree (AVL Tree & Red-Black Tree)

9365

71

82

Insert 65

47

32

Insert 82

Insert 87

87

Insertion Example

Page 53: Balanced Tree (AVL Tree & Red-Black Tree)

9365

71

87

Insert 65

47

32

Insert 82

Insert 87

82

Insertion Example

Page 54: Balanced Tree (AVL Tree & Red-Black Tree)

9365

87

Insert 65

47

32

Insert 82

Insert 87

82

71

87

93

change nodes’ colors

Insertion Example

Page 55: Balanced Tree (AVL Tree & Red-Black Tree)

87

93

65

Insert 65

47

32Insert 82Insert 87

82

71

Insertion Example

Page 56: Balanced Tree (AVL Tree & Red-Black Tree)

TreeNode<T> rbInsert(TreeNode<T> root,TreeNode<T> x)// returns a new root{ root=bstInsert(root,x); // a modification of BST insertItem x.setColor(red); while (x != root and x.getParent().getColor() == red) { if (x.getParent() == x.getParent().getParent().getLeft()) { //parent is left child y = x.getParent().getParent().getRight() //uncle of x if (y.getColor() == red) {// uncle is red x.getParent().setColor(black); y.setColor(black); x.getParent().getParent().setColor(red); x = x.getParent().getParent(); } else { // uncle is black if (x == x.getParent().getRight()) { x = x.getParent(); root = left_rotate(root,x); } x.getParent().setColor(black); x.getParent().getParent().setColor(red); root = right_rotate(root,x.getParent().getParent()); }} } else // ... symmetric to if } // end while root.setColor(black); return root;}

Insertion Algorithm

Page 57: Balanced Tree (AVL Tree & Red-Black Tree)

Red-Black Tree Deletion• If n has no children, we only have to remove n from the tree.• If n has a single child, we remove n and connect its parent to its child.• If n has two children, we need to :• Find the smallest node that is larger than n, call it m.• Remove m from the tree and Replace the value of n with m.• Then restores the red-black tree properties.

Page 58: Balanced Tree (AVL Tree & Red-Black Tree)

Red-Black Tree Deletion AlgorithmTreeNode<T> rbDelete(TreeNode<T> root,TreeNode<T> z)//return new root, z contains item to be deleted{ TreeNode<T> x,y; // find node y, which is going to be removed if (z.getLeft() == null || z.getRight() == null) y = z; else { y = successor(z); // or predecessor z.setItem(y.getItem); // move data from y to z } // find child x of y if (y.getRight() != null) x = y.getRight(); else x = y.getLeft(); // Note x might be null; create a pretend node if (x == null) { x = new TreeNode<T>(null); x.setColor(black); }

Page 59: Balanced Tree (AVL Tree & Red-Black Tree)

Red-black tree Searching:• Searching a node from a red-black tree doesn’t require more than the

use of the BST procedure, which takes O(log n) time.

Page 60: Balanced Tree (AVL Tree & Red-Black Tree)

Red-Black Trees efficiency All operations work in time O(height) hence, all operations work in time O(log n)! – much

more efficient than linked list or arrays implementation of sorted list!

Page 61: Balanced Tree (AVL Tree & Red-Black Tree)

Red-Black Tree Application• Completely Fair Scheduler in Linux Kernel.

• Computational Geometry Data structures.

• To keep track of the virtual memory segments for a process - the start address of the range serves as the key.

• Red–black trees are also particularly valuable in functional programming.

Page 62: Balanced Tree (AVL Tree & Red-Black Tree)

ComparisonFor small data :• Insert: RB tree will be faster because on average it uses less rotation.• Lookup: AVL tree is faster, because it has less depth.• Delete: RB tree is faster for it’s runtime.

For large data :• Insert: AVL tree is faster, because it maintains O(log n) which is better than RB

tree.• Lookup: AVL tree is faster. (same as in small data case)• Delete: AVL tree is faster on average, but in worst case RB tree is faster.

Page 63: Balanced Tree (AVL Tree & Red-Black Tree)

Thank You