Chase
Chase

Reputation: 11

inOrder Tree Traveral algorithm not working

    class Node {
    constructor(data, left = null, right = null) {
        this.data = data;
        this.left = left;
        this.right = right;
    };
};

class BST {
    constructor() {
        this.root = null
    };
    add(data) {
        const node = this.root;
        if (node === null) {
            this.root = new Node(data);
            return;
        } else {
            const searchTree = function(node) {
                if (data < node.data) {
                    if (node.left === null) {
                        node.left = new Node (data);
                        return;
                    } else if (node.left !== null) {
                        return searchTree(node.left);
                    }
                } else if (data > node.data) {
                    if (node.right === null) {
                        node.right = new Node(data);
                        return;
                    } else if (node.right !== null) {
                        return searchTree(node.right);
                    }
                } else {
                    return null;
                }
            };
            return searchTree(node);
        };
    };

    findMin() {
        let current = this.root;
        while (current.left !== null) {
            current = current.left;
        }
        return current.data;
    }

    findMax() {
        let current = this.root;
        while (current.right !== null) {
            current = current.right;
        }
        return current.data;
    }
    find(data) {
        let current = this.root;
        while (current.data !== data) {
            if (data < current.data) {
                current = current.left; 
            } else {
                current = current.right;
            };
            if (current === null) {
                return null;
            };
        };
        return current;
    };

    isPresent(data) {
        let current = this.root;
        while (current.data !== data) {
            if (data < current.data) {
                current = current.left;
            } else {
                current = current.right;
            };
            if (current === null) {
                return false;
            };
        };
        return true;
    };

    remove(data) {
        const removeNode = function (node, data) {
            if (node == null) {
                return null;
            }
            if (data == node.data) {
                // node has no children
                if (node.left == null && node.right == null) {
                    return null;
                }
                // node has no left child
                if (node.left == null) {
                    return node.right;
                }
                // node has no right child
                if (node.right == null) {
                    return node.left;
                }
                // node has two children
                var tempNode = node.right;
                while (tempNode.left !== null) {
                    tempNode = tempNode.left
                }
                node.data = tempNode.data
                node.right = removeNode(node.right, tempNode.data)
                return node;
            } else if (data < node.data) {
                node.left = removeNode(node.left, data);
                return node;
            } else {
                node.right = removeNode(node.right, data);
                return node;
            };
        };
        this.root = removeNode(this.root, data);
    };

    findMinHeight(node = this.root) {
        if (node == null) {
            return -1;
        };
        let left = this.findMinHeight(node.left);
        let right = this.findMinHeight(node.right);
        if (left < right) {
            return left + 1;
        } else {
            return right + 1;
        };
    };

    findMaxHeight(node = this.root) {
        if (node == null) {
            return -1;
        };
        let left = this.findMinHeight(node.left);
        let right = this.findMinHeight(node.right);
        if (left > right) {
            return left + 1;
        } else {
            return right + 1;
        };
    };

    isBalanced() {
        return (this.findMaxHeight() >= this.findMaxHeight() - 1);
    }

    inOrder() {
        if (this.root == null){
            return null;
        } else {
            var result = new Array();
            function traverseInOrder(node) {
                node.left && traverseInOrder(node.left);
                result.push(node.data);
                node.right && traverseInOrder(node.right);
            }
            traverseInOrder(this.root);
            return result;
        }
    }

this is the output and the tree

var tree = new BST();
tree.add("10");
tree.add("19");
tree.add("17");
tree.add("21");
tree.add("5");
tree.add("1");
tree.add("6");
console.log(tree.inOrder())


 [
  '1',  '10', '17',
  '19', '21', '5',
  '6'
]

As you can see I've been digging and this is what everyone says to use but it just wont work, it goes down then over to the right it doesn't make any sense, here is a visualization of the tree. I have all the code here for testing if anyone wants to test it out it would be appreciated.

                             10
                           /    \
                         5       19
                       /  \     /  \
                      1    6   17   21

Upvotes: 1

Views: 25

Answers (1)

trincot
trincot

Reputation: 350841

Your BST add and inOrder methods are correct, and the output is as should be expected, but you seem to expect a numerical order in your output. But since you have provided string values to your tree, the comparison is not numerical, but alphabetical. In that order, the output is correct. If you wanted numerical order, then pass numbers (without quotes):

var tree = new BST();
tree.add(10);
tree.add(19);
tree.add(17);
tree.add(21);
tree.add(5);
tree.add(1);
tree.add(6);
console.log(tree.inOrder())

If you run this, the output will be:

[ 1, 5, 6, 10, 17, 19, 21 ]

Upvotes: 3

Related Questions