Created
March 15, 2017 02:22
-
-
Save tooshitaka/b8763daf9b85bc65438cac992f58860b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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