Quick actions

cmd+k|ctrl+k

Navigation

Languages

BST

Snippet info

Language

JavaScript

Visibility

public

Author

jameeulla3

Created

2024-08-21T22:07:08.866622Z

Updated

2024-08-22T00:26:20.458657Z

class Vertex {
    constructor(value)
    {
        this.data = value; 
        this.left = null; 
        this.right = null; 
    }
}

class BST {
    constructor()
    {
        this.root = null;
    }
    insert(value)
    {
        let ver = new Vertex(value);
        if(this.root == null) this.root = ver; 
        else 
        {
            let currNode = this.root; 
            while(1)
            {
                if(value < currNode.data)
                {
                    if(!currNode.left){
                    currNode.left = ver;
                    return; 
                    }
                    else 
                    currNode = currNode.left; 
                }
                else 
                {
                    if(!currNode.right)
                    {
                    currNode.right = ver; 
                    break; 
                        
                    }
                    else 
                    currNode = currNode.right;
                }
            }
        }
    }
    lookup(value)
    {
        let currNode = this.root; 
        while(currNode)
        {
            if(currNode.data == value)
            return currNode; 
            else 
            {
                if(value<currNode.data)
                {
                    currNode = currNode.left; 
                }
                else 
                {
                    currNode = currNode.right;
                }
            }
        }
        return -1;
    }
    remove(value)
    {
        let targetNode = this.lookup(value);
        if(targetNode!==-1)
        {
            if(targetNode.left == null && targetNode.right == null)
            targetNode = null; 
         else{
              
             
         }
        }
    }
}
let obj = new BST(); 
obj.insert(5)
obj.insert(4);
obj.insert(2);
obj.insert(6);
obj.insert(8);
console.log(obj.lookup(9))
console.log(obj);
INFO