Arrays java

I wanna pass the "mid" variable to an array and return the array, btw how can i do it also without using recursive calls.

Thanks in advance Vasconcelos.
public static void orderedTree(int min, int max)
        {
            int mid=0;
            if(max < min){
                return;
            }else{
                mid = min+(max-min)/2; //wanna pass the mid to and array and return it;
                // Pick the middle one "to the left"
                orderedTree(min,mid-1);

                // Pick the middle one "to the right"
                orderedTree(mid+1,max);
            }
        }

Open in new window

VasconcelosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

a_bCommented:
mid = min+(max-min)/2; //wanna pass the mid to and array and return it;
                // Pick the middle one "to the left"
                orderedTree(min,mid-1);  --.>You are already passing mid here.
       
Can you please tell us what is the purpose of the function?
 
0
VasconcelosAuthor Commented:
the function gives the positions of an array so that i can insert them in a binary search tree making it a balanced Binary search tree
0
a_bCommented:
Can you give me an example?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

VasconcelosAuthor Commented:
has it shows on the image the algorithm gives the POSITIONS of the array of which it will start to insert the numbers so that the tree will look like that.
BinTreemoving2montana.gif
0
VasconcelosAuthor Commented:
the algorithm can be wrong :P
0
a_bCommented:
I am still a bit confused - " passing "mid" variable to an array and return the array,"

Returning an array from a function is quite simple, but you cannot pass a variable to an array. You can pass a variable to the function -

public static int[] orderedTree(int min, int max, int[] arr, int mid)
{
    //Content goes here
    return arr[]
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VasconcelosAuthor Commented:
what if i wanna save the positions of the array on another array has integers?
0
a_bCommented:
You can do that. Just keep passing that array to your recursive functions.
0
VasconcelosAuthor Commented:

public static int[] orderedTree(int min, int max, int[] arr, int mid)
{
    //Content goes here
    return arr[]
}

this is in the class Ordered and my question is how do i call it on main or read it?
0
a_bCommented:
Since this is a static method - Ordered.ordered(...pass correct arguements here)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.