Size Above Depth and size below Depth - java

I am a beginner of Java, and i was trying to write a code to count the number of nodes whose depth is smaller than a given depth and number of nodes whose depth is bigger a given depth.
public static int sizeAboveDepth(Node t, int k) {
// TODO: Complete this method.
if (t == null) {
return 0;
}
if (t.key > k)
return (sizeAboveDepth(t.left, k - 1) + sizeAboveDepth(t.right, k - 1) + 1);
else {
return sizeAboveDepth(t.left, k - 1) + sizeAboveDepth(t.right, k-1);
}
}
// EXERCISE 6: The method "sizeBelowDepth" is described above.
// The number of nodes in the tree "below" depth k (not including k)
// include node n if depth(n) > k.
public static int sizeBelowDepth(Node t, int k) {
// TODO: Complete this method.
if (t == null) {
return 0;
}
if (t.key > k)
return (sizeBelowDepth(t.left, k-1) + sizeBelowDepth(t.right, k-1) + 1);
else {
return sizeBelowDepth(t.left, k-1) + sizeBelowDepth(t.right, k-1);
}
}
so is there anyone can help me to fix it? I am kind of crazy about it now~~

public static int sizeAboveDepth(Node t, int depth, final int k) {
if ( t != null && depth < k){
return sizeAboveDepth( t.left, depth+1, k) +
sizeAboveDepth( t.right, depth+1, k) + 1;
} else {
return 0;
}
}
public static int sizeBelowDepth(Node t, int depth, final int k) {
if (t == null) {
return 0;
}
int below = sizeBelowDepth(t.left, depth+1, k) +
sizeBelowDepth(t.right, depth+1, k);
if (depth > k){
below++;
}
return below;
}

public static int sizeAboveDepth(Node t, int depth, static int k) {
// TODO: Complete this method.
if (t == null) {
return 0;
}
if (depth < k)
return (sizeAboveDepth(t.left,depth+1, k ) + sizeAboveDepth(t.right, depth+1, k) + 1);
else {
return 0
}
}
public static int sizeBelowDepth(Node t, int depth,static int k) {
// TODO: Complete this method.
if (t == null) {
return 0;
}
if (depth > k)
return (sizeBelowDepth(t.left, depth+1, k) + sizeBelowDepth(t.right, depth+1, k) + 1);
else {
return sizeBelowDepth(t.left,depth++, k) + sizeBelowDepth(t.right,depth++, k);
} }
I'm not sure, but I think this can help you.

Related

Hybrid sorting using insertion sort and merge sort

