Data Structures Balanced Binary Search Tree Virendra Singh - - PowerPoint PPT Presentation

data structures
SMART_READER_LITE
LIVE PREVIEW

Data Structures Balanced Binary Search Tree Virendra Singh - - PowerPoint PPT Presentation

Data Structures Balanced Binary Search Tree Virendra Singh Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay http://www.ee.iitb.ac.in/~viren/


slide-1
SLIDE 1

Data Structures

Balanced Binary Search Tree

Virendra Singh

Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay

http://www.ee.iitb.ac.in/~viren/ E-mail: viren@ee.iitb.ac.in

EE-717/453:Advance Computing for Electrical Engineers

Lecture 8 (15 Aug 2013)

slide-2
SLIDE 2

Red Black Trees

Colored Nodes Definition

  • Binary search tree.
  • Each node is colored red or black.
  • Root and all external nodes are black.
  • No root-to-external-node path has two

consecutive red nodes.

  • All root-to-external-node paths have the

same number of black nodes

15 Aug 2013 EE-717/EE-453@IITB 2

slide-3
SLIDE 3

Example Red Black Tree

10 7 8 1 5 30 40 20 25 35 45 60 3

slide-4
SLIDE 4

Red Black Trees

Colored Edges Definition

  • Binary search tree.
  • Child pointers are colored red or black.
  • Pointer to an external node is black.
  • No root to external node path has two

consecutive red pointers.

  • Every root to external node path has the

same number of black pointers.

15 Aug 2013 EE-717/EE-453@IITB 4

slide-5
SLIDE 5

Example Red Black Tree

10 7 8 1 5 30 40 20 25 35 45 60 3

slide-6
SLIDE 6

Properties

  • The height of a red black tree that has n

(internal) nodes is between log2(n+1) and 2log2(n+1).

15 Aug 2013 EE-717/EE-453@IITB 6

slide-7
SLIDE 7

Properties

  • Start with a red black tree whose height is

h; collapse all red nodes into their parent black nodes to get a tree whose node- degrees are between 2 and 4, height is >= h/2, and all external nodes are at the same level.

15 Aug 2013 EE-717/EE-453@IITB 7

slide-8
SLIDE 8

1 7 8 1 5 3 4 2 2 5 3 5 4 5 6 3

Properties

slide-9
SLIDE 9

Properties

  • Let h’>= h/2 be the height of the collapsed

tree.

  • In worst-case, all internal nodes of

collapsed tree have degree 2.

  • Number of internal nodes in collapsed tree

>= 2h’-1.

  • So, n >= 2h’-1
  • So, h <= 2 log2 (n + 1)

15 Aug 2013 EE-717/EE-453@IITB 9

slide-10
SLIDE 10

Properties

  • At most 1 rotation and O(log n) color flips

per insert/delete.

15 Aug 2013 EE-717/EE-453@IITB 10

slide-11
SLIDE 11

Insert

  • New pair is placed in a new node, which is

inserted into the red-black tree.

  • New node color options.

– Black node => one root-to-external-node path

has an extra black node (black pointer).

  • Hard to remedy.

– Red node => one root-to-external-node path

may have two consecutive red nodes (pointers).

  • May be remedied by color flips and/or a rotation.

15 Aug 2013 EE-717/EE-453@IITB 11

slide-12
SLIDE 12

Classification of 2 Red Nodes/Pointers

  • XYz

– X => relationship between gp and pp.

  • pp left child of gp => X = L.

– Y => relationship between pp and p.

  • p right child of pp => Y = R.

– z = b (black) if d = null or a black node. – z = r (red) if d is a red node.

L L b

a b c d gp pp p

15 Aug 2013 EE-717/EE-453@IITB 12

slide-13
SLIDE 13

XYr

  • Color flip.

a b c d gp pp p a b c d gp pp p

  • Move p, pp, and gp up two levels.
  • Continue rebalancing if necessary.

15 Aug 2013 EE-717/EE-453@IITB 13

slide-14
SLIDE 14

LLb

  • Rotate.
  • Done!
  • Same as LL rotation of AVL tree.

y x

a b

z

c d a b c d gp pp p

x y z

15 Aug 2013 EE-717/EE-453@IITB 14

slide-15
SLIDE 15

LRb

  • Rotate.
  • Done!
  • Same as LR rotation of AVL tree.
  • RRb and RLb are symmetric.

y x

a b

z

c d b c a d gp pp p

y x z

