diff --git a/labs/lab11/traveling-skeleton.cpp b/labs/lab11/traveling-skeleton.cpp index 7736ff618..4d179066a 100644 --- a/labs/lab11/traveling-skeleton.cpp +++ b/labs/lab11/traveling-skeleton.cpp @@ -36,6 +36,7 @@ int main (int argc, char **argv) { // vector IN ORDER, and ends back at the 'start' parameter. float computeDistance (MiddleEarth &me, string start, vector dests) { // YOUR CODE HERE + return 0; } // This method will print the entire route, starting and ending at the diff --git a/slides/code/10-heaps-huffman/binary_heap.cpp b/slides/code/10-heaps-huffman/binary_heap.cpp index f8a8ad07d..a74f87589 100644 --- a/slides/code/10-heaps-huffman/binary_heap.cpp +++ b/slides/code/10-heaps-huffman/binary_heap.cpp @@ -4,6 +4,7 @@ #include #include "binary_heap.h" +#include "heap_node.h" using namespace std; // default constructor @@ -97,14 +98,80 @@ bool binary_heap::isEmpty() { return heap_size == 0; } +heap_node* binary_heap::createTree() { + // Create all heap nodes and store them in an array + heap_node* nodes[heap_size+1]; // array containing all heap nodes + nodes[0] = NULL; // ignore the zero index + for (int i = 1; i <= heap_size; i++) { + heap_node* n = new heap_node; + n->value = heap[i]; + nodes[i] = n; + } + cout << endl; + + // Create the tree + // For every node x, we want to insert its left and right child + for (int i = 1; i <= heap_size; i++) { + heap_node* current = nodes[i]; + int leftChildIndex = 2*i; + int rightChildIndex = 2*i + 1; + + // If the index of the left child is larger than the number of + // elements in the heap, it means that the current node doesn't + // have a left child. + if (leftChildIndex >= heap_size+1) + current->left = NULL; + else + current->left = nodes[leftChildIndex]; + + // If the index of the right child is larger than the number of + // elements in the heap, it means that the current node doesn't + // have a right child. + if (rightChildIndex >= heap_size+1) + current->right = NULL; + else + current->right = nodes[rightChildIndex]; + } + + return nodes[1]; // return the root node of the tree +} + +// Helper function to print branches of the binary tree +void showTrunks(Trunk* p) { + if (p == nullptr) return; + showTrunks(p->prev); + cout << p->str; +} + void binary_heap::print() { - cout << "(" << heap[0] << ") "; - for ( int i = 1; i <= heap_size; i++ ) { - cout << heap[i] << " "; - // next line from http://tinyurl.com/mf9tbgm - bool isPow2 = (((i+1) & ~(i))==(i+1))? i+1 : 0; - if ( isPow2 ) - cout << endl << "\t"; + print(createTree(), NULL, false); +} + +// Recursive function to print binary tree +// It uses inorder traversal +void binary_heap::print(heap_node* root, Trunk* prev, bool isRight) { + if (root == NULL) return; + + string prev_str = " "; + Trunk* trunk = new Trunk(prev, prev_str); + + print(root->right, trunk, true); + + if (!prev) + trunk->str = "---"; + else if (isRight) { // github user @willzhang05 pointed out that I forgot to change this from isLeft to isRight on my first commit + trunk->str = ".---"; + prev_str = " |"; + } else { + trunk->str = "`---"; + prev->str = prev_str; } - cout << endl; + + showTrunks(trunk); + cout << root->value << endl; + + if (prev) prev->str = prev_str; + trunk->str = " |"; + + print(root->left, trunk, false); } diff --git a/slides/code/10-heaps-huffman/binary_heap.h b/slides/code/10-heaps-huffman/binary_heap.h index c4ee5f56a..603397f66 100644 --- a/slides/code/10-heaps-huffman/binary_heap.h +++ b/slides/code/10-heaps-huffman/binary_heap.h @@ -6,8 +6,19 @@ #define BINARY_HEAP_H #include +#include "heap_node.h" using namespace std; +struct Trunk { + Trunk* prev; + string str; + + Trunk(Trunk* prev, string str) { + this->prev = prev; + this->str = str; + } +}; + class binary_heap { public: binary_heap(); @@ -20,7 +31,9 @@ class binary_heap { unsigned int size(); void makeEmpty(); bool isEmpty(); + heap_node* createTree(); void print(); + void print(heap_node* root, Trunk* prev, bool isRight); private: vector heap; diff --git a/slides/code/10-heaps-huffman/heap_node.cpp b/slides/code/10-heaps-huffman/heap_node.cpp new file mode 100644 index 000000000..21fb52bb6 --- /dev/null +++ b/slides/code/10-heaps-huffman/heap_node.cpp @@ -0,0 +1,15 @@ +// Code written by Aaron Bloomfield, 2014 +// Released under a CC BY-SA license +// This code is part of the https://github.com/aaronbloomfield/pdr repository + +#include +#include "binary_heap.h" +#include "heap_node.h" +using namespace std; + +// default constructor +heap_node::heap_node() { + this->value = 0; + this->left = NULL; + this->right = NULL; +} diff --git a/slides/code/10-heaps-huffman/heap_node.h b/slides/code/10-heaps-huffman/heap_node.h new file mode 100644 index 000000000..ff4d31406 --- /dev/null +++ b/slides/code/10-heaps-huffman/heap_node.h @@ -0,0 +1,23 @@ +// Code written by Aaron Bloomfield, 2014 +// Released under a CC BY-SA license +// This code is part of the https://github.com/aaronbloomfield/pdr repository + +#ifndef HEAP_NODE_H +#define HEAP_NODE_H + +#include +using namespace std; + +class heap_node { +public: + heap_node(); + +private: + int value; + heap_node* left; + heap_node* right; + + friend class binary_heap; +}; + +#endif