I wish to implement a hybrid algorithm that switches from insertion sort to merge sort once the input array size becomes too big.
This is my main function (I fixed my input array size at 30 currently as I wish to test my merge sort function) :
public static void main(String[] args) {
int[] a = genrandarray(30);
long bgn, end;
bgn = System.currentTimeMillis();
imsort(a, 0, a.length, 30);
end = System.currentTimeMillis();
for(int i = 1; i < a.length; i++){
if(a[i-1] > a[i]){
System.out.println("failed");
break;
}
}
System.out.println("milliseconds " + (end-bgn));
}
Insertion sort is called when the array size is <20, else merge sort is called:
public static void imsort(int [] slot, int b, int e, int size) {
//if smaller than 20, use insertion sort
if (e-b<=20) {
insertionSort(slot, e); //e is the length of slot[]
System.out.println("imsort entered!");
}
else {
mergesort(b, e, slot);
System.out.println("mergesort entered!");
}
}
There is a Index 30 out of bounds error for my merge sort function currently.
public static int merge(int n, int m, int[] slot) {
int mid = (n+m)/2;
int a = n, b = mid+1, i, tmp, cmp=0, comp=0;
//sanity check
if (m-n <= 0) return -1000;
while (a <= mid && b <= m) {
cmp = compare(slot[a], slot[b]);
comp++;
if (cmp > 0) { //slot[a] > slot[b]
tmp = slot[b++];
for (i = ++mid; i > a; i--)
slot[i] = slot[i-1];
slot[a++] = tmp;
}
else if (cmp < 0) //slot[a] < slot[b]
a++;
else {
//slot[a] == slot[b]
if (a == mid && b == m) break;
tmp = slot[b++];
a++;
for (i = ++mid; i > a; i--)
slot[i] = slot[i-1]; slot[a++] = tmp;
}
} // end of while loop;
return comp;
} // end of merge
public static int mergesort(int s, int e, int[] slot) {
//int comp =0;
int mid = (s+e)/2;
int right=0, left=0;
if(e-s>0) {
//comp++;
left = mergesort(s,mid, slot);
//comp++;
right = mergesort(mid+1,e, slot);
}
return right + left + merge(s,e,slot);
}
I am unsure of the error in my merge / mergesort function. Can I get some further advice?
a.length returns you 30 which is the length of your random array from the genrandarray method i believe. And your array is indexed 0 through 29. Try changing the main method like this and it will work out
public static void main(String[] args) {
int[] a = genrandarray(30);
long bgn, end;
bgn = System.currentTimeMillis();
imsort(a, 0, a.length-1, 30);
end = System.currentTimeMillis();
for(int i = 1; i < a.length; i++){
if(a[i-1] > a[i]){
System.out.println("failed");
break;
}
}
System.out.println("milliseconds " + (end-bgn));
}
First, let me congratulate on a very good post for someone new. You didn't post the line of code where the error is happening and code isn't complete, so I filled in some blanks and executed it:
import java.util.Arrays;
import java.util.Random;
public class Test {
public static int[] genrandarray(int n)
{
int[] a = new int[n];
Random r = new Random();
for(int i=0;i<n;i++) a[i] = r.nextInt();
return a;
}
public static void main(String[] args) {
int[] a = genrandarray(30);
long bgn, end;
bgn = System.currentTimeMillis();
imsort(a, 0, a.length, 30);
end = System.currentTimeMillis();
for(int i = 1; i < a.length; i++){
if(a[i-1] > a[i]){
System.out.println("failed");
break;
}
}
System.out.println("milliseconds " + (end-bgn));
}
public static void imsort(int [] slot, int b, int e, int size) {
//if smaller than 20, use insertion sort
if (e-b<=20) {
Arrays.sort(slot, 0, e);
System.out.println("imsort entered!");
}
else {
mergesort(b, e, slot);
System.out.println("mergesort entered!");
}
}
public static int merge(int n, int m, int[] slot) {
int mid = (n+m)/2;
int a = n, b = mid+1, i, tmp, cmp=0, comp=0;
//sanity check
if (m-n <= 0) return -1000;
while (a <= mid && b <= m) {
cmp = slot[a] - slot[b];
comp++;
if (cmp > 0) { //slot[a] > slot[b]
tmp = slot[b++];
for (i = ++mid; i > a; i--)
slot[i] = slot[i-1];
slot[a++] = tmp;
}
else if (cmp < 0) //slot[a] < slot[b]
a++;
else {
//slot[a] == slot[b]
if (a == mid && b == m) break;
tmp = slot[b++];
a++;
for (i = ++mid; i > a; i--)
slot[i] = slot[i-1]; slot[a++] = tmp;
}
} // end of while loop;
return comp;
} // end of merge
public static int mergesort(int s, int e, int[] slot) {
//int comp =0;
int mid = (s+e)/2;
int right=0, left=0;
if(e-s>0) {
//comp++;
left = mergesort(s,mid, slot);
//comp++;
right = mergesort(mid+1,e, slot);
}
return right + left + merge(s,e,slot);
}
}
The error is caused by setting variable a to n in your merge function and then in the line cmp = slot[a] - slot[b]. Because arrays go from 0 to n-1, n will cause an out of bounds exception.

using comparable in java for Bubble up in MaxHeap

