58
Outline Binary Trees Binary Search Tree Treaps

Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Embed Size (px)

Citation preview

Page 1: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Outline

• Binary Trees

• Binary Search Tree

• Treaps

Page 2: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Binary Trees

• The empty set (null) is a binary tree

• A single node is a binary tree

• A node has a left child and a right child each of which is a binary tree

Page 3: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• A node is the parent of its children

• A node with no parent is called a root

• A node with no children is called a leaf,

• non-leaves are called internal nodes.x’sParentx’sParent

xx

x’s Childrenx’s Children

RootRoot

LeavesLeaves

Binary Trees

Page 4: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Binary Trees• The depth of a node is its distance from the root

0

1

2

3

4

xx

X’s depthX’s depth

• A level is the set of nodes with the same depth

• The height of a binary tree is the maximum depth of any node• The size of a tree is the number of nodes in the tree

Level 2Level 2

HeightHeight

Size = 14Size = 14

Page 5: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Binary Trees• A full binary tree is one in which every internal node has 2 children• Prove: A full binary tree with n leaves has n - 1 internal nodes• A complete binary tree is one in which every level is full, except possibly the last level • Prove: A complete binary tree of height k has size in the range [2k; 2k+1-1]

Page 6: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Applications

• In an expression tree

• Each leaf represents a value (variable or constant)• Each internal node represents a binary operator

• Expression trees are used inside of compilers.• In a binary search tree, each node u has a value u.x• For every node u, u.left.x < u.x < u.right.x

• Binary search trees can implement SortedSets and SortedMaps

Page 7: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Traversing Binary Trees

• How to traverse all the node of the binary tree?• If the tree is empty (null) do nothing. Otherwise• Traverse the left subtree.

• Traverse the right subtree.

Page 8: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Traversing Binary Trees• How can we do it without recursion?

• Where we go next depends on the previous node

1. If previous node was our

parent then go to left child2. If previous node was left child

then go to our right child3. If previous node was right child

then go to our parent• Rule 3 is problematic• How do we know our parent?• We can either use a stack to maintain the current path to the root, or maintain a parent pointer for each node

Page 9: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Preorder Traversal

• Visit u before either of its children.

