2

I am working on a Binary search tree and have a problem while adding the values to the tree. When I add values(numbers) in order (descending or ascending) they are added in the right positions, but if I add value that is supposed to go somewhere in between values that are already in the tree, they don't get sorted. you can see that in the picture, the number [ 3 ] is added after 1, but it should be between [ 4 ] and [ 1 ] So the question is, what exactly am I doing wrong and how to fix it

I am adding my code for the add function below the picture

enter image description here

The Node object

function Node(data){
this.data=data;
this.left=null;
this.right=null;}

function BinarySearchTree(){
this.root = null;
var current=null;
var newNode=null;

this.add = function(data){
    var root = this.root;
        if(!root){
            this.root= new Node(data);
            return;
        }else{
            if(this.ifExists(data)){
            }else{
                current=root;
                newNode= new Node(data);

                while(current){
                    if(data<current.data){
                        if(!current.left){
                                current.left=newNode;
                            break;
                        }else{
                                current= current.left;
                             }
                    }else{
                        if(!current.right){
                                current.right=newNode;
                            break;
                        }else{
                                current=current.right;
                            }
                        }
                    }
                }
            }
        }
}
this.ifExists=function(data){
         var current = this.root;
//       console.log(current);
         while(current){
             if(data==current.data){
                 return true;
             }
             current = data < current.value ? current.left : current.right;
         }
         return false;
     }
}

How I call the add function

var bst= new BinarySearchTree();
bst.add(7);
bst.add(8);
bst.add(4);
bst.add(1);
bst.add(15);
bst.add(67);
bst.add(9);
bst.add(3);
console.log(bst);

the output of console.log(bst);

enter image description here

14
  • 1
    you have some else else parts, which have no if before. Commented Sep 22, 2018 at 16:44
  • @NinaScholz mistakenly copied the code into stackoverflow, just fixed the code. But this was not the problem Commented Sep 22, 2018 at 16:51
  • I don't understand the problem. You marked the 3 as in the wrong place, but the 1 should be a left child, as it is smaller than 4. The 3 is a right child as it should be. Commented Sep 22, 2018 at 17:01
  • 1
    You should add a mvce so it's clear where the problem is. If I add the missing code to get this running, it works fine. I get a valid bst. Commented Sep 22, 2018 at 17:18
  • please add missing parts, like ifExists as well. Commented Sep 22, 2018 at 17:18

1 Answer 1

2

The 3 should be on right side, because it is bigger than 1. In your output this is true. In your picture you made an error there.

Other than that your tree is fine. The fact that 1 is smaller than 3 is correct, but a BST does only guaranty that an in-order tree walk yields an sorted list of the trees data and not that every smaller value is lower in the tree than a bigger value. If you perform an in order tree walk your BST would give you 1, 3, 4, 7, 8, 15

If you do not know how to do a in order tree walk look at this: BST In-Order-Walk from Wikipedia

Just note every data, when the point on the bottom of the node ist touched.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.