Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ae0b6df

Browse files
Fixes of binary seaching NullPointerExecptions-Recursive+ binary trees initial implementation.
1 parent 6909232 commit ae0b6df

File tree

2 files changed

+47
-36
lines changed

2 files changed

+47
-36
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.udemy.dsapart1.binarytrees;
2+
3+
public class BinaryTreeNodeEntity {
4+
5+
private double dataValue;
6+
private BinaryTreeNodeEntity leftNodeRef;
7+
private BinaryTreeNodeEntity rightNodeRef;
8+
private BinaryTreeNodeEntity currentNodeRef;
9+
10+
public BinaryTreeNodeEntity(double dataValue) {
11+
this.dataValue = dataValue;
12+
}
13+
14+
15+
}

‎src/main/java/com/udemy/dsapart1/searching/BinarySearching.java‎

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void main(String[] args) {
2626
System.out.println("\n**************** Performing Recursive Binary seaching ********************");
2727
System.out.print("\nEnter the element to be searched : ");
2828
double target2Item = scInp.nextDouble();
29-
int result2Index=recursiveBinarySearching(inputArr,targetItem,0,inputArr.length-1);
29+
int result2Index=recursiveBinarySearching(inputArr,target2Item,0,inputArr.length-1);
3030
System.out.println("\nTarget element found at index : "+result2Index);
3131
}
3232

@@ -38,23 +38,22 @@ public static void main(String[] args) {
3838
* @return resultIndex - int
3939
*
4040
*/
41-
private static int performBinarySearchingOnSortedArray(double[] inputArr,double targetItem) {
42-
int resultIndex=-1;
43-
int startIndex=0;
44-
int endIndex=inputArr.length-1;
45-
int midIndex;
46-
while (startIndex<=endIndex) {
47-
midIndex=(startIndex+endIndex/2);
48-
if(inputArr[midIndex]==targetItem) {
49-
return midIndex;
50-
}
51-
if (inputArr[midIndex]<targetItem) {
52-
returnstartIndex=midIndex+1;
53-
}
54-
else {
55-
returnendIndex=midIndex-1;
41+
private static int performBinarySearchingOnSortedArray(double[] inputArr,double targetItem) {
42+
int resultIndex = -1;
43+
int startIndex = 0;
44+
int endIndex = inputArr.length - 1;
45+
int midIndex;
46+
while (startIndex <= endIndex) {
47+
midIndex = ((startIndex + endIndex) / 2);
48+
if(inputArr[midIndex] == targetItem) {
49+
return midIndex;
50+
}
51+
if (inputArr[midIndex] < targetItem) {
52+
startIndex = midIndex + 1;
53+
} else {
54+
endIndex = midIndex - 1;
55+
}
5656
}
57-
}
5857
return resultIndex;
5958
}
6059

@@ -66,25 +65,22 @@ private static int performBinarySearchingOnSortedArray(double[] inputArr, double
6665
* @return resultIndex - int
6766
*
6867
*/
69-
private static int recursiveBinarySearching(double[] inputArr, double targetItem,int startIndex,int endIndex) {
70-
int resultIndex=-1;
71-
int midIndex;
72-
if(endIndex<startIndex)
73-
{
74-
return resultIndex;
75-
}
76-
midIndex=(startIndex+endIndex/2);
77-
if(inputArr[midIndex]==targetItem) {
78-
return midIndex;
79-
}
80-
if (inputArr[midIndex]<targetItem) {
81-
startIndex=midIndex+1;
82-
return recursiveBinarySearching(inputArr,targetItem,startIndex,endIndex);
83-
}
84-
else {
85-
endIndex=midIndex-1;
86-
return recursiveBinarySearching(inputArr,targetItem,startIndex,endIndex);
87-
}
68+
private static int recursiveBinarySearching(double[] inputArr, double targetItem, int startIndex, int endIndex) {
69+
int resultIndex = -1;
70+
if (endIndex < startIndex) {
71+
return resultIndex;
72+
}
73+
int midIndex = ((startIndex + endIndex) / 2);
74+
if (inputArr[midIndex] == targetItem) {
75+
return midIndex;
76+
}
77+
if (inputArr[midIndex] < targetItem) {
78+
startIndex = midIndex + 1;
79+
return recursiveBinarySearching(inputArr, targetItem, startIndex, endIndex);
80+
} else {
81+
endIndex = midIndex - 1;
82+
return recursiveBinarySearching(inputArr, targetItem, startIndex, endIndex);
83+
}
8884
}
8985

9086

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /