From 2d797fb976fb0a69f914b3ca3da0048a34a04d07 Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Fri, 21 Aug 2020 15:59:02 +0530 Subject: [PATCH] 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 --- .../java/com/datastructures/BinaryTree.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/datastructures/BinaryTree.java b/src/main/java/com/datastructures/BinaryTree.java index 4270b880..5bf1702e 100644 --- a/src/main/java/com/datastructures/BinaryTree.java +++ b/src/main/java/com/datastructures/BinaryTree.java @@ -8,7 +8,7 @@ package com.datastructures; public class BinaryTree { private final T data; - private BinaryTree right, // the upper binary tree + private BinaryTree right, // the upper binary tree left; // the lower binary tree public BinaryTree(T data) { @@ -21,35 +21,38 @@ public class BinaryTree { } /** - * 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 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"); + } } /** @@ -58,8 +61,8 @@ public class BinaryTree { * @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 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 { return data; } - public BinaryTree getRight() { + public BinaryTree getRight() { return right; } - public void setRight(BinaryTree right) { + public void setRight(BinaryTree right) { this.right = right; } - public BinaryTree getLeft() { + public BinaryTree getLeft() { return left; } - public void setLeft(BinaryTree left) { + public void setLeft(BinaryTree left) { this.left = left; } }