Use Depth First Search in a Binary Search Tree preorder & pastorder fails on test

Use Depth First Search in a Binary Search Tree

Hi,
My preorder() and postorder() methods fails on the test…
I used these values to build a Tree: 8, 3, 10, 1, 6, 14. 4, 7, 13
If I understand well

  • the preorder() method have to return an array with the root value first than following the left of values in ascending: [8, 1, 3, 4, 6, 7, 10, 13, 14]
  • the postorder() method have to return an array with values in ascending and at least the root value: [1, 3, 4, 6, 7, 10, 13, 14, 8]

If not, please let me know what is the right sequence…

thanks for help

my code
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
    this.value = value;
    this.left = null;
    this.right = null;
}
function BinarySearchTree() {
    this.root = null;
    this.add = function(value) {
        const newNode = new Node(value);
        
        this.root === null 
            ? this.root = newNode
            : this.insertNode(this.root, newNode)
        ;
        
    };
    
    this.insertNode = function(node, newNode) {
        
        if (newNode.value < node.value) {
            node.left === null
                ? node.left = newNode
                : this.insertNode(node.left, newNode)
            ;
        } else {
            node.right === null 
                ? node.right = newNode
                : this.insertNode(node.right, newNode)
            ;
        }
    };
    
    this.findMin = function() {
        if (!(this.root)) return null;
        let node = this.root;
        while (node.left) {
            node = node.left;
        }
        return node.value;
    };

    this.findMax = function() {
        if (!(this.root)) return null;
        let node = this.root;
        while (node.right) {
            node = node.right;
        }
        return node.value;
    };
    
    this.isPresent = function(value) {
        
        const findValue = function(node, value) {
            if (!(node)) {
                result = false;
                return;
            }
            if (node.value === value) {
                result = true;
                return;
            }
            if (node.value < value) {
                findValue(node.right, value);
            } else {
                findValue(node.left, value);
            }
        };
        
        let result = false;
        if (value === undefined || 
            this.root === null || 
            !(Number.isInteger(value)) ) {
            return false;
        }
        findValue(this.root, value);
        return result;
    };
    
    let distance, minDistance, maxDistance;
    const countStep = node => {
//        console.log(node.value, "on level", distance);
        if (!(node.left) && !(node.right)){
            // this is a leaf node
//            console.log("leaf node", node.value, "on level", distance);
            if (minDistance > distance) minDistance = distance;
            if (maxDistance < distance) maxDistance = distance;
            distance --;
            return;
        }
        
        if (node.left) {
            distance ++
            countStep(node.left);
        }
        if (node.right) {
            distance ++;
            countStep(node.right);
        }
    };
    
    this.findMinHeight = function() {
        if (!(this.root)) return -1;
        distance = 1;
        minDistance = Infinity;
        countStep(this.root.left);
        distance = 1;
        countStep(this.root.right);
        return  minDistance;
    };
    
    this.findMaxHeight = function() {
        if (!(this.root)) return -1;
        distance = 1;
        maxDistance = -Infinity;
        countStep(this.root.left);
        distance = 1;
        countStep(this.root.right);
        return  maxDistance;
    };
    
    this.isBalanced = function() {
        return (this.findMaxHeight() - this.findMinHeight()) < 2;
    };
    
    this.inorder = function(node = this.root, orderedList = []) {
        if (node) {
            this.inorder(node.left, orderedList);
            orderedList.push(node.value);
            this.inorder(node.right, orderedList);
        }
        return orderedList.length 
            ? orderedList 
            : null;
    };
    
    this.preorder = function(node = this.root, orderedList = []) {
        if (node) {
            orderedList.push(node.value);
            this.inorder(node.left, orderedList);
            this.inorder(node.right, orderedList);
        }
        return orderedList.length 
            ? orderedList 
            : null;
    };
    
    this.postorder = function(node = this.root, orderedList = []) {
        if (node) {
            this.inorder(node.left, orderedList);
            this.inorder(node.right, orderedList);
            orderedList.push(node.value);
        }
        return orderedList.length 
            ? orderedList 
            : null;
    };
}

const isBinarySearchTree = tree => {
    if (!(tree.root) || 
        !(tree.root.hasOwnProperty("left")) || 
        !(tree.root.hasOwnProperty("right"))) {
        return false;
    }
    
    let result = true;
    
    const checkValue = (parentNode) => {
        if (parentNode.left) {
            const leftNode = parentNode.left;
            if (leftNode.value >= parentNode.value) {
                result = false;
                return;
            } else {
                checkValue(leftNode);
            }
        }
        
        if (parentNode.right) {
            const rightNode = parentNode.right;
            if (rightNode.value <= parentNode.value) {
                result = false;
                return;
            } else {
                checkValue(rightNode);
            }
        }
    };
    
    checkValue(tree.root);
    return result;
};

const myTree = new BinarySearchTree();
myTree.add(8);
myTree.add(3);
myTree.add(10);
myTree.add(1);
myTree.add(6);
myTree.add(14);
myTree.add(4);
myTree.add(7);
myTree.add(13);
//myTree.add(12);
//console.log(myTree.findMin());
//console.log(myTree.findMax());
//console.log(myTree.isPresent(10));
//console.log(myTree.root);
//console.log(isBinarySearchTree(myTree));
//displayTree(myTree);
//console.log("the minimum level is:", myTree.findMinHeight());
//console.log("the maximum level is:", myTree.findMaxHeight());
//console.log(myTree.isBalanced());
console.log(myTree.inorder());
console.log(myTree.preorder());
console.log(myTree.postorder());

Using your example tree:

The preorder method would return 8, 3, 1, 6, 4, 7, 10, 14, 13

The postorder method would return 1, 4, 7, 6, 3, 13, 14, 10, 8

Thank you
I fixed… another silly mistake…