2

EDIT: I do not use any keys here because they would not work with my code - so my code is different from the one suggested.

I started working with binary trees last week at university, and we mostly use queues as a help when it comes to inserting anything to the tree. But i was wondering if it would be possible to do it without the help of the queue. I made this code so far:

public void withoutQueue(Node newNode) {

    if (root == null) {
        root = newNode;
        current = root;

    } else if (root != null) {

        if (current.left == null) {
            current.left = newNode;
            newNode.parent = current;
            counterleft++;

        } else if (current.left != null && current.right == null) {
            current.right = newNode;
            newNode.parent = current;
            counterright++;

        } else if (current.left != null && current.right != null && current.left.left == null && counterleft <= counterright) {
            Node helper = current;
            current = current.left;
            current.parent = helper;
            counterleft++;
            breadthFirstAppend(newNode);

        } else if (current.left != null && current.right != null && counterright <= counterleft) {
            Node helper = current;
            current = current.parent.right;
            current.parent = helper;
            counterright++;
            breadthFirstAppend(newNode);

        }

    }
}

But this far it does not really work that good. It inserts the first three correctly (root, root.left, root.right) - but then jumps to the 6th inserted node... i simply dont know what to do at this point. I added the counterleft/right integers so that i could maybe control it via that, but it didnt help very much.

My core problem is that it either jumps down only on the left (1,2,4,..) or only on the right (1,3,5,...) - but it should jump (1,2,3,4,5,...).

Any tips for how i could improve this code? I'll update it when I find something out myself.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Calimera
  • 145
  • 2
  • 13
  • so what you're asking is how to make a recursive insert method? because currently, you're hardcoding all the possible scenarios (and obviously it's not possible)? – Ousmane D. May 10 '17 at 13:20
  • Yeah, basically. Because I know that hardcoding isn't really a good solution here because it simply wont work after a lot of values... and it simply is not really that overseeable + managable as a recursive call.. – Calimera May 10 '17 at 13:21
  • if that's the case then there are a lot of answers on this site for you to look at. [1st example](http://stackoverflow.com/questions/14825492/recursive-binary-search-tree-insert), [2nd example](http://stackoverflow.com/questions/26270200/recursive-insert-for-binary-tree) and I could go on and on and on..... – Ousmane D. May 10 '17 at 13:22
  • believe me - i did. Do you really think i would ask without searching before? But they simply did not work out for me. Mostly compare methods / iterator keys were used - but my code does not work with that unfortunately, because when i want to use iterators, it always asks to add a queue. And when i compare it, the order is no longer right anymore. – Calimera May 10 '17 at 13:24
  • Why is this tagged with recursion ? – OneCricketeer May 10 '17 at 13:29
  • Because here: 'breadthFirstAppend(newNode); ' i am calling it recuresively and i want to solve it in a recuresive way. – Calimera May 10 '17 at 13:31
  • You might want to look here for ideas (the put() function is what you are looking for... just adapt it to your logic): http://algs4.cs.princeton.edu/32bst/BST.java.html – Faheem May 10 '17 at 13:34
  • This code doesn't make any sense because it doesn't make any attempt to keep the values in order. It doesn't look at the value being inserted, so it can't possibly do so! I think you're making a mistake ignoring the other implementations that have a separate key and value. If you don't have a separate key, just use the value anywhere the other codes used the key (i.e. the same value is both key and value at the same time). – Blckknght Sep 30 '17 at 04:29

0 Answers0