3
\$\begingroup\$

I implement BinaryTree (K extends Comparable, V) concept to binarySearchTree and I have written get and put method. As far as I check, there is no problem in my code. So now I wonder that are there any problem or weak point in my code? and m,

   public void put(K key, V value) {
        Node newNode = new Node(key, value, null, null);
        Node current = root;
        if (root == null) {
            root = newNode;
            return;
        }
        while (true) {
            int cmd = key.compareTo(current.key);
            if (cmd > 0) {
                if (current.right == null) {
                    current.right = newNode;
                    break;
                } else {
                    current = current.right;
                }
            } else if (cmd < 0) {
                if (current.left == null) {
                    current.left = newNode;
                    break;
                } else {
                    current = current.left;
                }

            } else  {
                current.value = newNode.value;
                break;
            }
        }
  public V get(K key)  {
        Node curr = root;
        if (curr == null) {
            throw new NoSuchElementException("There is no such a element");
        }
        while (true) {
            int cmd = key.compareTo(curr.key);
            if (cmd > 0) {
                curr = curr.right;
            } else if (cmd < 0) {
                curr = curr.left;
            } else {
                return curr.value;
            }
        }
    }

And i check these and it worked

   BinaryTree<Integer,String> binaryTree = new BinaryTree<Integer, String>();

        binaryTree.put(8,"8");
        binaryTree.put(4,"4");
        binaryTree.put(5,"5");
        binaryTree.put(1,"1");
        binaryTree.put(2,"2");
        binaryTree.put(3,"3");
        binaryTree.put(65,"65");
        binaryTree.put(4,"4 (2)");


        System.out.println(binaryTree.get(8));
        System.out.println(binaryTree.get(4));
        System.out.println(binaryTree.get(65));
        System.out.println(binaryTree.get(5));
\$\endgroup\$
1
\$\begingroup\$

Since the code is not compiling, it will be hard to do a proper code review.

In my opinion, you should rename the variable cmd in both of the methods, since it can be confusing.

BinaryTree#put method

  1. In my opinion, it's a bad choice to use the range operators (<, >, <=, >=) with the compareTo method; since you always get one of those values (-1, 0 & 1).

  2. I suggest that you check if the values are equals before checking if they are inferior / superior; this will alow you to merge some of the logic of the inferior / superior since they are pretty similar.


if (cmd == 0) {
   current.value = newNode.value;
   break;
}

if (cmd == 1) {
   //[...]
}
```
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks for your opinion @Doi9t) Besides cmd,everything seems, okay? \$\endgroup\$ – Samir Allahverdi Apr 5 at 17:56
  • 1
    \$\begingroup\$ @samir-allahverdi yes, everything seems OK \$\endgroup\$ – Doi9t Apr 5 at 19:48
  • \$\begingroup\$ Thanks a lot) Thanks for your time ) @Doi9t \$\endgroup\$ – Samir Allahverdi Apr 6 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.