data structures
play

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:


  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)

  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

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

  4. Splay Node – search(k) 20 10 40 6 15 30 25 2 8 • 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. 22 Aug 2013 EE-717/EE-453@IITB 4

  5. Splay Node – insert(newPair) 2 0 1 4 0 0 6 1 3 5 0 2 2 8 5 • 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. 22 Aug 2013 EE-717/EE-453@IITB 5

  6. Splay Node – delete(k) 20 10 40 6 15 30 25 2 8 • 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. 22 Aug 2013 EE-717/EE-453@IITB 6

  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

  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. q p a p q c b c a b • q right child of p is symmetric. 22 Aug 2013 EE-717/EE-453@IITB 9

  9. Splay Step • If q is at a level > 2, do a two-level move and continue the splay operation. q gp a p p d b gp q c c d a b • q right child of right child of gp is symmetric. 22 Aug 2013 EE-717/EE-453@IITB 10

  10. 2-Level Move (case 2) q gp p gp p d a b c d a q b c • q left child of right child of gp is symmetric. 22 Aug 2013 EE-717/EE-453@IITB 11

  11. Per Operation Actual Complexity • Start with an empty splay tree and insert pairs with keys 1, 2, 3, …, in this order. 1 1 2 2 1 22 Aug 2013 EE-717/EE-453@IITB 12

  12. Per Operation Actual Complexity • Start with an empty splay tree and insert pairs with keys 1, 2, 3, …, in this order. 3 3 2 2 2 4 1 3 1 1 22 Aug 2013 EE-717/EE-453@IITB 13

  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

  14. Binary Trie • Information Retrieval. • At most one key comparison per operation. • 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

  15. Example 0 1 0 0 1 1100 0 1 0 0001 0011 0 1 1000 1001 At most one key comparison for a search . 22 Aug 2013 EE-717/EE-453@IITB 16

  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

  17. Example 0 null 1 0 00 01100 10 11111 0 0 0000 001 1000 101 1 00100 001100 At most one key comparison for a search . 22 Aug 2013 EE-717/EE-453@IITB 18

  18. Fixed Length Insert 0 1 0 0 1 1 1100 0111 0 1 0 0001 0011 0 1 1000 1001 Zero compares. Insert 0111. 22 Aug 2013 EE-717/EE-453@IITB 19

  19. Fixed Length Insert 0 1 0 0 1 1 1100 0111 0 1 0 0001 0011 0 1 1000 1001 Insert 1101. 22 Aug 2013 EE-717/EE-453@IITB 20

  20. Fixed Length Insert 0 1 0 0 1 1 0111 0 1 0 0 0001 0011 0 1 0 1000 1001 1100 Insert 1101. 22 Aug 2013 EE-717/EE-453@IITB 21

  21. Fixed Length Insert 0 1 0 0 1 1 0111 0 1 0 0 0001 0011 0 1 0 1 1000 1001 1100 1101 Insert 1101. One compare. 22 Aug 2013 EE-717/EE-453@IITB 22

  22. Fixed Length Delete 0 1 0 0 1 1 0111 0 1 0 0 0001 0011 0 1 0 1 1000 1001 1100 1101 Delete 0111. 22 Aug 2013 EE-717/EE-453@IITB 23

  23. Fixed Length Delete 0 1 0 0 1 0 1 0 0 0001 0011 0 1 0 1 1000 1001 1100 1101 Delete 0111. One compare. 22 Aug 2013 EE-717/EE-453@IITB 24

  24. Fixed Length Delete 0 1 0 0 1 0 1 0 0 0001 0011 0 1 0 1 1000 1001 1100 1101 Delete 1100. 22 Aug 2013 EE-717/EE-453@IITB 25

  25. Fixed Length Delete 0 1 0 0 1 0 1 0 0 0001 0011 0 1 1 1000 1001 1101 Delete 1100. 22 Aug 2013 EE-717/EE-453@IITB 26

  26. Fixed Length Delete 1101 0 1 0 0 1 0 1 0 0 0001 0011 0 1 1000 1001 Delete 1100. 22 Aug 2013 EE-717/EE-453@IITB 27

  27. Fixed Length Delete 1101 0 1 0 0 1 0 1 0 0001 0011 0 1 1000 1001 Delete 1100. 22 Aug 2013 EE-717/EE-453@IITB 28

  28. Fixed Length Delete 0 1 0 0 1 1101 0 1 0 0001 0011 0 1 1000 1001 Delete 1100. One compare. 22 Aug 2013 EE-717/EE-453@IITB 29

  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

  30. Binary Trie 1 0 1 2 0 0 1 3 0 1 0 0 4 4 0001 0011 0 1 0 1 1000 1001 1100 1101 bit# field shown in black outside branch node. 22 Aug 2013 EE-717/EE-453@IITB 31

  31. Compressed Binary Trie 1 0 1 3 2 0 1 0 1 0001 0011 4 4 0 1 0 1 1000 1001 1100 1101 bit# field shown in black outside branch node. 22 Aug 2013 EE-717/EE-453@IITB 32

  32. Compressed Binary Trie 1 0 1 3 2 0 1 0 1 0001 0011 4 4 0 1 0 1 1000 1001 1100 1101 #branch nodes = n – 1. 22 Aug 2013 EE-717/EE-453@IITB 33

  33. Insert 1 0 1 3 2 0 1 0 1 0001 0011 4 4 0 1 0 1 1000 1001 1100 1101 Insert 0010. 22 Aug 2013 EE-717/EE-453@IITB 34

  34. Insert 1 0 1 3 2 0 1 0 1 0001 4 4 0 1 4 0010 0 1 0011 0 1 1000 1001 1100 1101 Insert 0100. 22 Aug 2013 EE-717/EE-453@IITB 35

  35. Insert 1 0 1 2 2 1 0 1 0 3 0100 4 4 0 1 0 1 0 1 0001 4 1000 1001 1100 0 1 1101 0010 0011 22 Aug 2013 EE-717/EE-453@IITB 36

  36. Delete 1 0 1 2 2 1 0 1 0 3 0100 4 4 0 1 0 1 0 1 0001 4 1000 1001 1100 1101 0 1 Delete 0010. 0010 0011 22 Aug 2013 EE-717/EE-453@IITB 37

  37. Delete 1 0 1 2 2 1 0 1 0 3 0100 4 4 0 1 0 1 0 1 0001 0011 1000 1001 1100 1101 Delete 1001. 22 Aug 2013 EE-717/EE-453@IITB 38

  38. Delete 1 0 1 2 2 1 0 1 0 3 0100 4 1000 0 1 0 1 0001 0011 1100 1101 22 Aug 2013 EE-717/EE-453@IITB 39

  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. u v • Directed edge has an orientation (u,v). 22 Aug 2013 EE-717/EE-453@IITB 40

Recommend


More recommend