Data Structures Splay Tree & Graph Virendra Singh Associate - - PowerPoint PPT Presentation

data structures
SMART_READER_LITE
LIVE PREVIEW

Data Structures Splay Tree & Graph Virendra Singh Associate - - PowerPoint PPT Presentation

Data Structures Splay Tree & Graph 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:


slide-1
SLIDE 1

Data Structures

Splay Tree & Graph

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 10 (22 Aug 2013)

slide-2
SLIDE 2

Splay Trees

  • Binary search trees.
  • Search, insert, delete, and split have amortized

complexity O(log n) & actual complexity O(n).

  • Two varieties.

– Bottom up. – Top down.

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

slide-3
SLIDE 3

Bottom-Up Splay Trees

  • Search, insert, delete, and join are done as in an

unbalanced binary search tree.

  • Search, insert, and delete are followed by a splay
  • peration that begins at a splay node.
  • When the splay operation completes, the splay

node has become the tree root.

  • Join requires no splay (or, a null splay is done).
  • For the split operation, the splay is done in the

middle (rather than end) of the operation.

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

slide-4
SLIDE 4

Splay Node – search(k)

  • If there is a pair whose key is k, the node containing

this pair is the splay node.

  • Otherwise, the parent of the external node where

the search terminates is the splay node.

20 10 6 2 8 15 40 30 25

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

slide-5
SLIDE 5

Splay Node – insert(newPair)

  • If there is already a pair whose key is newPair.key,

the node containing this pair is the splay node.

  • Otherwise, the newly inserted node is the splay

node.

2 1 6 2 8 1 5 4 3 2 5

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

slide-6
SLIDE 6

Splay Node – delete(k)

  • If there is a pair whose key is k, the parent of the

node that is physically deleted from the tree is the splay node.

  • Otherwise, the parent of the external node where

the search terminates is the splay node.

20 10 6 2 8 15 40 30 25

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

slide-7
SLIDE 7

Splay

  • Let q be the splay node.
  • q is moved up the tree using a series of splay

steps.

  • In a splay step, the node q moves up the tree by

0, 1, or 2 levels.

  • Every splay step, except possibly the last one,

moves q two levels up.

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

slide-8
SLIDE 8

Splay Step

  • If q = null or q is the root, do nothing

(splay is over).

  • If q is at level 2, do a one-level move

and terminate the splay operation.

p q a b c b c a q p

  • q right child of p is symmetric.

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

slide-9
SLIDE 9

Splay Step

  • If q is at a level > 2, do a two-level

move and continue the splay operation.

  • q right child of right child of gp is symmetric.

p q a b c gp d c d b p gp q a

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

slide-10
SLIDE 10

2-Level Move (case 2)

  • q left child of right child of gp is symmetric.

p q b c a gp d a c b gp p q d

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

slide-11
SLIDE 11

Per Operation Actual Complexity

  • Start with an empty splay tree and

insert pairs with keys 1, 2, 3, …, in this

  • rder.

1 1 2 1 2

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

slide-12
SLIDE 12

Per Operation Actual Complexity

  • Start with an empty splay tree and

insert pairs with keys 1, 2, 3, …, in this

  • rder.

1 2 3 1 2 3 1 2 3 4

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

slide-13
SLIDE 13

Per Operation Actual Complexity

  • Worst-case height = n.
  • Actual complexity of search, insert,

delete, and split is O(n).

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

slide-14
SLIDE 14

Binary Trie

  • Information Retrieval.
  • At most one key comparison per
  • peration.
  • Fixed length keys.

– Branch nodes.

  • Left and right child pointers.
  • No data field(s).

– Element nodes.

  • No child pointers.
  • Data field to hold dictionary pair.

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

slide-15
SLIDE 15

Example

At most one key comparison for a search.

0001 0011 1100 1000 1001 1 1 1 1

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

slide-16
SLIDE 16

Variable Key Length

  • Left and right child fields.
  • Left and right pair fields.

– Left pair is pair whose key terminates at

root of left subtree or the single pair that might otherwise be in the left subtree.

– Right pair is pair whose key terminates at

