#include <stdio.h>
size_t size = 13;
int64_t tree[] = {1, 4, -3, 5, 2, -4, -5, 3, 5, 2, 4, 0, 5}; // 13
inline size_t parent(size_t i) { return (i - 1) >> 1; }
inline size_t left(size_t i) { return (i << 1) + 1; }
inline size_t right(size_t i) { return (i << 1) + 2; }
void preOrderTreversal(size_t i) {
if (i < size) {
printf("%" PRId64 " ", tree[i]);
preOrderTraversal(left(i));
preOrderTraversal(right(i));
}
}
void postOrderTraversal(size_t i) {
if (i < size) {
preOrderTraversal(left(i));
preOrderTraversal(right(i));
printf("%" PRId64 " ", tree[i]);
}
}
void inOrderTraversal(size_t i) {
if (i < size) {
preOrderTraversal(left(i));
printf("%" PRId64 " ", tree[i]);
preOrderTraversal(right(i));
}
}
int main() {
preOrderTraversal(0);
printf("\n");
inOrderTraversal(0);
printf("\n");
postOrderTraversal(0);
return 0;
}
Comments
Leave a comment