RED BLACK TREE

Post on 01-Jan-2016

28 views 0 download

description

RED BLACK TREE. Group Members. Bushra Shabbir SP10- BCS -020 Mehreen Arif SP10- BCS -040 Misbah Kiran SP10- BCS -042. Introduction. Binary Tree: is a tree structure in which each node has at most 2 children. - PowerPoint PPT Presentation

Transcript of RED BLACK TREE

REDRED BLACK TREE BLACK TREE

GroupGroup Members Members

► BushraBushra Shabbir SP10- Shabbir SP10-BCSBCS-020-020► MehreenMehreen Arif SP10- Arif SP10-BCSBCS-040-040► MisbahMisbah Kiran SP10- Kiran SP10-BCSBCS-042-042

IntroductionIntroduction

► Binary Tree:Binary Tree: is a tree structure in which each is a tree structure in which each node has at most 2 children.node has at most 2 children.

► Binary Search Tree:Binary Search Tree: Each node has a value, Each node has a value, where the left sub-tree node contains only where the left sub-tree node contains only values lesser or equal to it’s parent, and the values lesser or equal to it’s parent, and the right sub-tree contains nodes of greater or right sub-tree contains nodes of greater or equal value to it’s parents.equal value to it’s parents.

Binary Search TreeBinary Search Tree

► In the case of a Binary Search Tree In the case of a Binary Search Tree search, search, insert and deleteinsert and delete all have a running time in all have a running time in the order of O (log n) for n items in best the order of O (log n) for n items in best case.case.

► A Binary Search tree is not self balancing, so A Binary Search tree is not self balancing, so in worst case its running time is O (n). As in in worst case its running time is O (n). As in case of first 5 integers: case of first 5 integers:

1

2

3

4

5

Balanced Search TreeBalanced Search Tree

►Balanced MeansBalanced Means:: the longest path is the longest path is no more then twice as long as the no more then twice as long as the shortest path.shortest path.

11

8

1

2

7 15

12

5

Red Red Black TreeBlack Tree

► A A redred black tree black tree is a type of binary search is a type of binary search tree that is self balancing. Red-black trees tree that is self balancing. Red-black trees aim to keep the tree balanced byaim to keep the tree balanced by

► Coloring each node in the tree with either Coloring each node in the tree with either red or blackred or black

► Preserving a set of properties that Preserving a set of properties that guarantee that the deepest path in the tree guarantee that the deepest path in the tree is not longer than twice the shortest one.is not longer than twice the shortest one.

PropertiesProperties►A A redred-black tree has the following -black tree has the following

properties:properties: Every node is colored eitherEvery node is colored either Red Red or Black.or Black. By convention, the root is always BlackBy convention, the root is always Black Each NULL pointer is considered to be a Black Each NULL pointer is considered to be a Black

“node”.“node”. If a node is If a node is RedRed, then both of its children are , then both of its children are

Black. (If not then Black. (If not then Red ViolationRed Violation Occurs) Occurs) Every path from a node to a NULL contains the Every path from a node to a NULL contains the

same number of Black nodes. (If not then Black same number of Black nodes. (If not then Black Violation occurs)Violation occurs)

Height of Height of RRBTBT

►Height of a node:Height of a node: H H ((xx) = number of edges in a longest path ) = number of edges in a longest path

to a leaf.to a leaf.

►Black-height of a node Black-height of a node xx, , bh bh ((xx):): Bh Bh ((xx)) = number of black nodes (including = number of black nodes (including

nil)nil)on the path from on the path from x x to leaf, not counting to leaf, not counting xx..

►Black-height of a red-black tree is the Black-height of a red-black tree is the black-height of its root.black-height of its root.

RotationRotation

► Rotation needs to be talked about before Insert and Delete.

► Operations (Tree-Insert and Tree-Delete) can destroy the properties of a red black tree. To restore sometimes pointers in the tree need to be change. This is called “Rotation”.

► There are 2 types of rotations, left and right.

Note: Sometimes to restore the red black properties,

some nodes have to have their colors changed from red to black or black to red.

Left RotationLeft Rotation