15 Aug 2013 EE-717/EE-453@IITB 15

slide-16
SLIDE 16

Delete

  • Delete as for unbalanced binary search

tree.

  • If red node deleted, no rebalancing

needed.

  • If black node deleted, a subtree

becomes one black pointer (node) deficient.

15 Aug 2013 EE-717/EE-453@IITB 16

slide-17
SLIDE 17

Delete A Black Leaf

1 7 8 1 5 3 4 2 2 5 3 5 4 5 6 3

  • Delete 8.
slide-18
SLIDE 18

Delete A Black Leaf

y

  • y is root of deficient subtree.
  • py is parent of y.

1 7 1 5 3 4 2 2 5 3 5 4 5 6 3

py

slide-19
SLIDE 19

Delete A Black Degree 1 Node

1 7 8 1 5

30 40 20 25 35 45 60

3

  • Delete 45.

y

  • y is root of deficient subtree.

py

slide-20
SLIDE 20

Delete A Black Degree 2 Node

10 7 8 1 5 30 40 20 25 35 45 60 3

  • Not possible, degree 2 nodes are never deleted.
slide-21
SLIDE 21

Rebalancing Strategy

  • If y is a red node, make it black.

10 7 8 1 5 30 40 20 25 35 45 60 3 y py

15 Aug 2013 EE-717/EE-453@IITB 21

slide-22
SLIDE 22

Rebalancing Strategy

  • Now, no subtree is deficient. Done!

60 10 7 8 1 5 30 40 20 25 35 45 3 y py

15 Aug 2013 EE-717/EE-453@IITB 22

slide-23
SLIDE 23

Rebalancing Strategy

  • y is a black root (there is no py).
  • Entire tree is deficient. Done!

60 10 7 8 1 5 30 40 20 25 35 45 3 y

15 Aug 2013 EE-717/EE-453@IITB 23

slide-24
SLIDE 24

Rebalancing Strategy

  • y is black but not the root (there is a py).
  • Xcn
  • y is right child of py => X = R.
  • Pointer to v is black => c = b.
  • v has 1 red child => n = 1.

a b y py v

15 Aug 2013 EE-717/EE-453@IITB 24

slide-25
SLIDE 25

Rb0 (case 1)

  • Color change.
  • Now, py is root of deficient subtree.
  • Continue!

a b y py v y a b py v

15 Aug 2013 EE-717/EE-453@IITB 25

slide-26
SLIDE 26

Rb0 (case 2)

  • Color change.
  • Deficiency

eliminated.

  • Done!

a b y py v y a b py v

15 Aug 2013 EE-717/EE-453@IITB 26

slide-27
SLIDE 27

Rb1 (case 1)

  • LL rotation.
  • Deficiency

eliminated.

  • Done!

a b y py v a b y v py

15 Aug 2013 EE-717/EE-453@IITB 27

slide-28
SLIDE 28

Rb1 (case 2)

  • LR rotation.
  • Deficiency

eliminated.

  • Done!

a y py v b c w c y w py a b v

15 Aug 2013 EE-717/EE-453@IITB 28

slide-29
SLIDE 29

Rb2

  • LR rotation.
  • Deficiency eliminated.
  • Done!

a y py v b c w c y w py a b v

15 Aug 2013 EE-717/EE-453@IITB 29

slide-30
SLIDE 30

Rr(n)

  • n = # of red children of v’s right child w.

a y py v b c w

Rr( 2)

15 Aug 2013 EE-717/EE-453@IITB 30

slide-31
SLIDE 31

Rr(0)

  • LL rotation.
  • Done!

a b y py v a b y v py

15 Aug 2013 EE-717/EE-453@IITB 31

slide-32
SLIDE 32

Rr(1) (case 1)

  • LR rotation.
  • Deficiency eliminated.
  • Done!

a y py v b w c y c w py a v b

15 Aug 2013 EE-717/EE-453@IITB 32

slide-33
SLIDE 33

Rr(1) (case 2)

  • Rotation.
  • Deficiency eliminated.
  • Done!

a y py v b w c d x y d x py a v b c w

15 Aug 2013 EE-717/EE-453@IITB 33

slide-34
SLIDE 34

Rr(2)

  • Rotation.
  • Deficiency

eliminated.

  • Done!

a y py v b w c d x d y x py a v b c w

15 Aug 2013 EE-717/EE-453@IITB 34

slide-35
SLIDE 35

Red Black Tree

  • The height of a red black tree that has n

