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
This commit is contained in:
parent
6aecb37b49
commit
2d797fb976
@ -8,7 +8,7 @@ package com.datastructures;
|
||||
|
||||
public class BinaryTree<T extends Comparable> {
|
||||
private final T data;
|
||||
private BinaryTree right, // the upper binary tree
|
||||
private BinaryTree<T> right, // the upper binary tree
|
||||
left; // the lower binary tree
|
||||
|
||||
public BinaryTree(T data) {
|
||||
@ -21,36 +21,39 @@ public class BinaryTree<T extends Comparable> {
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts a new value in it's correspondant place
|
||||
* inserts a new value in it's correspondent place
|
||||
*
|
||||
* @param newDataValue value of the new binary tree to add on this tree
|
||||
*/
|
||||
public void insert(T newDataValue) {
|
||||
this.insert(new BinaryTree(newDataValue));
|
||||
this.insert(new BinaryTree<>(newDataValue));
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts a new binary tree in it's correspondant place
|
||||
* inserts a new binary tree in it's correspondent place
|
||||
*
|
||||
* @param newData new value to add on this tree
|
||||
*/
|
||||
public void insert(BinaryTree newData) {
|
||||
public void insert(BinaryTree<T> newData) {
|
||||
|
||||
int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value
|
||||
|
||||
if (cpr < 0)
|
||||
if (this.left == null)
|
||||
if (cpr < 0) {
|
||||
if (this.left == null) {
|
||||
this.setLeft(newData);
|
||||
else
|
||||
} else {
|
||||
this.left.insert(newData);
|
||||
else if (cpr > 0)
|
||||
if (this.right == null)
|
||||
}
|
||||
} else if (cpr > 0) {
|
||||
if (this.right == null) {
|
||||
this.setRight(newData);
|
||||
else
|
||||
} else {
|
||||
this.right.insert(newData);
|
||||
else
|
||||
}
|
||||
} else {
|
||||
System.out.println("Redundant value, not added");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* search and specific value on the tree
|
||||
@ -58,8 +61,8 @@ public class BinaryTree<T extends Comparable> {
|
||||
* @param data Searched value
|
||||
* @return Binary tree which contains the value, null if it doesn't exist
|
||||
*/
|
||||
public BinaryTree search(T data) {
|
||||
int cpr = data.compareTo(this.data); //new value comparission respect to actual value
|
||||
public BinaryTree<T> search(T data) {
|
||||
int cpr = data.compareTo(this.data); //new value comparison respect to actual value
|
||||
|
||||
if (cpr < 0) {
|
||||
if (this.left == null)
|
||||
@ -113,19 +116,19 @@ public class BinaryTree<T extends Comparable> {
|
||||
return data;
|
||||
}
|
||||
|
||||
public BinaryTree getRight() {
|
||||
public BinaryTree<T> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(BinaryTree right) {
|
||||
public void setRight(BinaryTree<T> right) {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public BinaryTree getLeft() {
|
||||
public BinaryTree<T> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(BinaryTree left) {
|
||||
public void setLeft(BinaryTree<T> left) {
|
||||
this.left = left;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user