Вы находитесь на странице: 1из 25

CSCI 2720:

Data Structures

Review 3

Tianming Liu
Department of Computer Science & Bioimaging Research Center
The University of Georgia

Priority Queues

A nodes key value is <= the key value of its children

5
8

16
18

12 10 56
20

No conclusion can be drawn about the relative order


of the items in the left and right sub-trees of a node

Heaps: Insert

Insert node with key 7


5

16
20 18

12
22

10
44

13

56
7

Heaps: Insert

16
20

12
18

22

7
44

13 10

8
56

16
20

7
12

18

22 44

9
13

56
10

Heaps: DeleteMin
Given the tree below

13

16
20

12
18

22

10
44

13

8
56

16
20

12
18

22

10
44

56

Heaps: DeleteMin

13

16
20

12
18

22

10
44

12
56

16
20

13
18

22

10
44

56

Sorting

Sorting
Selection Sort
Bubble Sort
Insertion Sort

Shellsort

Invented by Donald Shell in 1959.


1st algorithm to break the quadratic time
barrier
Shellsort works by comparing elements that
are distant rather than adjacent elements in
an array.

Mergesort
Mergesort

(divide-and-conquer)

Divide array into two halves.


Recursively sort each half.
Merge two halves to make sorted whole.

divide

sort

merge

Merging
Merge.

Keep track of smallest element in each sorted half.


Insert smallest of two elements into auxiliary array.
Repeat until done.

smallest

G
A

smallest

T
auxiliary array

Merging
Merge.

Keep track of smallest element in each sorted half.


Insert smallest of two elements into auxiliary array.
Repeat until done.

smallest

G
A

L
G

smallest

T
auxiliary array

Quicksort

Divide step:
Pick any element (pivot) v in S
Partition S {v} into two disjoint groups
S1 = {x S {v} | x <= v}
S2 = {x S {v} | x v}

Conquer step: recursively sort S1 and S2

Combine step: the sorted S1 (by the time


returned from recursion), followed by v,
followed by the sorted S2 (i.e., nothing
extra needs to be done)

S1

S2

To simplify, we may assume that we dont have repetitive elements,


So to ignore the equality case!

A better partition

Want to partition an array A[left .. right]


First, get the pivot element out of the way by swapping it with
the last element. (Swap pivot and A[right])
Let i start at the first element and j start at the next-to-last
element (i = left, j = right 1)

swap
5

pivot

3 12 19

4 19 3 12

Pseudo-code
Input: an array a[left, right]
QuickSort (a, left, right) {
if (left < right) {
pivot = Partition (a, left, right)
Quicksort (a, left, pivot-1)
Quicksort (a, pivot+1, right)
}
}

Compare with MergeSort:


MergeSort (a, left, right) {
if (left < right) {
mid = divide (a, left, right)
MergeSort (a, left, mid-1)
MergeSort (a, mid+1, right)
merge(a, left, mid+1, right)
}
}

Sets

Collection of objects

A sorted associative container that does not allow


duplicates

MultiSet

A sorted associative container that allows duplicates

Generic Set Algorithms

Very useful software tools

Implement set theory

Union

Intersection

Difference

Containment

Merge

Runtime complexity in O(size)

Disjoint Sets

Disjoint Sets

We have a fixed set U of Elements Xi


U is divided into a number of disjoint subsets S1,
S2 , S3 , S k
Si Sj is empty i j
S1 S2 S3 Sk = U

Linked-lists for two sets


Set {c,h,e}

head

tail
Set {f, g} head
tail

UNION of
two Sets
head

tail

Sets of Digital Data

Bit vectors
Trie
Patricia Tree
de la Briandais trees

Bit Vector

Used to represent a subset S U


A table of N bits, Bits[0.. N-1]

Bits[i] == 1 if ui S
Bits[i] == 0 if ui S

Example: todays attendance


0
1

6 -- student number

1 = present
0 = absent

Hash Tables

Hash Tables solve these problems by using a much smaller array and mapping keys
with a hash function.

Let universe of keys U and an array of size m. A hash function h is a function from U
to 0m, that is:

h:U
k1

k2

k 3 k4
k6

(universe of keys)

0m

0
1
2
3
4
5
6
7

h (k2)=2
h (k1)=h (k3)=3
h (k6)=5
h (k4)=7

Hashing with Chaining


The problem is that keys 34 and 54 hash in the same entry (4). We
solve this collision by placing all keys that hash in the same hash table
entry in a chain (linked list) or bucket (array) pointed by this entry:

Insert 54

0
1
2

Insert 101

other
key key data

0
1

21
2

3
4

21
2

101

54

34

3
54

34
CHAIN

SAMs: solutions

K-d trees
point quadtrees
MX-quadtrees

Q: how would you organize,


e.g., n-dim points, on disk?

K-d tree
A
D
B

B
C

C
D
A

Leaf nodes correspond to unique regions in space

The End

Вам также может понравиться