Using a binary tree and value stored in array tell user that at what index the d
ID: 3670303 • Letter: U
Question
Using a binary tree and value stored in array tell user that at what index the demanded value can be possibly stored?
for example
int array[4] = {3,1,2,3,5}
Enter your value = 4
result :possible to store at index 4
//(because value 3 is less than 4 and is on index 3 so value 4 can be stored after that )
Enter your value =3
value already stored(return 1)
Enter your value = 6
result: possible to store at index 5(which is not available but still we have to show and also 6 is greater than 5 so would be able to store only after that)
Explanation / Answer
import java.util.Scanner;
Class Binary
{
public static void main(String args[])
{
int array[4]={3,1,2,3,5}
Scanner in = new Scanner(System.in);
System.out.printf("Enter your value ");
int n = in.nextInt();
for(int i=0;i<4;i++)
{
//checks already number exits in the array or not
if(a[i]==n)
{
System.out.println(i);
break;
}
//checks possiblity to store the index
//remeber here we have check the index position for the given tree but not adding it
if(a[i]<n&&a[i+1]>n)
{
System.out.println(("Possible to store index "+i);
}//if
}//for
}//main
}//class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.