void traverse ( Node u) {if (u == null ) return ;// visit u heretraverse (u. left );traverse (u. right );}

00

11

22

33

44

55 66

77 88

99

10101111

1212 1313

Page 10: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Preorder Example ( making a copy of the

tree)• To copy the subtree rooted at u we

• First create a copy of u

• Then copy u.left and u.right recursivelyprotected Node copyTo ( Node u, BinaryTree <Node > t) {if (u == null )

return null ;Node w = t. new Node ();

w. left = copyTo (u.left , t);w. right = copyTo (u.right , t);

return w;}

Page 11: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Postorder Traversal

• Visit u after visiting both of its children.

void traverse ( Node u) {if (u == null ) return ;traverse (u. left );traverse (u. right ); // visit u here}

1313

77

11

00

66

22 55

33 44

1212

881111

99 1010

Page 12: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Postorder Example (computing the size)

(evaluating expression tree)

• The size of node u is the size of its children + 1• We need to know the result of u’s children before compute the expression for uValue eval ( Node u) {if (u. isLeaf ()) return u. value ;Value vl = eval (u. left );Value vr = eval (u. right );return u.op(u.left , u. right );

+

*

-

*

/

+

/

*

+

x 2 3

5 y

a

b 6

(x+2)(3-5y)(a(b+6))

Page 13: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Inorder Traversal

• Visit u after visiting its left child and before visiting its right child.

void traverse ( Node u) {if (u == null ) return ;traverse (u. left );// visit u heretraverse (u. right );}

88

22

11

00

44

33 66

55 77

1010

991212

1111 1313

Page 14: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Inorder Example ( printing binary search

tree)• To keep the order we need to print u after the left child and before the right child

void printInOrder ( Node u) {

if (u == null ) return ;

printInOrder (u. left );

System.out. println (u.x);

printInOrder (u. right );}

Page 15: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Complete Example (printing expression tree)

• Some problems require all three

String toString ( Node u) {if (u. isLeaf )

return u. value . toString ();

return "(" // pre+ toString (u. left )+ u.op // in+ toString (u. right )+ ")"; // post

}}

+

*

-

*

/

+

/

*

+

x 2 3

5 y

a

b 6

( ( ( x+2 )( 3 – ( 5 * y ) ) ) / ( a * ( b + 6 ) ) )( ( ( x+2 )( 3 – ( 5 * y ) ) ) / ( a * ( b + 6 ) ) )

Page 16: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Traversing Binary Trees

• Pre-, in-, and post- order numberings allow us to deduce some information about nodes

• u is an ancestor of w if and only if pre(u) < pre(w) and post(u)>post(w)

+

1,8

5,7

7,6

2,2

0,14

10,13

12,12

3,0

4,1

6,38,4

9,5

11,9

13,10

14,11

uu

ww

vv

u (1,8), w (9,5) 1 < 9 and 8 > 5 u (1,8), w (9,5) 1 < 9 and 8 > 5 v (10,3), w (9,5) 10 > 9 and 13 > 5 v (10,3), w (9,5) 10 > 9 and 13 > 5 Depth w (9,5) = 4 9 - 5 = 4 Depth w (9,5) = 4 9 - 5 = 4

• For a leaf w, depth(w)=pre(w)-

post(w)• size(u) = ?

Page 17: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Higher Degree Tree

• Non-binary trees are also possible (and sometimes useful)

Page 18: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• Binary trees are useful for representing lots of things

• Inorder, Preorder, and Postorder traversal solve a lot of problems

–All run in O(n) time on a tree with n nodes

Binary Trees Summary

Page 19: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2 3 40 1 7 8 95 6

ii jjmm

10

• Binary search is used to search in a sorted array a of Comparable values for some value x– If a.length == 1 then quit– Compare x with a[a.length/2]• If x == a[a.length/2] then we have found x, quit• If x < a[a.length/2] then we recursively search– a[0],...,a[a.length/2-1]

• Else (x > a[a.length/2]) then we recursively search– a[a.length/2]+1,...,a[a.length-1]

Binary Search

Page 20: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2 3 40 1 7 8 95 6

iijjmm

10

• Binary search is used to search in a sorted array a of Comparable values for some value x– If a.length == 1 then quit– Compare x with a[a.length/2]• If x == a[a.length/2] then we have found x, quit• If x < a[a.length/2] then we recursively search– a[0],...,a[a.length/2-1]

• Else (x > a[a.length/2]) then we recursively search– a[a.length/2]+1,...,a[a.length-1]

Binary Search

Page 21: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2 3 40 1 7 8 95 6

ii jjmm

10

• Binary search is used to search in a sorted array a of Comparable values for some value x– If a.length == 1 then quit– Compare x with a[a.length/2]• If x == a[a.length/2] then we have found x, quit• If x < a[a.length/2] then we recursively search– a[0],...,a[a.length/2-1]

• Else (x > a[a.length/2]) then we recursively search– a[a.length/2]+1,...,a[a.length-1]

Binary Search

Page 22: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• Binary search is used to search in a sorted array a of Comparable values for some value x– If a.length == 1 then quit– Compare x with a[a.length/2]• If x == a[a.length/2] then we have found x, quit• If x < a[a.length/2] then we recursively search– a[0],...,a[a.length/2-1]

• Else (x > a[a.length/2]) then we recursively search– a[a.length/2]+1,...,a[a.length-1]

Binary Search

2 3 40 1 7 8 95 6

ii jjmm

10

Page 23: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• At each iteration of binary search we perform 1 comparison in O(1) time– This comparison either:

1. ends the algorithm (because we find x) or

2. reduces the size of the search range by (at least) a factor of 2

• How often can we divide a.length by 2 before we get 1?

– log2(a.length)

• On an array of length n, binary search finishes

after log2n iterations and runs in O(log n) time

in the worst case.

Analysis of Binary Search

Page 24: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• O(log n) is much faster than O(n)

• There are cases in which binary search need log n iterations.

–Ex: If we look for an element lower than the minimum.

Analysis of Binary Search

Page 25: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• Theorem: When run on a sorted array a of length

n, binary search performs log2 n + 1 comparisons

and runs in O(log n) time in the worst case

• A sorted array can be used to implement the SortedSet– operations contains(), headSet(), tailSet(), and

subSet() in O(log n) time per operation

• What about the add() and remove() operations?

Binary Search Summary

• Adding/removing an element to/from a sorted array is slow–Takes O(1 + n - i ) time if the rank of the

element is i.

Page 26: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2 3 40 1 7 8 95 6 10

Binary Search and Binary Search Tree

• Binary search defines an implicit binary tree on the elements of a:• a[a.length/2] is the root

• the left subtree contains a[0]...a[a.length/2-1]

• the right subtree contains a[a.length/2+1]...a[a.length-1]

Page 27: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2 3 40 1 7 8 9

5

6 10

Binary Search and Binary Search Tree

• Binary search defines an implicit binary tree on the elements of a:• a[a.length/2] is the root

• the left subtree contains a[0]...a[a.length/2-1]

• the right subtree contains a[a.length/2+1]...a[a.length-1]

Page 28: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2

3 40 1 7

8

9

5

6 10

Binary Search and Binary Search Tree

• Binary search defines an implicit binary tree on the elements of a:• a[a.length/2] is the root

• the left subtree contains a[0]...a[a.length/2-1]

• the right subtree contains a[a.length/2+1]...a[a.length-1]

Page 29: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2

3

4

0

1 7

8

9

5

6

10

Binary Search and Binary Search Tree

• A binary search tree is a binary tree where each node u stores a distinct value u.x (Comparable)

• For every node u all values stored in the subtree u.left are smaller than u.x

• For every node u all values stored in the subtree u.right are larger than u.x

Page 30: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2

3

4

0

1 7

8

9

5

6

10

Searching in a Binary Search Tree

• A binary search tree is a binary tree where each node

• To find a value x in a binary search tree:

• If x == root.x then we have found x

• If x < root.x then recursively search in root.left

• Else (x > root.x) then recursively search in root.right

x=3x=3x=

3x=3

x=3x=3

x=3x=33

Page 31: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2

3

4

0

1 7

8

9

5

6

10

Inserting into a Binary Search Tree

• To insert a new value x in a binary search tree:

• We search for x

• The last node in the search path becomes the parent of x.

X=4.5X=4.5

x=4.5x=4.54

4.5

2

5

Page 32: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2

3

4

0

1 7

8

9

5

6

10

Removing in a Binary Search Tree• We want to delete a node u containing x from a

binary search tree

X=4.5X=4.5

4.5

• This is easy if u is a leaf (x= 4.5)• This is easy if u has only one child (x=10)• What if u has two children? (x=5)

4.5

x=10x=10

10

x=5x=5

Page 33: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

2

3

4

0

1 7

8

9

5

6

10

Removing in a Binary Search Tree• If x has two children then we want to replace u.x

with something that is easy to delete

ww

4.54.5

10

• Go to u.right• Go left until reaching a node w with no left

child (w.left == null)• Delete w and set u.x = w.x

uu

ww

ww

6

6

Page 34: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• How big is d(x) the length of the search path for x?

Analysis of Binary Search Trees

– It depends on the order in which elements were inserted–Which of the following is more likely?

14121086420

13951

113

7

0

1

13

14

14,13,12,11,10,9,8,7,6,5,4,3,2,1,0

14,13,12,11,10,9,8,7,6,5,4,3,2,1,0

7,3,11,1,5,9,13,0,2,4,6,8,10,12,14

7,3,11,1,5,9,13,0,2,4,6,8,10,12,14

Page 35: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Analysis of Binary Search Trees

• There is only one way to get the left picture

• There are thousands of ways to get the right picture

14121086420

13951

113

7

0

1

13

14

14,13,12,11,10,9,8,7,6,5,4,3,2,1,0

14,13,12,11,10,9,8,7,6,5,4,3,2,1,0

7,3,11,1,5,9,13,0,2,4,6,8,10,12,14

7,3,11,1,5,9,13,0,2,4,6,8,10,12,147,3,11,1,5,9,13,0,2,4,6,8,10,14,12

7,3,11,1,5,9,13,0,2,4,6,8,10,14,127,3,11,1,5,9,13,0,2,4,6,12,10,14,8

7,3,11,1,5,9,13,0,2,4,6,12,10,14,8

Page 36: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• A random binary search tree is obtained by:– Starting with a set S of n distinct elements– Randomly permuting S– Inserting elements of S one at a time into a

binary search tree

Random Binary Search Trees

• Theorem: For any particular value x ϵ S, the expected value of d(x) is at most 2 ln n.

• Theorem: The expected height is at most 4.311 ln n• The expected time to search for an element is O(log n)

• The expected time needed to build a random binary search tree is O(n log n) (this is quicksort)

Page 37: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• quicksort(S):

Quicksort

7 2 15 8 6 3 04 95

p=5p=5

1. Pick a random element p from S

Page 38: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• quicksort(S):

Quicksort

4 3 02 1 7 9 65 8

p=5p=5

S<

S<

S=S= S

>

S>

–Pick a random element p from S–Compare every element to p to get 3 sets•S< = {x ϵ S : x < p}

•S= = {x ϵ S : x = p}

•S> = {x ϵ S : x > p}

2 3 40 1 7 8 95 6

–quicksort(S<)

–quicksort(S>)

Page 39: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• quicksort(S):– Pick a random element p from S– Compare every element to p to get 3 sets

•S< = {x ϵ S : x < p}

•S= = {x ϵ S : x = p}

•S> = {x ϵ S : x > p}

– quicksort(S<)

– quicksort(S>)

– return S S<·S=·S>

Quicksort

2 3 40 1 7 8 95 6

SS

Page 40: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• quicksort(S):

Quicksort

• Build a random binary search tree ( buildrbst(S) ):

– Pick a random element p from S– Compare every element to p to get 3 sets• S< = {x ϵ S : x < p}

• S= = {x ϵ S : x = p}

• S> = {x ϵ S : x > p}– quicksort(S<), quicksort(S>) and return S S<·S=·S>

– Pick a random element p, make p the root and delete it from– For each element x ϵ S• if x < p insert x in left subtree • if x = p do nothing• if x > p insert x in right subtree

Quicksort performs exactly the same comparisons as thosedone when building a random binary search tree O(n log n) expected time.

Page 41: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Random BST

• The expected length of a search path

Skiplist

2 ln n = 2 loge n 2 log2 n

Random binary search trees versus skiplists

• 2 ln n = 2 loge n ≈ 1.3867 log2 n

• Search paths in random binary search trees are nearly 30% shorter than in skiplists

Which is better?

Page 42: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• Binary search can search a sorted array of n elements in O(log n) time using at most log2 n + 1 comparisons– But don't support efficient insertion or deletion

Summary

• Binary search trees can store elements for– Searching– Inserting– Deleting– But there is no guarantee (other than O(n)) on the

running time

• Random binary search trees can support search in O(log n) expected time

Page 43: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• We want to use the properties of about random binary search trees in a dynamic binary search tree ( O(log n) expected value of d(x) )–Supports insertions and deletions

Treap: Randomized Search Trees

• Problem: after inserting/deleting in a random binary search tree, the resulting binary search tree is no longer random

• Solution: Make the tree “look like" a random binary search tree after each insertion/deletion

Page 44: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• A treap is binary search tree where each node u stores

Treap: Randomized Search Trees

4,.534,.53

9,.619,.61

6,.696,.69

3,.723,.72

5,.025,.02

8,.158,.15

7,.287,.28

2,.312,.31

0,.770,.77

1,.491,.49

–A key u.x (we search for these)–A priority u.prio (these are assigned randomly)

• Treap is a binary search tree on the keys

• Treap is a heap on the priorities

Page 45: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

• A treap is binary search tree where each node u stores

Treap: Randomized Search Trees

–A key u.x (we search for these)–A priority u.prio (these are assigned randomly)

• Treap is a binary search tree on the keys

• Treap is a heap on the priorities– Node u with minimum u.prio is the root– Subtrees are built recursively

4,.534,.53

9,.619,.61

6,.696,.69

3,.723,.72

5,.025,.02

8,.158,.15

7,.287,.28

2,.312,.31

0,.770,.77

1,.491,.49

Page 46: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Treap: Randomized Search Trees

4,.534,.53

9,.619,.61

6,.696,.69

3,.723,.72

5,.025,.02

8,.158,.15

7,.287,.28

2,.312,.31

0,.770,.77

1,.491,.49

5,.025,.02

4,.534,.53

3,.723,.72

2,.312,.31

0,.770,.77

1,.491,.49

9,.619,.61

6,.696,.69

8,.158,.15

7,.287,.28

Page 47: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Treap: Randomized Search Trees

4,.534,.53

9,.619,.61

6,.286,.28

3,.723,.72

5,.025,.02

8,.158,.15

7,.697,.69

2,.312,.31

0,.770,.77

1,.491,.49

5,.025,.02

4,.534,.53

3,.723,.72

0,.770,.77

1,.491,.49

2,.312,.31

9,.619,.61

6,.286,.28

8,.158,.15

7,.697,.69

Page 48: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Treap: Randomized Search Trees

4,.534,.53

9,.619,.61

6,.286,.28

3,.723,.72

5,.025,.02

8,.158,.15

7,.697,.69

2,.312,.31

0,.770,.77

1,.491,.49

5,.025,.02

4,.534,.53

3,.723,.72

0,.770,.77

1,.491,.49

2,.312,.31

9,.619,.61

6,.286,.28

8,.158,.15

7,.697,.69

Page 49: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Treap: Randomized Search Trees

• Alternative (equivalent) definition for treaps:–Sort (x,prio) pairs by priority– Insert one at a time into a binary search tree

• Since a treap is random binary search tree, it inherits all their properties–Expected depth d(x) and height O(log n)

• How do we support insertions and deletions?

• We need a way to make the treap “look random again" after an insertion or deletion

Page 50: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Treap: Randomized Search Trees

u

v

A B

C

u

v

A

B C

Rotate right

Rotate right

Rotate leftRotate left

• A < v.x < B < u.x < C is preserved–Left Turn: u moves up in the tree (v moves

down)–Right Turn: v moves up in the tree (u moves

down)

Page 51: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Insertion into a Treaps5,.02

• To insert the value x into a treap:

2,.31 8,.15

1,.49 4,.53 7,.28 9,.61

0,.77 3,.72 6,.69

3.5,.51

1. Make a new node u with u.x=x and a random priority u.prio

2. Insert u as usual (so that u is a leaf)

3.5,.51

Page 52: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Insertion into a Treaps5,.02

• To insert the value x into a treap:

2,.31 8,.15

1,.49 4,.53 7,.28 9,.61

0,.77

3,.72

6,.69

1. Make a new node u with u.x=x and a random priority u.prio

2. Insert u as usual (so that u is a leaf)3. While u is not the root and u.prio <

u.parent.prio• I Do a rotation at u.parent to move u upwards

3.5,.51

Page 53: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Insertion into a Treaps5,.02

• To insert the value x into a treap:

2,.31 8,.15

1,.49

4,.53

7,.28 9,.61

0,.77 3,.72 6,.69

1. Make a new node u with u.x=x and a random priority u.prio

2. Insert u as usual (so that u is a leaf)3. While u is not the root and u.prio <

u.parent.prio• I Do a rotation at u.parent to move u upwards

3.5,.51

Page 54: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Treap: Randomized Search Trees

• To insert the value x into a treap:1. Make a new node u with u.x=x and a

random priority u.prio2. Insert u as usual (so that u is a leaf)3. While u is not the root and u.prio <

u.parent.prio• I Do a rotation at u.parent to move u upwards

• Step 1 Takes O(1) time

• Step 2 Takes O(d(x)) = O(log n) expected time

• Step 3 Takes at most O(d(x)) = O(log n) expected timeInsertion into a treap takes O(log n) expected timeInsertion into a treap takes O(log n) expected time

Page 55: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Insertion into a Treaps5,.02

• To delete the node u from a treap:1. While u is not a leaf•Do a rotation at u to move u downward

• left or right rotation depends if – (u.left.prio < u.right.prio)

2,.31 8,.15

1,.49

4,.53

7,.28 9,.61

0,.77 3,.72 6,.69

3.5,.51

Page 56: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Insertion into a Treaps5,.02

2,.31 8,.15

1,.49 4,.53 7,.28 9,.61

0,.77

3,.72

6,.693.5,.51

• To delete the node u from a treap:1. While u is not a leaf•Do a rotation at u to move u downward

• left or right rotation depends if – (u.left.prio < u.right.prio)

Page 57: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Insertion into a Treaps5,.02

• When u is a leaf:

2,.31 8,.15

1,.49 4,.53 7,.28 9,.61

0,.77 3,.72 6,.69

2. Remove u as usual (so that u is a leaf)

3.5,.513.5,.51

Deletion perform the same rotation that insertion in reverse order

Page 58: Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child

Treap: Randomized Search Trees

• The quicksort recursion tree is a random binary search tree

• Theorem: A treap storing n elements supports the operations search(x), insert(x), delete(x) in O(log n) expected time per operation

• Treaps can implement SortedSet/SortedMap interfaces in O(log n) expected time per operation

• The expected number of comparisons done during a search is at most 2 ln n (better than skiplists)

• The expected number of rotations done during an insertion/deletion is only O(1) (this is true, but we didn't prove it)