I am trying to insert in a maxHeap in java and then bubble up the object. This is what I have done, I am not sure how I should approach the bubble up method.
I do understand the algorithm behind bubble up, which is as follows:
get parent node
see if L_childNode is less than parent Node. If Yes, then swap parent with L_child.
see if R_childNode is less than parent Node. If Yes, then swap parent with L_child.
Please point out what am I doing wrong?
private int getLeftChild(int n){
return x*2+1;
}
private int getRightChild(int n){
return x*2+2;
}
public void insert (E item) {
//Integer pos_lastEl= new Integer (heapArray.lastElement());
heapArray.add(item);
bubbleUp(item);
}
//To use to reheap up when item inserted at end of heap (complete tree)
private void bubbleUp(E x){
int place = heapArray.size()-1;
int parent=(place-1)/2;
if ((parent>=0) && (parent.compareTo(heapArray.get(getLeftChild))<0)){
swap(place,parent);
}else ((parent>=0 && (parent.compareTo(heapArray.get(getRightChild))<0))){
swap(place,parent);
}
}
//swaps two objects at index i and j
private void swap(int i, int j){
int max=heapArray.size();
if(i>=0 && i<max && j>=0 && j<max){
E temp=heapArray.get(i);
//put J item in I
heapArray.set(i,heapArray.get(j));
heapArray.set(j,temp);
}
}
Your major problem is using if instead of while to bubble up the newly added element to the proper position.
And there are also some other issues in your code, sorry I had to do some refactoring to make it clean enough:
public class MaxHeapTest<E extends Comparable<E>> {
List<E> heapArray = new ArrayList<>();
public static void main(String... args) {
int N = 13;
MaxHeapTest<Integer> maxHeap = new MaxHeapTest();
for (int i = 0; i < N; ++i) { // ascending;
maxHeap.insert(i);
}
while (!maxHeap.isEmpty()) { // descending now;
System.out.print(maxHeap.delMax() + " ");
}
}
public E delMax() {
E e = heapArray.get(0);
swap(0, heapArray.size() - 1);
heapArray.remove(heapArray.size() - 1);
sinkDown(0);
return e;
}
public void insert(E item) {
heapArray.add(item);
bubbleUp(item);
}
public boolean isEmpty() {
return heapArray.isEmpty();
}
private void bubbleUp(E x) {
int k = heapArray.indexOf(x);
int j = (k - 1) / 2;
while (j >= 0) {
if (heapArray.get(j).compareTo(heapArray.get(k)) < 0) {
swap(k, j);
k = j;
j = (j - 1) / 2;
} else break;
}
}
private void sinkDown(int k) {
int j = 2 * k + 1;
while (j < heapArray.size()) {
if (j < heapArray.size() - 1 && heapArray.get(j).compareTo(heapArray.get(j + 1)) < 0) j++;
if (heapArray.get(k).compareTo(heapArray.get(j)) < 0) {
swap(k, j);
k = j;
j = 2 * j + 1;
} else break;
}
}
private void swap(int i, int j) {
E temp = heapArray.get(i);
heapArray.set(i, heapArray.get(j));
heapArray.set(j, temp);
}
}
After the maxHeap, we can easily output the descending numbers as:
12 11 10 9 8 7 6 5 4 3 2 1 0

tree: root to leaf sum (recursion)