Here are the steps involved in for left rotation:Here are the steps involved in for left rotation:► Assume node x is the parent and node y is a Assume node x is the parent and node y is a

non-leaf right child.non-leaf right child.► Let y be the parent and x be its left child.Let y be the parent and x be its left child.

►Let y’s left child be x’s right child.Let y’s left child be x’s right child.

W

z Y

X

V W

X

Z

Y

V

Right RotationRight Rotation

Here are the steps involved in for right Here are the steps involved in for right rotation:rotation:

► Assume node x is the parent and node y is a Assume node x is the parent and node y is a non-leaf left child.non-leaf left child.

► Let y be the parent and x be its right child.Let y be the parent and x be its right child.

►Let y’s right child be x’s left child.Let y’s right child be x’s left child.

W

Y

Z

X

V

W

z X

Y

V

InsertionInsertion

Insertion can be done in the order of O (log n). It follows closely the insertion into a binary search tree, except

► The Node inserted is red► A restore function must be called to fix

red-black properties that might be violated.

13

Example of Inserting Sorted Example of Inserting Sorted NumbersNumbers

►1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 1110 11 1

Insert 1. A leaf so

red. Realize it is

root so recolor

to black.1

14

Insert 2Insert 2

1

2

make 2 red. Parent

is black so done.

15

Insert 3Insert 3

1

2

3

Insert 3. Parent is red. Parent's sibling is black(null) 3 is outside relative to grandparent. Rotate

parent and grandparent2

1 3

16

Insert 4Insert 4

2

1 3

On way down see2 with 2 red children.Recolor 2 red andchildren black.Realize 2 is rootso color back to black

2

1 3

4

When adding 4parent is blackso done.

17

Insert 5Insert 5

2

1 3

4

5

5's parent is red.Parent's sibling isblack (null). 5 isoutside relative tograndparent (3) so rotateparent and grandparent thenrecolor

18

Finish insert of 5Finish insert of 5

2

1 4

3 5

19

Insert 6Insert 6

2

1 4

3 5

On way down see4 with 2 redchildren. Make4 red and childrenblack. 4's parent isblack so no problem.

20

Finishing insert of 6Finishing insert of 6

2

1 4

3 5

6

6's parent is blackso done.

21

Insert 7Insert 7

2

1 4

3 5

6

7

7's parent is red.Parent's sibling isblack (null). 7 isoutside relative tograndparent (5) so rotate parent and grandparent then recolor

22

Finish insert of 7Finish insert of 7

2

1 4

3 6

5 7

23

Insert 8Insert 8

2

1 4

3 6

5 7

On way down see 6with 2 red children.Make 6 red andchildren black. Thiscreates a problembecause 6's parent, 4, isalso red. Must performrotation.

24

Still Inserting 8Still Inserting 8

2

1 4

3 6

5 7

Re-colored nowneed torotate

25

Finish inserting 8Finish inserting 8

4

2

3

6

5 71

8

Again Re-colored

26

Insert 9Insert 9

4

2

3

6

5 71

8

9

On way down see 4 has two red childrenso recolor 4 red and children black. Realize 4 is the root so recolor black

27

Finish Inserting 9Finish Inserting 9

4

2

3

6

5 81

7 9After rotations and re-coloring

28

Insert 10Insert 10

4

2

3

6

5 81

7 9On way down see 8 has twored children so change 8 tored and children black 10

29

Insert 11Insert 11

4

2

3

6

5 81

7 9

10

11

Again a rotation isneeded.

30

Finish inserting 11Finish inserting 11

4

2

3

6

5 81

7 10

9 11

DeletionDeletion

► Deletion, like insertion, should preserve all Deletion, like insertion, should preserve all the RBT properties.the RBT properties.

► The properties that may be violated The properties that may be violated depends on the color of the deleted node.depends on the color of the deleted node. RedRed – OK. – OK. Why?Why? Black?Black? Black ViolationBlack Violation

► Steps:Steps: Do regular BST deletion.Do regular BST deletion. Fix any violations of Fix any violations of RRBT properties that BT properties that

may result.may result.