4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different...

44

Transcript of 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different...

Page 1: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps
Page 2: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Comp215: Tree Deletion + TreapsDan S. Wallach (Rice University)

Copyright Ⓒ 2015, Dan S. Wallach. All rights reserved.

Page 3: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletionInsertion: we allocated new nodes from top to bottom - O(log n)Can we do the same for removing a node?

5

92

4 7

3 86

Page 4: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletion, no mutation

Page 5: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(4)Easy case: if the value we want to remove is to the leftreturn new Tree<>(value, left.remove(deadValue), right);

tree

5

92

4 7

3 86

Page 6: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(4)Easy case: if the value we want to remove is to the rightreturn new Tree<>(value, left, right.remove(deadValue));

tree 5

92

4 7

3 86

Page 7: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(4)Okay, we found it. Since it’s empty on one side, just return the other.if(left.empty()) return right;if(right.empty()) return left;

tree5

92

4 7

3 86

Page 8: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(5)But what if both children are non-empty?Goal: reduce to a smaller problem then continue recursively.

tree

5

92

4 7

3 86

Page 9: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree rotationTree<T> rotateRight() {

if(left.empty()) return this;return new Tree<>(left.getValue(), left.getLeft(), new Tree<>(value, left.getRight(), right));

}

5

2

A B

C

2

5A

B C

Page 10: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(5)But what if both children are non-empty?Rotate and recurse!return rotateRight().remove(deadValue);

tree

5

92

4 7

3 86

Page 11: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(5)But what if both children are non-empty?Rotate and recurse!return rotateRight().remove(deadValue);

tree.rotateRight()

5

92

4 7

3 86

2

5

Page 12: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(5)But what if both children are non-empty?Rotate and recurse!return rotateRight().remove(deadValue);

tree.rotateRight()

9

4 7

3 86

2

5

Page 13: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(5)But what if both children are non-empty?Rotate and recurse!return rotateRight().remove(deadValue); tree.remove(5)

9

4 7

3 86

2

5

Page 14: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(5)But what if both children are non-empty?Rotate and recurse!return rotateRight().remove(deadValue); tree.rotateRight()

9

7

3 86

2

4

5

Page 15: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(5)But what if both children are non-empty?Rotate and recurse!return rotateRight().remove(deadValue);

tree.remove(5)

9

7

3 86

2

4

5

Page 16: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree deletiontree.remove(5)But what if both children are non-empty?Rotate and recurse!

O(log n) time, O(log n) new nodesAnd original tree still there if you kept a pointer

Because no mutation!

9

7

3 86

2

4

Page 17: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree balanceWhat if we inserted numbers in sorted order?ITree<Integer> tree = Tree.of(1,2,3,4,5,6,7,8,9,10);

We won’t get a very pretty tree: O(n) worst case behavior.

12

34

56

78

Page 18: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Solution: “balanced” treesMany, many different algorithms for doing this (AVL, red-black, etc.)We’re going to learn about treaps [Aragon & Seidel, 1989](Then-sophomore Wallach had Seidel as his Comp215-equiv. professor in 1990.)You’ll implement it for this week’s assignment, so pay attention.

Treap = tree + heapEvery node has a random integer (“priority” or “heapVal”), created when inserted.

Tree property: nodes to the left are less-than current node; nodes to the right are greater-than (recursively)Heap property: current node’s priority is less than left and right’s priority (recursively)

More on heaps / priority queues on Friday. Enough to get you started today.

Page 19: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Tree vs. heap propertyTree value (yellow), heap value / priority (blue)

5

92

4 7

3 8

4

7

12

17

14

22

38

Page 20: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertionFirst, insert like it’s a normal tree (inserting “6” here)Note that we need to copy the priorities as we allocate new nodes

5

92

4 7

3 8

4

7

12

17

14

22

38

Page 21: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertionFirst, insert like it’s a normal tree (inserting “6” here)Note that we need to copy the priorities as we allocate new nodes

5

92

4 7

3 8

5

???4

7

12

17

14

22

38

4

Page 22: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertionFirst, insert like it’s a normal tree (inserting “6” here)Note that we need to copy the priorities as we allocate new nodes

5

92

4 7

3 8

5

9

???

4

7

12

17

14

22

38

4

14

Page 23: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertionFirst, insert like it’s a normal tree (inserting “6” here)Note that we need to copy the priorities as we allocate new nodes

5

92

4 7

3 8

5

9

7

???

4

7

12

17

14

22

38

4

14

22

Page 24: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertionFirst, insert like it’s a normal tree (inserting “6” here)Note that we need to copy the priorities as we allocate new nodes

5

92

4 7

3 8

5

9

7

6

4

7

12

17

14

22

38

4

14

22

Page 25: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertionFirst, insert like it’s a normal tree (inserting “6” here)Note that we need to copy the priorities as we allocate new nodes

2

4

3 8

5

9

7

6

7

12

17 38

4

14

22

