This is a slide Group Philsophy Binary Search Tree (BST) A binary - - PowerPoint PPT Presentation

this is a slide
SMART_READER_LITE
LIVE PREVIEW

This is a slide Group Philsophy Binary Search Tree (BST) A binary - - PowerPoint PPT Presentation

This is a slide Group Philsophy Binary Search Tree (BST) A binary search tree 12 5 15 3 8 16 17 4 6 9 7 Binary Search Tree 2 Construct binary search tree Input[2, 4, 5, 1, 3] 1 4 3 5 O(logN) What if the input


slide-1
SLIDE 1

This is a slide

Group Phil♂sophy

slide-2
SLIDE 2

Binary Search Tree (BST)

slide-3
SLIDE 3

12 5 3 6 8 4 15 7 9 17 16

A binary search tree

slide-4
SLIDE 4

Binary Search Tree

  • Construct binary search tree
  • Input[2, 4, 5, 1, 3]

2 4 1 5 3

  • O(logN)
slide-5
SLIDE 5
  • What if the input is [1, 2, 3, 4, 5] ?

1 2 3 4 5

  • A linked list.
  • Only O(N)
slide-6
SLIDE 6

Some Self-Balancing Binary Search Trees

  • AVL(Adelson-Velskii & Landis Tree)
  • Red Black Tree
  • Size Balanced Tree
  • Treap
slide-7
SLIDE 7

Treap

a randomized

binary search tree

slide-8
SLIDE 8

A tree or a heap?

  • Key [4, 2, 1, 3, 5]
  • Priority [15, 11, 13, 18, 19]

2 3 4 1 5

11 19 18 15 13

slide-9
SLIDE 9

Rotate

4 2 5 1 3 2 1 4 3 5

right rotate left rotate

slide-10
SLIDE 10

Rotate

4 2 5 1 3 2 1 4 3 5

right rotate 30 10 50 20 40 10 20 30 50 40

slide-11
SLIDE 11

Insert

Lets build a treap together!

slide-12
SLIDE 12

2 1 5 3 6

10 18 20 40 30

4

15

Insert

Key = 4 Priority = 15

Left rotate 3 Right rotate 5

slide-13
SLIDE 13

Search

Just as you do in the BST

2 1 4 3 5

10 18 15 20 30

6

40

slide-14
SLIDE 14

Delete

Just do it!

2 1 6 4 7

10 15 12 15 13

8

30

5

19

3

40

Rotate then do it

slide-15
SLIDE 15

2 1 6 4 7

10 15 12 15 13

8

30

5

19

3

40

Find node 6 Right rotate 6 Left rotate 6 Delete 6

slide-16
SLIDE 16

Deeper understanding of treap

slide-17
SLIDE 17
  • DEF. mean that i is a proper ancestor of k.
  • If i is proper ancestor of k,
  • Otherwise

k i  k i   1 k i  

2 3 4 1 5

5 3 4 1 1 3 4 1 3 2 1 5 2 1 4 2            

Input array is [1, 2, 3, 4, 5] Input priority is [0.3, 0.1, 0.8, 0.5, 0.9] 0.1 0.9 0.8 0.5 0.3

slide-18
SLIDE 18
  • The depth of node k:

 

 

 

A i

k i k depth

   

   

 

 

   

A i A i

k i Pr k i E E E

slide-19
SLIDE 19
  • DEF. X(i,k) denotes set of nodes which key value is between i and k.
  • For input array [2, 3, 1, 5, 4]
  • X(2,5)={2,3,4,5}
  • X(1,3)={1,2,3}
slide-20
SLIDE 20
  • Lemma. For all , i has the smallest priority in X(i,k)

k i 

iff 1 k i  

slide-21
SLIDE 21

 

 

                    k i if 1 k i 1 k i if k i if 1 i k 1 1 i k k i k i Pr

   

 

 

2 n ln 2 2 1 k n ln k ln 1 H 1 H j 1 j 1 1 k i 1 1 i k 1 1 k i 1 k i Pr k depth E

1 k n k 1 k n 2 i k 2 j n 1 k i 1 k 1 i k i A i A i

                       

            

     

slide-22
SLIDE 22
  • depth(k) = O(logN) in expectation.
  • search(k) takes time O(depth(k)) = O(logN) in expectation.
  • insert(k) takes time O(depth(k))=O(logN) in expectation
slide-23
SLIDE 23
  • Lemma.

k is a node in treap, then

   

 

 

N log O k depth max E 

slide-24
SLIDE 24

Why we are using a treap?

  • Easy to understand
  • Easy to use
  • Well reliability
  • Average depth O(logN)
slide-25
SLIDE 25

Thank you

Q & A