From 05dc0cbce844d62a6f63519e9ae26543548b6d42 Mon Sep 17 00:00:00 2001 From: Adeet Patel Date: Wed, 1 Jan 2020 13:14:03 -0500 Subject: [PATCH 1/5] Return 0 for computeDistance() --- labs/lab11/traveling-skeleton.cpp | 1 + 1 file changed, 1 insertion(+) 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 From 7d435c0ddda3053878b3bf4ab08b76b5bd9d660d Mon Sep 17 00:00:00 2001 From: Adeet Patel Date: Wed, 1 Jan 2020 15:06:30 -0500 Subject: [PATCH 2/5] Create tree from array --- slides/code/10-heaps-huffman/binary_heap.cpp | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/slides/code/10-heaps-huffman/binary_heap.cpp b/slides/code/10-heaps-huffman/binary_heap.cpp index f8a8ad07d..e251e58f6 100644 --- a/slides/code/10-heaps-huffman/binary_heap.cpp +++ b/slides/code/10-heaps-huffman/binary_heap.cpp @@ -97,6 +97,44 @@ 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 +} + void binary_heap::print() { cout << "(" << heap[0] << ") "; for ( int i = 1; i <= heap_size; i++ ) { From 8f39cf2fb6160a30317bac154fce765f16c3b49f Mon Sep 17 00:00:00 2001 From: Adeet Patel Date: Wed, 1 Jan 2020 15:13:50 -0500 Subject: [PATCH 3/5] Implement heap_node class --- slides/code/10-heaps-huffman/heap_node.cpp | 15 ++++++++++++++ slides/code/10-heaps-huffman/heap_node.h | 23 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 slides/code/10-heaps-huffman/heap_node.cpp create mode 100644 slides/code/10-heaps-huffman/heap_node.h 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 From 7c2640dcc6b6b6fc69bb6769bf7ee3375effd500 Mon Sep 17 00:00:00 2001 From: Adeet Patel Date: Wed, 1 Jan 2020 15:15:19 -0500 Subject: [PATCH 4/5] #include heap_node.h --- slides/code/10-heaps-huffman/binary_heap.cpp | 1 + slides/code/10-heaps-huffman/binary_heap.h | 2 ++ 2 files changed, 3 insertions(+) diff --git a/slides/code/10-heaps-huffman/binary_heap.cpp b/slides/code/10-heaps-huffman/binary_heap.cpp index e251e58f6..a47790614 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 diff --git a/slides/code/10-heaps-huffman/binary_heap.h b/slides/code/10-heaps-huffman/binary_heap.h index c4ee5f56a..dcecae120 100644 --- a/slides/code/10-heaps-huffman/binary_heap.h +++ b/slides/code/10-heaps-huffman/binary_heap.h @@ -6,6 +6,7 @@ #define BINARY_HEAP_H #include +#include "heap_node.h" using namespace std; class binary_heap { @@ -20,6 +21,7 @@ class binary_heap { unsigned int size(); void makeEmpty(); bool isEmpty(); + heap_node* createTree(); void print(); private: From 2fbff51dcd38c685b20314804353d221914a34b8 Mon Sep 17 00:00:00 2001 From: Adeet Patel Date: Wed, 1 Jan 2020 15:20:36 -0500 Subject: [PATCH 5/5] Implement print functionality (taken from lab 5) --- slides/code/10-heaps-huffman/binary_heap.cpp | 44 ++++++++++++++++---- slides/code/10-heaps-huffman/binary_heap.h | 11 +++++ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/slides/code/10-heaps-huffman/binary_heap.cpp b/slides/code/10-heaps-huffman/binary_heap.cpp index a47790614..a74f87589 100644 --- a/slides/code/10-heaps-huffman/binary_heap.cpp +++ b/slides/code/10-heaps-huffman/binary_heap.cpp @@ -136,14 +136,42 @@ heap_node* binary_heap::createTree() { 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 dcecae120..603397f66 100644 --- a/slides/code/10-heaps-huffman/binary_heap.h +++ b/slides/code/10-heaps-huffman/binary_heap.h @@ -9,6 +9,16 @@ #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(); @@ -23,6 +33,7 @@ class binary_heap { bool isEmpty(); heap_node* createTree(); void print(); + void print(heap_node* root, Trunk* prev, bool isRight); private: vector heap;