the problem is: calculate the total sum of all root-to-leaf numbers. for example: if the tree is (1,2,3), 1 is root, 2 is left child, 3 is right child, two paths: 1->2 1->3, sum=12+13=25
this is my correct recursive solution. in the helper method, return the sum:
public int sumNumbers(TreeNode root) {
if (root == null) {
return 0;
}
return getSum(root, 0);
}
private int getSum(TreeNode root, int value) {
if (root.left == null && root.right == null) {
return root.val + value * 10;
}
int sum = 0;
if (root.left != null) {
sum += getSum(root.left, value * 10 + root.val);
}
if (root.right != null) {
sum += getSum(root.right, value * 10 + root.val);
}
return sum;
}
but when I add the sum as a parameter in the helper method, I always got 0.
public int getSum(TreeNode root) {
int sum = 0, path = 0;
helper(root, path, sum);
return sum;
}
private void helper(TreeNode root, int path, int sum) {
if (root == null) {
return;
}
int path = 10 * path + root.val;
if (root.left == null && root.right == null) {
sum += path;
return;
}
helper(root.left, path, sum);
helper(root.right, path, sum);
}
I believe there must be some points I misunderstand about the recursion. thank you in advance to give me some explanation why the value of sum is not 'transferred' back to the sum in getSum method.
Also you need to think about overflow. My solution has passed in LeetCode, hopefully it gives you some tips.
public class Solution {
private long sum = 0;
public int sumNumbers(TreeNode root) {
if(root == null) return 0;
sum(root, new Stack<Integer>());
if(this.sum >= Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
return (int)this.sum;
}
private void sum(TreeNode node, Stack<Integer> stack){
if(node == null) return;
stack.push(node.val);
if(node.left == null && node.right == null){
long tempSum = 0;
int index = 0;
for(int i=stack.size()-1;i>=0;i--){
int k = stack.get(i);
int times = (int)Math.pow(10, index++);
k *= times;
tempSum += k;
}
this.sum += tempSum;
}
sum(node.left, stack);
sum(node.right, stack);
if(stack.size() > 0)
stack.pop();
}
}
ZouZou is right about the pass by value, although this only applies to primitives. Changing your sum to an Integer instead of int should do the trick, other solution would be to us a global variable (i.e. field)

How to use recursion in creating a binary search algorithm

I have been using my time off university to practice Java through coding algorithms. One of the algorithms I coded was the binary search:
public class BinarySearch {
private static int list[] = {3, 6, 7, 8, 9, 10};
public static void main(String[] args) {
BinarySearch b = new BinarySearch();
b.binarySearch(list);
}
public void binarySearch(int[] args) {
System.out.println("Binary search.");
int upperBound = args.length;
int lowerBound = 1;
int midpoint = (upperBound + lowerBound) / 2;
int difference = upperBound - lowerBound;
int search = 7;
for (int i = 0; i < args.length; i++) {
if (search < args[midpoint - 1] && difference != 1) {
upperBound = midpoint - 1;
midpoint = upperBound / 2;
} else if (search > args[midpoint - 1] && difference != 1) {
lowerBound = midpoint + 1;
midpoint = (lowerBound + upperBound) / 2;
} else if (search == args[midpoint - 1]) {
midpoint = midpoint - 1;
System.out.println("We found " + search + " at position " + midpoint + " in the list.");
i = args.length;
} else {
System.out.println("We couldn't find " + search + " in the list.");
i = args.length;
}
}
}
}
I really want to be able to write a much cleaner and efficient binary search algorithm, an alternative to what I've coded. I have seen examples of how recursion is used such as when doing factorial with numbers which I understand. However when coding something of this complexity I am confused on how to use it to my advantage. Therefore my question is how do I apply recursion when coding a binary search algorithm. And if you have any tips for me to perfect my recursion skills even if it has to be something that doesn't regard to binary search then please feel free to post.
If you really want to use recursion, this should do it.
public static int binarySearch(int[] a, int target) {
return binarySearch(a, 0, a.length-1, target);
}
public static int binarySearch(int[] a, int start, int end, int target) {
int middle = (start + end) / 2;
if(end < start) {
return -1;
}
if(target==a[middle]) {
return middle;
} else if(target<a[middle]) {
return binarySearch(a, start, middle - 1, target);
} else {
return binarySearch(a, middle + 1, end, target);
}
}
Here is an easier way of doing binary search:
public static int binarySearch(int intToSearch, int[] sortedArray) {
int lower = 0;
int upper = sortedArray.length - 1;
while (lower <= upper) {
int mid = lower + (upper - lower) / 2;
if(intToSearch < sortedArray[mid])
upper = mid - 1;
else if (intToSearch > sortedArray[mid])
lower = mid + 1;
else
return mid;
}
return -1; // Returns -1 if no match is found
}
Following is a code sample extracted from here.
public class BinarySearch {
public boolean find(int[] sortedValues, int value) {
return search(sortedValues, value, 0, sortedValues.length - 1);
}
private boolean search(int[] sorted, int value, int leftIndex, int rightIndex) {
// 1. index check
if (leftIndex > rightIndex) {
return false;
}
// 2. middle index
int middle = (rightIndex + leftIndex) / 2;
// 3. recursive invoke
if (sorted[middle] > value) {
return search(sorted, value, leftIndex, middle - 1);
} else if (sorted[middle] < value) {
return search(sorted, value, middle + 1, rightIndex);
} else {
return true;
}
}
}
You can find implementations of the below test cases against the above binary search implementation as well in the reference link.
1. shouldReturnFalseIfArrayIsEmpty()
2. shouldReturnFalseIfNotFoundInSortedOddArray()
3. shouldReturnFalseIfNotFoundInSortedEvenArray()
4. shouldReturnTrueIfFoundAsFirstInSortedArray()
5. shouldReturnTrueIfFoundAtEndInSortedArray()
6. shouldReturnTrueIfFoundInMiddleInSortedArray()
7. shouldReturnTrueIfFoundAnywhereInSortedArray()
8. shouldReturnFalseIfNotFoundInSortedArray()
A possible example is :
// need extra "helper" method, feed in params
public int binarySearch(int[] a, int x) {
return binarySearch(a, x, 0, a.length - 1);
}
// need extra low and high parameters
private int binarySearch(int[ ] a, int x,
int low, int high) {
if (low > high) return -1;
int mid = (low + high)/2;
if (a[mid] == x) return mid;
else if (a[mid] < x)
return binarySearch(a, x, mid+1, high);
else // last possibility: a[mid] > x
return binarySearch(a, x, low, mid-1);
}
Here you can check in C Binary Search, With and Without Recursion
Source : http://www.cs.utsa.edu/~wagner/CS3343/recursion/binsearch.html
Here is a algorithm which should get you going. Let your method signature be:
public boolean binarysearchRecursion(Array, begin_index,end_index, search_element)
Check if your begin_index > end_index if YES then return false.
Calculate mid_element for your input array.
Check if your search_element is equal to this mid_element. if YES return true
If mid_element > search_element Call your method with for range 0 - mid
If mid_element < search_element Call your method with for range mid+1 - Length_of_Array
Also as #DwB said in his comment you are better using loop to get things done. Some problems are recursive in nature(Like binary tree problems). But this one is not one of them.
This is another way of doing recursion:
int[] n = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
#Test
public void testRecursiveSolution() {
Assert.assertEquals(0, recursiveBinarySearch(1,n));
Assert.assertEquals(15, recursiveBinarySearch(16,n));
Assert.assertEquals(14, recursiveBinarySearch(15,n));
Assert.assertEquals(13, recursiveBinarySearch(14,n));
Assert.assertEquals(12, recursiveBinarySearch(13,n));
Assert.assertEquals(11, recursiveBinarySearch(12,n));
Assert.assertEquals(10, recursiveBinarySearch(11,n));
Assert.assertEquals(9, recursiveBinarySearch(10,n));
Assert.assertEquals(-1, recursiveBinarySearch(100,n));
}
private int recursiveBinarySearch(int n, int[] array) {
if(array.length==1) {
if(array[0]==n) {
return 0;
} else {
return -1;
}
} else {
int mid = (array.length-1)/2;
if(array[mid]==n) {
return mid;
} else if(array[mid]>n) {
return recursiveBinarySearch(n, Arrays.copyOfRange(array, 0, mid));
} else {
int returnIndex = recursiveBinarySearch(n, Arrays.copyOfRange(array, mid+1, array.length));
if(returnIndex>=0) {
return returnIndex+mid+1;
} else {
return returnIndex;
}
}
}
}
While it doesn't return the index, this at least returns the idea of 'yes' or 'no' that something is in the collection:
public static boolean recursive(int[] input, int valueToFind) {
if (input.length == 0) {
return false;
}
int mid = input.length / 2;
if (input[mid] == valueToFind) {
return true;
} else if (input[mid] > valueToFind) {
int[] smallerInput = Arrays.copyOfRange(input, 0, mid);
return recursive(smallerInput, valueToFind);
} else if (input[mid] < valueToFind) {
int[] smallerInput = Arrays.copyOfRange(input, mid+1, input.length);
return recursive(smallerInput, valueToFind);
}
return false;
}
A recursion BinarySearch with break conditions in case you can not find the value you are looking for
public interface Searcher{
public int search(int [] data, int target, int low, int high);
}
The Implementation
public class BinarySearch implements Searcher {
public int search(int[] data, int target, int low, int high) {
//The return variable
int retorno = -1;
if(low > high) return retorno;
int middle = (high + low)/2;
if(target == data[middle]){
retorno = data[middle];
}else if(target < data[middle] && (middle - 1 != high)){
//the (middle - 1 != high) avoids beeing locked inside a never ending recursion loop
retorno = search(data, target, low, middle - 1);
}else if(target > data[middle] && (middle - 1 != low)){
//the (middle - 1 != low) avoids beeing locked inside a never ending recursion loop
retorno = search(data, target, middle - 1, high);
}else if(middle - 1 == low || middle - 1 == high){
//Break condition if you can not find the desired balue
retorno = -1;
}
return retorno;
}
}

Array Out Of Bounds

The purpose of this program is to find the kth smallest element in an array without sorting the array using a recursive and nonrecursive decrease and conquer type method.
I was hoping someone could look over my code and try to help me with my array out of bounds error(s).
The method that is throwing these errors is the recursive selection the non recursive selection works fine.
My driver is also attached and everything should compile if you want to test my code.
public class KthSmallest
{
private int counter;
private int term;
private int[] A;
int SelectionNonRecursive(int A[], int kthSmallest, int sizeOfA)
{
this.A = A;
if(kthSmallest == 1 || kthSmallest == sizeOfA)
{
return (LinearSearch(kthSmallest, sizeOfA));
}
else
{
for(int i = 0; i<sizeOfA; i++)
{
counter = 0;
for(int j = 0; j<sizeOfA; j++)
{
if(A[i] < A[j])
{
counter++;
}
}
if((sizeOfA - counter) == kthSmallest)
{
return A[i];
}
}
}
return 0;
}
int SelectionRecursive(int A[], int kthSmallest, int sizeOfA)
{
this.A = A;
return Selection_R(0, sizeOfA - 1, kthSmallest);
}
int Selection_R(int l, int r, int kthSmallest)
{
if(l<r)
{
if(kthSmallest == 1 || kthSmallest == A.length)
{
return (LinearSearch(kthSmallest, A.length));
}
else
{
int s = LomutoPartition(l, r);
if(s == kthSmallest - 1)
{
return A[s];
}
else if(s > (A[0] + kthSmallest - 1))
{
Selection_R(l, s-1, kthSmallest);
}
else
{
Selection_R(s+1, r, kthSmallest);
}
}
}
return 0;
}
int LomutoPartition(int l, int r)
{
int pivot = A[l];
int s = l;
for(int i = l+1; i<r; i++)
{
if(A[i] < pivot)
{
s += 1;
swap(A[s], A[i]);
}
}
swap(A[l], A[s]);
return s;
}
public void swap(int i, int j)
{
int holder = A[i];
A[i] = A[j];
A[j] = holder;
}
int LinearSearch(int kthSmallest, int sizeOfA)
{
term = A[0];
for(int i=1; i<sizeOfA; i++)
{
if(kthSmallest == 1)
{
if(term > A[i])
{
term = A[i];
}
}
else
{
if(term < A[i])
{
term = A[i];
}
}
}
return term;
}
}
public class KthDriver
{
public static void main(String[] args)
{
KthSmallest k1 = new KthSmallest();
int[] array = {7,1,5,9,3};
System.out.print(k1.SelectionRecursive(array, 3, array.length));
}
}
Inside your LomutoPartition method, you are passing the array elements in your swap method: -
swap(A[s], A[i]); // Inside for loop
and
swap(A[l], A[s]); // Outside for loop
And your swap method considers them as indices: -
public void swap(int i, int j) <-- // `i` and `j` are elements A[s] and A[i]
{
int holder = A[i]; <-- // You are accessing them as indices(A[i] -> A[A[s]])
A[i] = A[j];
A[j] = holder;
}
That is why you are getting that exception. Because, if any element in array is greater than size, it will blast out.
You should change your invocation to: -
swap(s, i); // Inside for loop
and
swap(l, s); // Outside for loop
respectively. And leave your method as it is.
Note that, you should pass array indices, and not array elements. If you pass array elements, then the swapping in the method will not be reflected in your array. Because, your method will have its own copy of your elements.

Categories

Resources