Skip to content

Print binary heap in tree form #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions labs/lab11/traveling-skeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> dests) {
// YOUR CODE HERE
return 0;
}

// This method will print the entire route, starting and ending at the
Expand Down
83 changes: 75 additions & 8 deletions slides/code/10-heaps-huffman/binary_heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <iostream>
#include "binary_heap.h"
#include "heap_node.h"
using namespace std;

// default constructor
Expand Down Expand Up @@ -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);
}
13 changes: 13 additions & 0 deletions slides/code/10-heaps-huffman/binary_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
#define BINARY_HEAP_H

#include <vector>
#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();
Expand All @@ -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<int> heap;
Expand Down
15 changes: 15 additions & 0 deletions slides/code/10-heaps-huffman/heap_node.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#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;
}
23 changes: 23 additions & 0 deletions slides/code/10-heaps-huffman/heap_node.h
Original file line number Diff line number Diff line change
@@ -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 <vector>
using namespace std;

class heap_node {
public:
heap_node();

private:
int value;
heap_node* left;
heap_node* right;

friend class binary_heap;
};

#endif