I need to print out a binary tree that looks like this:
--------x-------
----x-------x---
--x---x---x---x-
-x-x-x-x-x-x-x-x
xxxxxxxxxxxxxxxx
Using recursion to print the left side of the line and the right side of the line with the exception of the first line. So the function would call a display function with parameters of the left starting point and the right ending point. Then it calls itself twice, on for the left side and one for the right.
#include <stdio.h>
#define LENGTH 16
void makeBranches(int, int);
void display(int, int);
int main(){
makeBranches(0, LENGTH-1);
}
void makeBranches(int left, int right){
if(left >= right){
return;
} else{
display(left, right);
makeBranches(left, (right+left)/2);
makeBranches((right+left)/2+1, right);
}
}
void display(int left, int right){
int mid = (left+right)/2;
int i;
for(i = left; i <= right; i++){
if(i == mid)
printf("X");
else
printf("-");
}
if(right == LENGTH-1)
printf("\n");
}
This is currently what my code looks like, although it has changed many times.
I cannot figure out how to get the first call of makeBranches execute and then the second call. Right now it only does the left side calls and looks like this:
-------X--------
---X-----X--X-