Skip to content

Commit 2d797fb

Browse files
committed
Corrected code smells with raw type and dangling else block
-Generic types shouldn't be used raw (without type parameters) in variable declarations or return values. Doing so bypasses generic type checking, and defers the catch of unsafe code to runtime. https://rules.sonarsource.com/java/RSPEC-3740 - The dangling else problem appears when nested if/else statements are written without curly braces. In this case, else is associated with the nearest if but that is not always obvious and sometimes the indentation can also be misleading. https://rules.sonarsource.com/java/tag/confusing/RSPEC-5261
1 parent 6aecb37 commit 2d797fb

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/main/java/com/datastructures/BinaryTree.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class BinaryTree<T extends Comparable> {
1010
private final T data;
11-
private BinaryTree right, // the upper binary tree
11+
private BinaryTree<T> right, // the upper binary tree
1212
left; // the lower binary tree
1313

1414
public BinaryTree(T data) {
@@ -21,35 +21,38 @@ public String toString() {
2121
}
2222

2323
/**
24-
* inserts a new value in it's correspondant place
24+
* inserts a new value in it's correspondent place
2525
*
2626
* @param newDataValue value of the new binary tree to add on this tree
2727
*/
2828
public void insert(T newDataValue) {
29-
this.insert(new BinaryTree(newDataValue));
29+
this.insert(new BinaryTree<>(newDataValue));
3030
}
3131

3232
/**
33-
* inserts a new binary tree in it's correspondant place
33+
* inserts a new binary tree in it's correspondent place
3434
*
3535
* @param newData new value to add on this tree
3636
*/
37-
public void insert(BinaryTree newData) {
37+
public void insert(BinaryTree<T> newData) {
3838

3939
int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value
4040

41-
if (cpr < 0)
42-
if (this.left == null)
41+
if (cpr < 0) {
42+
if (this.left == null) {
4343
this.setLeft(newData);
44-
else
44+
} else {
4545
this.left.insert(newData);
46-
else if (cpr > 0)
47-
if (this.right == null)
46+
}
47+
} else if (cpr > 0) {
48+
if (this.right == null) {
4849
this.setRight(newData);
49-
else
50+
} else {
5051
this.right.insert(newData);
51-
else
52+
}
53+
} else {
5254
System.out.println("Redundant value, not added");
55+
}
5356
}
5457

5558
/**
@@ -58,8 +61,8 @@ else if (cpr > 0)
5861
* @param data Searched value
5962
* @return Binary tree which contains the value, null if it doesn't exist
6063
*/
61-
public BinaryTree search(T data) {
62-
int cpr = data.compareTo(this.data); //new value comparission respect to actual value
64+
public BinaryTree<T> search(T data) {
65+
int cpr = data.compareTo(this.data); //new value comparison respect to actual value
6366

6467
if (cpr < 0) {
6568
if (this.left == null)
@@ -113,19 +116,19 @@ public T getData() {
113116
return data;
114117
}
115118

116-
public BinaryTree getRight() {
119+
public BinaryTree<T> getRight() {
117120
return right;
118121
}
119122

120-
public void setRight(BinaryTree right) {
123+
public void setRight(BinaryTree<T> right) {
121124
this.right = right;
122125
}
123126

124-
public BinaryTree getLeft() {
127+
public BinaryTree<T> getLeft() {
125128
return left;
126129
}
127130

128-
public void setLeft(BinaryTree left) {
131+
public void setLeft(BinaryTree<T> left) {
129132
this.left = left;
130133
}
131134
}

0 commit comments

Comments
 (0)