Following my previous question, I would like now to put the Binary Tree values in a sorted array.
So, first I used my numOfNodeswn function that counts total sum of nodes in my tree,
I created an array according to the result of this function and I started to think that Instead of locating the minimum value of the tree and making not-that-easy successor function, I can simply take advantge of the utility of this kind of tree and make a kind of Inorder process, which during it I can put the correct values into my array.
My main problem is to control of variable i- which is the index according to it, I will know exactly where to put the correct values.
(header is the the head of the tree)
This is what I wrote so far:
public double[] toDoubleArray() {
double[] arr = new double[numOfNodeswn(header)];
int i=0;
return putvalues(arr, i, header);
}
private double[] putvalues(double[] arr, int i, RBNode t) {
if (t!=null){
putvalues (arr, i, t.left);
arr[i]=t.value;
i++;
putvalues (arr, i, t.right);
}
return arr;
}