ok so i have a binary tree(not a search one)
i declared it using an array like this
a[7]=0 1 2 3 4 5 6//lets say
1 2 3 4 5 6 7
node 0 is the root and has the key 1
and i constructed the array like this [2*i+1]=left node key and [2*i+2]=right node key
the question is
i need to make an algorithm,exactly an bfs , over an height...lets say if i have
1 2 3 4 5 6 7 array and i want to make the sum of nodes with height 2
it will return me 4 5 6 7
its not hard but my theacher want to implement recursive and i kinda suck at recursive implementation
this is the code i've tried to implement...
Code:
#include "iostream"
#define MAX 1000
#define sent 777
using namespace std;
int arb[MAX];
void calculation(int h,int lvl,int y);
int main()
{
int i=0,k=0,h,lvl=0,y=1;
cout<<"give the root a value ";
cin>>arb[0];
cout<<"give root brother a value ";
cin>>arb[2*i+1]>>arb[2*i+2];
for(i=1;i<MAX;i++)
{
cout<<"give value to the brothers of "<<arb[i]<<" ";
cin>>arb[2*i+1]>>arb[2*i+2];
cout<<"you want to introduce another node ?0 yes 1 no";
cin>>k;
if(k==1)
break;
}
k=2*i+2;
for(i=0;i<=k;i++)
cout<<arb[i]<<" ";
cout<<"introduce the height for calculation ";
cin>>h;
for(int j=1;j<h;j++)
{
lvl=2*lvl+1;
j++;
}
calculat(h,lvl,y);
return 0;
}
void calculation(int h,int lvl,int y)
{
int k=2;
cout<<arb[2*lvl+1]<<arb[2*lvl+2];
if(y<k)
{
y++;
lvl++;
calculat(h,lvl,y);
}
else
return;
}