Skip to content

Instantly share code, notes, and snippets.

@tooshitaka
Created March 15, 2017 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tooshitaka/b8763daf9b85bc65438cac992f58860b to your computer and use it in GitHub Desktop.
Save tooshitaka/b8763daf9b85bc65438cac992f58860b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int s = 251;
int parent(int i) { return i / 2; }
int left(int i) { return 2 * i; }
int right(int i) { return 2 * i + 1; }
// MAIN FUNCTION
int main(int argc, char** argv)
{
int heap[s];
// Input
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> heap[i];
}
// Output
for (int i = 1; i <= n; i++) {
cout << "node " << i << ":";
cout << " key = " << heap[i] << ", ";
if (parent(i) >= 1) cout << "parent key = " << heap[parent(i)] << ", ";
if (left(i) <= n) cout << "left key = " << heap[left(i)] << ", ";
if (right(i) <= n) cout << "right key = " << heap[right(i)] << ", ";
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment