Write code to PRINT all items of ARRAY BASED REPRESENTATION of a binary tree using NON-RECURSIVE pre-order traversal
QUESTION
You are given an ARRAY BASED
REPRESENTATION of a binary tree of integer type. Write code to PRINT all items of this tree
using NON-RECURSIVE pre-order traversal.
Solution:
void
Print(int array[], int size){
Stach S;
int i =0;
int count =0;
while (count<size){
if(i<size){
cout<<array[i];
S.push(i);
i
= 2*i+1;
count++;
}
else{
X
= S.pop();
i
= 2*x+2;
}
}
}
|
Comments
Post a Comment