Data Structure II So Pak Yeung 26-2-2011. Outline Review Array Sorted Array Linked List Binary...

35
Data Structure II So Pak Yeung 26-2-2011

Transcript of Data Structure II So Pak Yeung 26-2-2011. Outline Review Array Sorted Array Linked List Binary...

Page 1: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Data Structure II

So Pak Yeung

26-2-2011

Page 2: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Outline

Review Array Sorted Array Linked List

Binary Search Tree Heap Hash Table

Page 3: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Review

Operation Find an element Find the min/max element Insert an element Remove an element

Time complexity? O(1)? O(lg N)? O(N)?

Page 4: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Array

Find an element O(N)

Find the smallest element O(N)

Insert an element O(1)

Remove an element O(1)

Page 5: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Sorted Array

Find an element O(lg N)

Find the smallest element O(1)

Insert an element O(N)

Remove an element O(N)

Page 6: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Linked List

Find an element O(N)

Find the smallest element O(N)

Insert an element O(1)

Remove an element O(1)

Page 7: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Binary Search Tree

Binary Tree At most 2 children for each node

For each Node i, Node j <= Node i, for all Node j in left subtree of N

ode i Node j > Node i, for all Node j in right subtree of N

ode i

Page 8: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Binary Search Tree

5 13 34

8

212

Page 9: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Binary Search Tree

5 13 34

8

212

• Find 13

Page 10: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Binary Search Tree

5 13 34

8

212

• Find 3

???

Page 11: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Binary Search Tree

5 13 34

8

212

• Insert 1

1

Page 12: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Binary Search Tree

5 13 34

8

212

• Insert 3

1

3

Page 13: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Binary Search Tree

Find an element Seems O(lg N)?

Find min/max Seems O(lg N)?

Insert an element Seems O(lg N)?

Page 14: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Binary Search Tree

5

13

34

8

21

2

•Worst Case: O(N)!!!

Page 15: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Binary Search Tree

Remove a leaf node Just Do it!

Remove a node with single child Replace the node by its child

Remove a node with 2 children Replace the node by the max node of left subtree /

min node of right subtree Lazy Deletion

Mark the node as deleted instead of deleting it

Page 16: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Binary Search Tree

5

13

34

8

21

2

•Again, seems O(lg N), worst Case: O(N)!!!

Page 17: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

Priority Queue Binary Tree For each node, it is no greater/less than all n

odes of its subtree Operation

Extract min/max Insert

Page 18: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

5 21 34

1

132

8

Page 19: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

Extract min/max Get the value from the root (O(1) to find min) Replace the root by the last node Shift down

Time complexity O(lg N)

Page 20: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

5 21 34

1

132

8

• Get 1

Page 21: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

5 21

132

8

34

Page 22: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

5 21

1334

8

2

Page 23: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

34 21

135

8

2

Page 24: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

Insert an element Add the node at the end Shift up

Time complexity O(lg N)

Page 25: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

34 21

135

8

2

• Add 1

1

Page 26: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

34 21

15

8

2

13

Page 27: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

34 21

25

8

1

13

Page 28: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

Build heap Insert each node O(N lg N) There exists a faster way!! Only need to perform shift down process from the

bottom O(N)

Page 29: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Heap

Find an element Not support O(N), if implement by array

Remove an element Consider that subtree O(lg N)

Page 30: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Hash Table

Using limited memory, storing data of unlimited range

Convert data to integers that are in a limited range by a hash function

Page 31: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Hash Table

Mark 5 number Each number is

between [1,10000000] Not a good idea to use

an array of size of 10000000

A[n%5]=n

0 6625800

1 65536

2 1234567

3 38

4 4

Page 32: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Hash Table

Insert an element O(1)

Find an element Using the same Hash function! O(1)

Delete an element Lazy Deletion O(1)

Page 33: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Hash Table

Collision? E.g. 56 and 111 Open Hashing Close Hashing

Page 34: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Hash TableOpening Hashing

0

1

2

3

4

11156

127

Page 35: Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Hash Table

Close hashing Insert 1 If the cell is occupied,

find the next empty cell

0 6625800

1 65536

2 1234567

3 38

4 1