# CS 261 Data Structures

Τεχνίτη Νοημοσύνη και Ρομποτική

6 Νοε 2013 (πριν από 4 χρόνια και 6 μήνες)

119 εμφανίσεις

CS 261

Data Structures

Build?Heap and Heap Sort

Heap Implementation:
Constructors

void buildHeap(struct dyArray * data) { int I;

int max = dyArraySize(data);

// All nodes greater than max/2
-

1 are leaves and thus adhere to the heap property!

for (i = max / 2
-

1; i >= 0; i
--
)

// Make subtree rooted at
i

a heap.

}

At the beginning, only the leaves are proper heaps:

Leaves are all nodes with indices greater than max / 2

At each step, the subtree rooted at index
i

becomes
a heap

Heap Implementation:
Build heap

void buildHeap(struct vecctor * data) { int I;

int max = vectorSize(data);

// All nodes greater than max/2
-

1 are leaves and thus adhere to the heap property!

for (i = max / 2
-

1; i >= 0; i
--
)

// Make subtree rooted at
i

a heap.

}

For all subtrees that are are not already heaps
(initially, all
inner, or non
-
leaf, nodes)
:

Call

with the
largest

node index that is not already
guaranteed to be a heap

Iterate until the root node becomes a heap

Why call

with the
largest

non
-
heap node?

Because its children, having larger indices, are already
guaranteed to be heaps

Heap Implementation:

12

5

4

7

7

8

3

9

11

10

2

1

7

2

5

3

8

4

3

0

12

11

max

5

7

6

4

7

9

8

11

9

10

10

2

i

(
max/2
-
1
)

(leaf nodes)

First iteration:

-
leaf

node (index 4)

Heap Implementation:

12

5

4

7

7

8

2

9

11

10

3

1

7

2

5

3

8

4

2

0

12

11

max

5

7

6

4

7

9

8

11

9

10

10

3

i

Second iteration:

-
heap

node (index 3)

Heap Implementation:

12

5

4

7

7

8

2

9

11

10

3

1

7

2

5

3

8

4

2

0

12

11

max

5

7

6

4

7

9

8

11

9

10

10

3

i

Third iteration:

-
heap

node (index 2)

Heap Implementation:

12

4

5

7

7

8

2

9

11

10

3

1

7

2

4

3

8

4

2

0

12

11

max

5

7

6

5

7

9

8

11

9

10

10

3

i

Fourth iteration:

-
heap

node (index 1)

Heap Implementation:

12

4

5

2

7

8

3

9

11

10

7

1

2

2

4

3

8

4

3

0

12

11

max

5

7

6

5

7

9

8

11

9

10

10

7

i

Fifth iteration:

-
heap

node (index 0

root
)

Heap Implementation:

2

4

5

3

7

8

7

9

11

10

12

1

3

2

4

3

8

4

7

0

2

5

7

6

5

7

9

8

11

9

10

10

12

(entire tree)

Heap Sort
-

Basic idea

Build the initial heap

Repeately

Remove the smallest element (root)

Rebuild the heap

So the heap starts out with n elements, then has n
-
1, then n
-
2, and
so on

Where should you store the elements that are removed?

Why not put them in the other end of the array? (The one that is no
longer being used for the heap).

Heap Implementation:
sort

void heapSort(struct dyArray * data) { int i;

buildHeap(data);
// Build initial heap.

for (i = dyArraySize(data)

1; i > 0; i
--
) {
// For each of the n elements:

dyArraySwap(data,0, i);
// Swap last element with the first (smellest) element

// Rebuild heap property.

}

}

Sorts the data in descending order
(from largest to smallest)
:

Builds heap from initial
(unsorted)

data

Iteratively swaps the smallest element
(at index 0)

with last
unsorted

element

Adjust the heap after each swap, but only considers the
unsorted

data

View from Middle of Execution

Heap Analysis:
sort

Execution time:

Build heap:
n

calls to

=
n

log
n

Loop:
n

calls to

=
n

log
n

Total: 2
n

log
n

= O(
n

log
n
)

:

Same average as merge sort and quick sort

Doesn’t require extra space as the merge sort does

Doesn’t suffer if data is already sorted or mostly sorted

On the worksheet

I’m having you represent the heap as a tree (easier to
visualize than the array representation)

Build the initial heap

Then repeately remove the smallest element, then build
the heap again, until there are no elements in the tree.