CISC220Lab8

advertisement
Lab 8:
(60 pts) Due Thursday, April 23
For this lab, you may work with a partner, or you may work on your own. This lab is due next Thursday.
Problem 1 ( 20 pts):
Implement the code for a binary heap using an array.
The array will be 13 in length, and should create a heap out of the following series of numbers:
27,69,87,95,5,85,93,78,58,12,51,2,38
(to make life easy, I’ve created a file called heapnums.txt and I’m including my main file that includes a function
for reading the numbers into your heap at the bottom of this lab).
Your code should include a function for inserting into and deleting from the heap. It should also include a function
for printing out the array (after it has been “heapified”).
Your results after reading in the numbers and turning them into a heap should be:
95 87 93 78 51 69 85 27 58 5 12 2 38
Now implement 3 deletes.
Your results should be:
93 87 85 78 51 69 38 27 58 5 12 2
87 78 85 58 51 69 38 27 2 5 12
85 78 69 58 51 12 38 27 2 5
Problem 2 (30 pts):
Now implement the binary heap as a binary tree with nodes that include links to 1 parent and two children (as we
did in a binary search tree). Implement the insert and the delete, and make sure both can be done in log n time.
Use the Printio function to print the tree out in order, after reading in the numbers from the file. After reading in
the file, your results should be the following:
27, 78, 58, 87, 5, 51, 12, 95, 2, 69, 38, 93, 85,
Note: you may have to modify my ReadHeapNumsFromFile() function slightly so that it inserts the values into your
Binary Heap as a linked tree if you named the insert function something different in the linked tree and your Heap
Tree class is something other than BH.
Hints for this one: There’s more than one way to do this. One way is to be aware that you can figure out where to
insert and delete using the knowledge that at every level in the tree, we can have 2height nodes. So, for instance,
when the height of the tree is 0, we have 20 nodes, and at height of 1, we have 21 nodes, at height 2, we add 22
nodes, etc. So if we know how many nodes, and we know the tree is complete, we can know how many levels
down we need to go and even which path to travel down at each level to get to the next empty node at that
bottom level.
After each delete, the Printio should result in the following:
27,78,58,87,5,51,12,93,2,68,85,38
27,58,2,78,5,51,12,87,69,85,38
27,58,2,78,5,51,85,12,69,38
In this case, which do you prefer coding, the linked list or the array?
Problem 3 (10 pts):
Finding the kth smallest element in a set of numbers:
Write code that uses a binary heap to efficiently find the 6 th smallest element in a series of numbers, regardless
of how many numbers you input.
To be more specific, you’ll have a main program that takes as an input a number between 7 and 50. It then
generates that many random numbers. It then creates a binary heap that has 6 values (it can be either of your
versions, above). It then checks and inserts the next values as needed. When finished, the root should be the
kth smallest value.
Note: print out your values as you generate them or you won’t be able to tell if your program works correctly.
#include
#include
#include
#include
#include
#include
"BH.hpp"
<iostream>
<stdlib.h>
<time.h>
<fstream>
"NodeHeap.hpp"
//void readandwritefile(string oldfile, string newfile);
using namespace std;
void readHeapNumsFromFile (string heapfile, BH *heap);
int main() {
BH *heap = new BH();
readHeapNumsFromFile("heapnums2.txt",heap);
heap->printTree();
heap->remove();
heap->printTree();
heap->remove();
heap->printTree();
heap->remove();
heap->printTree();
return 0;
}
void readHeapNumsFromFile (string heapfile, BH *heap) {
ifstream file(heapfile.c_str());
int x;
while (!file.eof()) {
file >>x;
if (!file.eof()) {
heap->insert(x);
}
}
return;
}
Download