0

Im trying to develop Bynary Tree on Java. I have 2 classes BynaryTreeMap and his inner class Elem.

public class Main {

    public static void main(String[] args) {
        BinaryTreeMap tree = new BinaryTreeMap(22);

        System.out.println(tree.MapSearch(22));
        tree.insert(10);
        System.out.println(tree);
    }


}

class BinaryTreeMap {
    Elem root;
    private static int height;

    BinaryTreeMap(int key){
        root = new Elem(key);
        height = 1;
    }

    public Elem next(Elem elem){
        Elem next;
        if (elem.right != null) {
            next = this.Minimum(elem.right);
        }
        else{
            next = elem.parent;
            while (next != null && elem == next.right){
                elem = next;
                next = next.parent;
            }
        }
        return next;
    }

    public void insert(int key){
        Elem elem = new Elem(key);
        Elem x;
        if (root == null)
            root = elem;
        else {
            x = root;
            while(true){
                if (x.key == key)
                    throw new RuntimeException("elem is already in tree");
                if (key < x.key){
                    if (x.left == null){
                        x.setLeft(elem);
                        break;
                    }
                    x = x.left;
                }
                else
                if (x.right == null){
                    x.setRight(elem);
                    break;
                }
                x = x.right;
            }
        }

    }

    public Elem descend(int key){
        Elem elem = root;
        while (elem != null && elem.key != key){
            if (key <= elem.key)
                elem = elem.left;
            else elem = elem.right;
        }
        return elem;
    }

    public boolean MapSearch(int key){
        return descend(key) != null;
    }

    public Elem lookUp(int key){
        Elem elem = descend(key);
        if (elem == null)
            throw new RuntimeException("No such elemet");
        return elem;
    }

    public boolean isEmpty(){
        return root == null;
    }

    public Elem Minimum(Elem elem){
        Elem min = elem;
        while (min.left != null)
            min = min.left;
        return min;
    }

    public Elem Maximum(Elem elem){
        Elem max = elem;
        while (max.right != null)
            max = max.right;
        return max;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder("BinaryTreeMap{");
        while (root != null){
            stringBuilder.append(root.key);
            root = this.next(root);
        }
        stringBuilder.append("}");
        return stringBuilder.toString();
    }

    public class Elem{
        private Elem parent = null;
        private Elem left = null;
        private Elem right = null;
        private final int key;
        Elem(int key){
            this.key = key;
        }

        private void setLeft(Elem leftAncestor){
            this.left = leftAncestor;
            leftAncestor.parent = this;
        }

        private void setRight(Elem rightAncestor){
            this.right = rightAncestor;
            rightAncestor.parent = this;
        }
    }
}

Output

true
BinaryTreeMap{22}

If i run the program in debug mode, my output is worse.

false
BinaryTreeMap{}

Correct output

true
BinaryTreeMap{22 10}

As i can see my root variable became null, but i cant understand why. Tried to google it, bud didnt succeed. Maybe i didnt uderstand how inner classes work? Thank you for you help.

Lanak
  • 23
  • 5
  • 1
    That is not the issue, your issue is that you're not printing left children, only right children. When I tested your output with `insert(30)` and `insert(40)` the output came up as `BinaryTreeMap{223040}`(You also need to fix your stringbuilder). The issue lies in your `next` and `toString()` methods – Alias Cartellano Nov 17 '21 at 21:11
  • thank you, i didnt notice it. – Lanak Nov 18 '21 at 10:30

0 Answers0