Page 26: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertionWe’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

2

4

3 8

5

???9

???7

???6

7

12

17 8 38

4

14

22

Page 27: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertionWe’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

2

4

3 8

5

???9

???7

???6

7

12

17 8 38

4

14

22treap

Page 28: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertion

2

4

3 8

5

???9

???7

???6

7

12

17 8 38

4

14

22

treap

Is treap.priority < treap.left.priority and < treap.right.priority?

We’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

Page 29: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertion

2

4

3 8

5

???9

???7

???6

7

12

17 8 38

4

14

22

treap

No! left priority is smallest.Is treap.priority < treap.left.priority and < treap.right.priority?

We’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

Page 30: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

We’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

Treap insertion

2

4

3

5

7

12

17 8 38

4

treap

Rotate right to get smallest on top.6 8

7 22

9 14

Page 31: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

We’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

Treap insertion

2

4

3

5

7

12

17 8 38

4

treap

Rotate right to get smallest on top.6 8

7 22

9 14

6 8

7 22

Page 32: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

We’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

Treap insertion

2

4

3

5

7

12

17 8 38

4

treap

Rotate right to get smallest on top.

9 14

6 8

7 22

Page 33: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertion

2

4

3

5

7

12

17 8 38

4

treap

Rotate right (again) to get smallest on top.

9 14

6 8

7 22

We’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

Page 34: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertion

2

4

3

5

7

12

17 8 38

4

treap

Rotate right (again) to get smallest on top.

9 14

6 8

7 22

6 8

9 14

We’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

Page 35: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertion

2

4

3

5

7

12

17 8 38

4

treap

Rotate right (again) to get smallest on top.

7 22

6 8

9 14

We’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

Page 36: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

We’ve satisfied the “tree” property, but not the “heap” propertyThe newly inserted node gets a random priority; we must “heapify”.First insert (recursively), then “heapify” on the way out.

Treap insertion

2

4

3

5

7

12

17 8 38

4

treap

Heap property satisfied. We’re done.

7 22

6 8

9 14

Page 37: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap insertionBut that doesn’t look particularly balanced!Yes, but let’s talk about the odds.

2

4

3

5

7

12

17 8 38

4

treap

7 22

6 8

9 14

Page 38: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap: odds of degenerate caseDegenerate case: start with a “linked list” treeMeasure odds that inserting new value preserves the linked list propertyAssume all numbers selected at random. Linked list is highly unlikely.

12

34

56

78

h1

h2

h3

h4

h5

h6

h7

h8

P(hn > hn�1) = 2�(n�1)

Page 39: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap: expected # of rotations (in general)P(1 rotation) = 0.5

P(2 rotations) = 0.25

P(3 rotations) = 0.125

lim

n!•SnP(n rotations) =

lim

n!•Snn2

�n =

2 rotations =

O(1)

Page 40: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Implementing edu.rice.tree.TreapYou need a source of random numbers (java.util.Random)Warning: you might get the same priority more than once; don’t panic

When you remove a value, preserve the heap and tree propertiesWarning: the remove code from Tree won’t magically work here

Careful if you want to inherit from edu.rice.tree.TreeWarning: any method in Tree that has “new Tree...” in it won’t workWe advise you against extending Tree.

Careful with the empty treapWhat’s the priority of the empty treap?

Page 41: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Implementing treaps

Page 42: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treap performanceProf. Dan’s code, JDK 8u51, 2014 MacPro, inserting integers

1M random tree inserts: 0.851 μs per insert 1M random treap inserts: 1.194 μs per insert

10K random tree inserts: 0.199 μs per insert10K random treap inserts: 0.359 μs per insert

10K sequential tree inserts: 113.925 μs per insert10K sequential treap inserts: 0.328 μs per insert

Randomized algorithms rock.Often easier to implement than deterministic. Fast runtime. Stable performance.

Page 43: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Treaps are coolAttackers can’t pick inputs that cause worst-case behaviorSo long as attackers can’t predict the random numbers.☞ Useful for intrusion detection systems (e.g., looking for network scanners).

Optional: Replace random priority with a hash of the tree node valueTreap becomes determinstic (same inputs, any order → identical treap structure)☞ Recursive hashes over these treaps useful for quick set-equality tests

(Advanced: Read up on Merkle hash trees. Treaps show up in many “authenticated data structures.”)

Once you understand randomized data structuresUseful throughout computer science(Seidel’s specialty is computational geometry: full of randomized algorithms.)

Page 44: 4 % 2 3 % # 5 2 )4 0 5 9 · 2015. 9. 16. · Solution: “balanced” trees Many, many different algorithms for doing this (AVL, red-black, etc.) We’re going to learn about treaps

Live coding (time permitting)Walkthrough of TreeTest.testTreapPerformance

Walkthrough of different tree traversalsinorder consumer vs. eager list vs. lazy list

Challenge: add together the smallest 1000 entries in a tree with 1 million integers