0

I have the following code. But it is expected to be without parameters for example sum() and i am not quite sure on how to fix that so that the code would still work. Could someone help me with that? I would like change the code as little as possible. is there a way to add it to the methods and the recall the recusive methods from that

import java.util.*;
public class BinarySearchTree {

        private class BinaryNode {
                private int element;
                private BinaryNode left;
                private BinaryNode right;

                private BinaryNode(int element) {
                        this.element = element;
                }
        }

        private BinaryNode root;

        public void insert(int newNumber) {
                // special case: empty tree
                if (root == null) {
                        root = new BinaryNode(newNumber);
                        return;
                }

                BinaryNode parent = null;
                BinaryNode child = root;
                while (child != null) {
                        parent = child;
                        if (newNumber == child.element) {
                                //number already in tree
                                return;
                        } else if (newNumber < child.element) {
                                child = child.left;
                        } else {
                                child = child.right;
                        }
                }

                if (newNumber < parent.element) {
                        parent.left = new BinaryNode(newNumber);
                } else {
                        parent.right = new BinaryNode(newNumber);
                }
        }

        public int maximumRecursive(BinaryNode root) {
                if (root.right == null)
                        return root.element;
                return maximumRecursive(root.right);
        }

        public int maximumIterative() {

                if (root == null) {
                        throw new NoSuchElementException();
                }

                BinaryNode current = root;
                while (current.right != null)
                        current = current.right;

                return (current.element);
        }

        public int height(BinaryNode root) {
                if (root == null)
                        return 0;

                return 1 + Math.max(height(root.left), height(root.right));
        }

        public int sum(BinaryNode root) {
                if (root == null)
                        return 0;

                return root.element + sum(root.left) + sum(root.right);
        }

        public String reverseOrder(BinaryNode root) {
                if (root == null) {
                        return "";
                }
                return reverseOrder(root.right) + " " + ((Integer) root.element).toString() + " " + reverseOrder(root.left);
        }
markspace
  • 10,621
  • 3
  • 25
  • 39
issac
  • 1
  • 1
  • Two questions: 1. If the requirement is that sum have no parameters, does that mean it should calculate the sum of all nodes in the tree? 2. Can you confirm that this is a school assignment. If not, why not use TreeMap? – ControlAltDel Jan 23 '22 at 20:16
  • it should calculate all numbers in the binary search tree, not only sum shouldnt have no parameters but all of the methods. It is an assignment for university, but my code wont work for the site i have to turn it in with the parameters. – issac Jan 23 '22 at 20:30
  • You could convert your recursive functions to iterative ones. To re-use your current logic, you could make those methods private. Then you can add the corresponding public method that calls the private one passing the root. For example: public int sum() { return sum(root); } private int sum(BinaryNode root) { ... } – Mirko Alicastro Jan 23 '22 at 21:57
  • @issac, any feedback on the answer below? – trincot Jan 25 '22 at 18:07

1 Answers1

1

You can just overload your sum method, so you have the version with parameter, and without parameter. Make the one with parameter private, as that version should only be used during the recursive deepening (inside the class):

       private int sum(BinaryNode root) {
                if (root == null)
                        return 0;

                return root.element + sum(root.left) + sum(root.right);
        }
        public int sum() {
                return sum(root);
        }

You can do a similar thing for the other methods that are recursive, and for that reason have a parameter. For instance, for height:

        private int height(BinaryNode root) {
                if (root == null)
                        return 0;

                return 1 + Math.max(height(root.left), height(root.right));
        }
        public int height() {
                return height(root);
        }
trincot
  • 317,000
  • 35
  • 244
  • 286
  • And probably the methods that accept a BynaryNode shouldn't be public but just used internally. It doesn't make sense to me that a BinarySearchTree exposes a method `sum(BinaryNode root)` to compute the sum starting from a generic BinaryNode. Moreover, the BinaryNode class is private, so those methods shouldn't be exposed, otherwise, they are useless. – Mirko Alicastro Jan 23 '22 at 22:24
  • 1
    Good remark, @Mirko. Updated. – trincot Jan 23 '22 at 22:31