?
Solved

Arrays java

Posted on 2010-04-02
10
Medium Priority
?
226 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:Vasconcelos
  • 5
  • 5
10 Comments
 
LVL 20

Expert Comment

by:a_b
ID: 29469586
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
 

Author Comment

by:Vasconcelos
ID: 29470881
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
 
LVL 20

Expert Comment

by:a_b
ID: 29472015
Can you give me an example?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:Vasconcelos
ID: 29473691
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
 

Author Comment

by:Vasconcelos
ID: 29473785
the algorithm can be wrong :P
0
 
LVL 20

Accepted Solution

by:
a_b earned 2000 total points
ID: 29474113
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
 

Author Comment

by:Vasconcelos
ID: 29474407
what if i wanna save the positions of the array on another array has integers?
0
 
LVL 20

Expert Comment

by:a_b
ID: 29475060
You can do that. Just keep passing that array to your recursive functions.
0
 

Author Comment

by:Vasconcelos
ID: 29475168

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
 
LVL 20

Expert Comment

by:a_b
ID: 29475651
Since this is a static method - Ordered.ordered(...pass correct arguements here)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question