(internal) nodes is between log2(n+1) and 2log2(n+1).

  • java.util.TreeMap => red black tree

15 Aug 2013 EE-717/EE-453@IITB 35

slide-36
SLIDE 36

B-Tree

15 Aug 2013 EE-717/EE-453@IITB 36

slide-37
SLIDE 37

B-Trees

  • Large degree B-trees used to represent very

large dictionaries that reside on disk.

  • Smaller degree B-trees used for internal-

memory dictionaries to overcome cache- miss penalties.

15 Aug 2013 EE-717/EE-453@IITB 37

slide-38
SLIDE 38

AVL Trees

  • n = 230 = 109 (approx).
  • 30 <= height <= 43.
  • When the AVL tree resides on a disk, up

to 43 disk access are made for a search.

  • This takes up to (approx) 4 seconds.
  • Not acceptable.

15 Aug 2013 EE-717/EE-453@IITB 38

slide-39
SLIDE 39

Red-Black Trees

  • n = 230 = 109 (approx).
  • 30 <= height <= 60.
  • When the red-black tree resides on a disk,

up to 60 disk access are made for a search.

  • This takes up to (approx) 6 seconds.
  • Not acceptable.

15 Aug 2013 EE-717/EE-453@IITB 39

slide-40
SLIDE 40

m-way Search Trees

  • Each node has up to m – 1 pairs and m

children.

  • m = 2 => binary search tree.

15 Aug 2013 EE-717/EE-453@IITB 40

slide-41
SLIDE 41

4-Way Search Tree

10 30 35

k < 10 10 < k < 30 30 < k < 35 k > 35

15 Aug 2013 EE-717/EE-453@IITB 41

slide-42
SLIDE 42

Maximum # Of Pairs

  • Happens when all internal nodes are m-

nodes.

  • Full degree m tree.
  • # of nodes = 1 + m + m2 + m3 + … + mh-1

= (mh – 1)/(m – 1).

  • Each node has m – 1 pairs.
  • So, # of pairs = mh – 1.

15 Aug 2013 EE-717/EE-453@IITB 42

slide-43
SLIDE 43

Capacity Of m-Way Search Tree

m = 2 m = 200 h = 3 7 8 * 10

6 - 1

h = 5 31 3.2 * 10

11 - 1

h = 7 127 1.28 * 10

16 - 1

15 Aug 2013 EE-717/EE-453@IITB 43

slide-44
SLIDE 44

Definition Of B-Tree

  • Definition assumes external nodes

(extended m-way search tree).

  • B-tree of order m.

– m-way search tree. – Not empty => root has at least 2 children. – Remaining internal nodes (if any) have at

least ceil(m/2) children.

– External (or failure) nodes on same level.

15 Aug 2013 EE-717/EE-453@IITB 44

slide-45
SLIDE 45

2-3 And 2-3-4 Trees

  • B-tree of order m.

– m-way search tree. – Not empty => root has at least 2 children. – Remaining internal nodes (if any) have at

least ceil(m/2) children.

– External (or failure) nodes on same level.

  • 2-3 tree is B-tree of order 3.
  • 2-3-4 tree is B-tree of order 4.

15 Aug 2013 EE-717/EE-453@IITB 45

slide-46
SLIDE 46

B-Trees Of Order 5 And 2

  • B-tree of order m.

– m-way search tree. – Not empty => root has at least 2 children. – Remaining internal nodes (if any) have at

least ceil(m/2) children.

– External (or failure) nodes on same level.

  • B-tree of order 5 is 3-4-5 tree (root may be 2-node

though).

  • B-tree of order 2 is full binary tree.

15 Aug 2013 EE-717/EE-453@IITB 46

slide-47
SLIDE 47

Choice Of m

  • Worst-case search time.

– (time to fetch a node + time to search node) *

height

– (a + b*m + c * log2m) * h

where a, b and c are constants.

m search time

50 400

15 Aug 2013 EE-717/EE-453@IITB 47

slide-48
SLIDE 48

B+-Trees

  • Same structure as B-trees.
  • Dictionary pairs are in leaves only. Leaves

form a doubly-linked list.

  • Remaining nodes have following structure:

j a0 k1 a1 k2 a2 … kj aj

  • j = number of keys in node.
  • ai is a pointer to a subtree.
  • ki <= smallest key in subtree ai and > largest

in ai-1.

15 Aug 2013 EE-717/EE-453@IITB 48