root of right subtree or the single pair that might otherwise be in the right subtree.

– Field is null otherwise.

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

slide-17
SLIDE 17

Example

At most one key comparison for a search.

1 null 00 01100 0000 001 10 11111 00100 001100 1000 101 1

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

slide-18
SLIDE 18

Fixed Length Insert

Insert 0111.

0001 0011 1100 1000 1001 1 1 1 1 0111 1

Zero compares.

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

slide-19
SLIDE 19

Fixed Length Insert

Insert 1101.

0001 0011 1100 1000 1001 1 1 1 1 0111 1

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

slide-20
SLIDE 20

Fixed Length Insert

Insert 1101.

1100 1 0001 0011 1000 1001 1 1 1 0111 1

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

slide-21
SLIDE 21

Fixed Length Insert

Insert 1101.

1 0001 0011 1000 1001 1 1 1 0111 1

One compare.

1100 1101 1

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

slide-22
SLIDE 22

Fixed Length Delete

Delete 0111.

1 0001 0011 1000 1001 1 1 1 0111 1 1100 1101 1

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

slide-23
SLIDE 23

Fixed Length Delete

Delete 0111. One compare.

1 0001 0011 1000 1001 1 1 1 1100 1101 1

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

slide-24
SLIDE 24

Fixed Length Delete

Delete 1100.

1 0001 0011 1000 1001 1 1 1 1100 1101 1

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

slide-25
SLIDE 25

Fixed Length Delete

Delete 1100.

1 0001 0011 1000 1001 1 1 1 1101 1

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

slide-26
SLIDE 26

Fixed Length Delete

Delete 1100.

1 0001 0011 1000 1001 1 1 1 1101

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

slide-27
SLIDE 27

Fixed Length Delete

Delete 1100.

1 0001 0011 1000 1001 1 1 1 1101

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

slide-28
SLIDE 28

Fixed Length Delete

Delete 1100. One compare.

1 0001 0011 1000 1001 1 1 1 1101

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

slide-29
SLIDE 29

Compressed Binary Tries

  • No branch node whose degree is 1.
  • Add a bit# field to each branch node.
  • bit# tells you which bit of the key to use to

decide whether to move to the left or right subtrie.

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

slide-30
SLIDE 30

Binary Trie

1 0001 0011 1000 1001 1 1 1 1100 1101 1 1 2 3 4 4

bit# field shown in black outside branch node.

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

slide-31
SLIDE 31

Compressed Binary Trie

1 0001 0011 1000 1001 1 1 1 1100 1101 1 1 2 3 4 4

bit# field shown in black outside branch node.

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

slide-32
SLIDE 32

Compressed Binary Trie

1 0001 0011 1000 1001 1 1 1 1100 1101 1 1 2 3 4 4

#branch nodes = n – 1.

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

slide-33
SLIDE 33

Insert

1 0001 0011 1000 1001 1 1 1 1100 1101 1 1 2 3 4 4

Insert 0010.

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

slide-34
SLIDE 34

Insert

Insert 0100.

1 0001 1000 1001 1 1 1 1100 1101 1 1 2 3 4 4 0010 0011 1 4

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

slide-35
SLIDE 35

Insert

1 0001 1000 1001 1 1 1 1100 1101 1 1 2 3 4 4 0010 0011 1 4 2 0100 1

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

slide-36
SLIDE 36

Delete

0001 1 1000 1001 1 1 1 1100 1101 1 1 2 3 4 4 0010 0011 1 4 2 0100 1

Delete 0010.

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

slide-37
SLIDE 37

Delete

0001 1 1000 1001 1 1 1 1100 1101 1 1 2 3 4 4 0011 2 0100 1

Delete 1001.

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

slide-38
SLIDE 38

Delete

0001 1 1000 1 1 1100 1101 1 1 2 3 4 0011 2 0100 1

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

slide-39
SLIDE 39

Graphs

  • G = (V,E)
  • V is the vertex set.
  • Vertices are also called nodes and

points.

  • E is the edge set.
  • Each edge connects two different

vertices.

  • Edges are also called arcs and lines.
  • Directed edge has an orientation (u,v).

u v

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