0

Running into some weird JavaScript issues regarding the scope of my function declaration. In my BST function I'm able to call the function node without issues. But in my createMinimalHeightBST function I get an error when I try to call the BST function.

The error says:

TypeError: BST is not a function.

Please refer to lines commented WORKS and DOESNT WORK.


CODE

var BST = new BST(10);

console.log(BST.root);


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

function BST(root_data) {
    this.root = root_data === undefined ? root_data : new node(root_data); //WORKS
    this.addNode = function(node, root) {
        if (root === undefined) return this.addNode(node, this.root);
        if (!root) {
            root = node;
            return;
        }
        if (root.data > node.data) {
            if (!root.left) {
                root.left = node;
                return;
            }
            return this.addNode(node, root.left);
        }
        if (!root.right) {
            root.right = node;
            return;
        }  
        return this.addNode(node, root.right);
    };
}

var test = [2, 5, 9, 21, 50, 88];

function createMinimalHeightBST(sorted_arr) {
    var mid = Math.floor(sorted_arr.length / 2);
    var minimal_height_tree = new BST(sorted_arr[mid]); // DOESNT WORK

    for (var i=0; i<sorted_arr.length; i++) {
        if (i !== mid) minimal_height_tree.addNode(sorted_arr[i]);
    }

    return minimal_height_tree;
}

console.log(createMinimalHeightBST(test));
AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109

2 Answers2

1

Fixed code: Now no confusion with function vs variable declaration in first line var BST = new BST_Function(10);

    console.log(BST.root);

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

    function BST_Function(root_data) {
        this.root = root_data === undefined ? root_data : new node(root_data); //WORKS
        this.addNode = function (node, root) {
            if (root === undefined) return this.addNode(node, this.root);
            if (!root) {
                root = node;
                return;
            }
            if (root.data > node.data) {
                if (!root.left) {
                    root.left = node;
                    return;
                }
                return this.addNode(node, root.left);
            }
            if (!root.right) {
                root.right = node;
                return;
            }
            return this.addNode(node, root.right);
        };
    }

    var test = [2, 5, 9, 21, 50, 88];

    function createMinimalHeightBST(sorted_arr) {
        var mid = Math.floor(sorted_arr.length / 2);
        var minimal_height_tree = new BST_Function(sorted_arr[mid]); // DOESNT WORK

        for (var i = 0; i < sorted_arr.length; i++) {
            if (i !== mid) minimal_height_tree.addNode(sorted_arr[i]);
        }

        return minimal_height_tree;
    }

    console.log(createMinimalHeightBST(test));
kblau
  • 2,094
  • 1
  • 8
  • 20
0

define var BST = new BST(10); after your constructor and please change the variable name to something different

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24