We have a given array and we want to print each node's level in the BST.
For example if the given array is: {15, 6, 2, 10, 9, 7, 13}
then the answer is:
1 2 3 3 4 5 4
(it means that the level of node that stores 15 is 1 and ...)
I have some algorithms in my mind but I don't know how to implement them in code.
These are the steps that you should follow :
Create a binary search tree from the elements given in the array.
Write a function findLevel( Node root, int value) to find the level of any value passed to this function.
Iterate the array and pass each array element as an argument to findLevel( Node root, int value) and print the values returned from the function.
Related
If I have an array that looks like this:
int[] arr = {6, 12, 3, 9, 8, 25, 10};
Why does this return -2:
Arrays.binarySearch(arr, 8);
I understand that binarySearch only works if the array is sorted. My question is what determines the returned index?
As #assylias mentioned in the comments the documentation for binarySearch I can quote from it
Returns:
index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
So basically this is what happens in your attempt to search unsorted array:
{6, 12, 3, 9, 8, 25, 10}
it takes middle element 9 and compares it to your searched element 8, since 8 is lower it takes lower half {6, 12, 3}
in second step it compares 12 to 8 and since 8 is again lower it takes lower half {6}
6 doesn't equal 8 and since it's the last element it didn't find what you wanted
it returns (-(insertion point) - 1) where insertion point is
the index of the first element in the range greater than the key
in your case that index is 1 since first element greater then 8 is 12 and it's index is 1
when you put that into equation it returns (-1 - 1) which equals -2
Hope I have answered your question.
Suppose that there's a binary tree like the one below that needs to be stored in an array.
7
/ \
1 10
/\
9 11
And I found that the formula for storing the nodes in the array begins with storing the root node at position 0, and then for every node at index i, its children are placed at indices (i*2)+1 and (i*2)+2. And if the index of either child is greater than the array.length - 1, then that node does not have children.
So I begin by putting 7 at position 0, then its children 1 and 10 at position i2+1 and i2+2 which would be 1 and 2:
|7|1|10| | | |
0 1 2 3 4 5
Now, I'm stuck with node 1 which does not have any children. What should I put as its children?
Is it OK to put some default value that would represent the absence of a node, for example -1, like this:
|7|1|10|-1|-1|9|11|
0 1 2 3 4 5 6 7
This algorithm for storing a binary tree in an array is designed for trees such that every branch of the tree starting from the root node is of the same length: the array size is based on the greatest depth within the tree, and then it assigns an array position for every tree position of equal or lesser depth. If you have many different branch lengths, this may not be the correct algorithm for you. If your branch lengths are mostly the same depth but are sometimes empty near the end of the tree, placing a 'null' value such as -1 or Integer.MIN_VALUE may be an appropriate solution, as long as you know that you will not normally need to place any -1 values into the tree.
If you happen to know that you will only be missing elements from the greatest depth level of your tree (as in the example you provided), and the left/right order of the tree does not matter, you can instead simply reorder your tree such that the empty values are always in the bottommost, rightmost positions, which is also the set of positions at the end of your array, thus making your tree a complete binary tree. Then, you need only remember the number of elements in the tree, which is one greater than the index of the last non-null value. Diagrammed:
7
/ \
10 1
/\
9 11
-->
|7|10|1|9|11|0|0|
0 1 2 3 4 5 6
length = 5 or lastIndex = 4
I was thinking of using values which will break binary tree property at this point. In a general binary tree Left is always smaller and right is bigger than current node. If encounter higher on left or lower on right means end nodes.
I want to create a set of elements from the given input.
(i.e. i/p : 5 5 4 4 4 3 3 3 3 2 2 2 1 1 1 ,then o/p : 5 4 3 2 1)
I have a logic: create an array and store the elements in it.successively read elements and write a loop which will assign boolean false if two elements (the current chosen element and an element from the array whose index is less than that of the current element) are not the same. After the loop has executed for an element , all the boolean values stored are passed through bitwise OR operation and if the overall value is false , then the current element is pushed to an array that stores the set and the next element is the given sequence is chosen and same operation is performed.
I haven't yet written the code for this. So, is this algo. right? Also, do you know a better algo. to find a set?
Thanks.
You never need to use arrays for this sort of thing. Learn about Lists and Sets and use those instead.
Set<Integer> set = new LinkedHashSet<Integer>(Arrays.asList(5, 5, 4, 4, 3, 2, 4, 3, 1));
System.out.println(set); // prints [5, 4, 3, 2, 1]
You have k lists of sorted integers. Find the smallest range that includes at least one number from each of the k lists.
For example,
List 1: [4, 10, 13, 14]
List 2: [0, 9, 15, 18]
List 3: [5, 18, 22, 30]
The smallest range here would be [14, 18] as it contains 14 from list 1, 15 from list 2, and 18 from list 3.
MY approach is:
Just use a MinHeap and insert the first elements from K lists
Remove the the min element and add the next element from the corresponding list
Simultaneously track the max and min value so that we can calculate the minimum range
But the only issue I am facing is: Suppose for one list there is no more elements left than should I finish there or should I continue?
Very nice O(n log n) algorithm!
You can finish there because you will never find the better interval fulfilling the given condition "range that includes at least one number from each of the k lists".
Suppose you are leaving current minimum m (the last element from some list) and instead you are removing something (not minimum) from another list. In that case the range can only grow (because minimum of the range is determined by m). So there is no point in doing that and you can just stop your algorithm.
No, that is not your terminating condition. Look at this example:
1: [0]
2: [1]
The range is quite clearly [0,1], but if you stopped as soon as you detected an empty list, you would return [0,0].
So, you can only stop once you know you have seen values from all k lists and one of the lists has run out of items. If you are keeping track of the min- and max-values for each list separately, this should be pretty easy, seeing as you can just make sure there is some min- and max-value for each list.
I want to obtain the minimum of a double array and the other two minimum values. In total I wan to obtain the 3 smaller values of the array. I am not using the class array, but I am using a double[].
Easiest way is to call
Arrays.sort()
and take the first 3 values.
Otherwise, you can simply loop through the array and keep track of the three smallest, much like you would the smallest.
double[] dlist = {17.0, 10.0, 44, 7, 4.0, 33, 24, 10, 48.0, 49.0};
Arrays.sort (dlist);
System.out.println (dlist [0] + " " + dlist [1] /*...*/);
Similarly to above, you could loop through and store the smallest one, then remove from the array. Then do it again, and again. But I think the ways mentioned above are more efficient.
Well, if you can't use the Arrays class at all, you will probably want 3 variables, one to hold each of the values you are trying to get. Just start off by setting them equal to the first 3 elements in the array (if there are at least 3, otherwise just set a few of them).
Then use a for loop to go through the rest of the elements in the array. If an element is smaller than one or more of the numbers you already found, get rid of the largest of those numbers and add this one to the list of smallest numbers instead.
1. declare 3 variables
2. set variables equal to first 3 elements in array
3. loop from index 3 (4th element) to the length of the array
a. see which of the already found numbers is bigger than the current element (if any)
b.replace the biggest of the found numbers with the new number if at least one was found
4. print out or return the numbers you found