What could cause a return statement not to work in an if code block

Viewed 157

I am trying to compare two binary trees to see if they are equal in structure and value, but at some point in the algorithm, a return statement doesn't work when placed in an if code block for just the values i want to compare. i.e

let a = "myVal"
let b = "myVal"

if(a = b){
    return false
}

The above doesn't work with only the variabes i want to compare but works well with every other variable.

I have checked the type and value of both variables and they are indeed equal.

Also, when i throw an error for within the if code block, it works when th e condition is met but a return statement doesn't just work.

Here's the full code

function compare(a, b){
    if(a === null && b === null){
        return true;
    }
    if(typeof a === 'object' && typeof b === 'object'){
        // compare their structures
        let aRoot = Object.keys(a);
        let bRoot = Object.keys(b);
        if(aRoot.length !== bRoot.length){
            console.log('0')
            return false; //Trees are of different structures
        }

        //Loop through the roots of the tree
        for(let i in aRoot){
            if(aRoot[i] !== bRoot[i]){
                //Make sure the roots are represented with equal names
                console.log('1')
                return false;
            }
            let aValue = a[aRoot[i]];
            let bValue = b[bRoot[i]];
            if(typeof aValue !== typeof bValue){
                console.log('2')
                return false
            }
            if(aValue !== null && bValue !== null){
                //If they are both of the same types compare their values check if they are child nodes or not
                if(typeof aValue !== 'object'){
                    //Here's the main problem
                    if(aValue !== bValue){
                        // console.log("aValue : ", aValue, " bValue : ", bValue)
                        // console.log("aValue type : ", typeof aValue, " bValue type : ", typeof bValue)
                        return false;
                    }
                }
                else{
                    // console.log('a ', aValue)
                    compare(aValue, bValue);
                }
            }
        }
    }
    return true;
}


let aNode = {val: 1, left: null, right: null, d: {val: 1, left: null, right: null, f: {val: 2, left: null, right: null}}};

let bNode = {val: 1, left: null, right: null, d: {val: 3/* This differs from aNode*/, left: null, right: null, f: {val: 2, left: null, right: null}}};

console.log(compare(aNode, bNode))

It works with trees with no root nodes, but doesn't with trees with root nodes.

The problem arises from the if(aValue !== bValue) part of the code. That code block doesn't just return false but when an error is thrown, it works.

2 Answers

Your recursive call... compare(aValue, bValue); should be return compare(aValue, bValue); otherwise you lose the returned values from the calls

function compare(a, b){
    if(a === null && b === null){
        return true;
    }
    if(typeof a === 'object' && typeof b === 'object'){
        // compare their structures
        let aRoot = Object.keys(a);
        let bRoot = Object.keys(b);
        if(aRoot.length !== bRoot.length){
            console.log('0')
            return false; //Trees are of different structures
        }

        //Loop through the roots of the tree
        for(let i in aRoot){
            if(aRoot[i] !== bRoot[i]){
                //Make sure the roots are represented with equal names
                console.log('1')
                return false;
            }
            let aValue = a[aRoot[i]];
            let bValue = b[bRoot[i]];
            if(typeof aValue !== typeof bValue){
                console.log('2')
                return false
            }
            if(aValue !== null && bValue !== null){
                //If they are both of the same types compare their values check if they are child nodes or not
                if(typeof aValue !== 'object'){
                    //Here's the main problem
                    if(aValue !== bValue){
                        // console.log("aValue : ", aValue, " bValue : ", bValue)
                        // console.log("aValue type : ", typeof aValue, " bValue type : ", typeof bValue)
                        return false;
                    }
                }
                else{
                    // console.log('a ', aValue)
                    return compare(aValue, bValue);
// Check this ------^----^
                }
            }
        }
    }
    return true;
}


let aNode = {val: 1, left: null, right: null, d: {val: 1, left: null, right: null, f: {val: 2, left: null, right: null}}};

let bNode = {val: 1, left: null, right: null, d: {val: 3/* This differs from aNode*/, left: null, right: null, f: {val: 2, left: null, right: null}}};

console.log(compare(aNode, bNode))

Can you please try this code, I think it may solve your issue.

/* Given two trees, return true if they are structurally identical */
int identicalTrees(node* a, node* b)  
{  
    /*1. both empty */
    if (a == NULL && b == NULL)  
        return 1;
    /* 2. both non-empty -> compare them */
    if (a != NULL && b != NULL)  
    {  
        return
        (  
            a->data == b->data &&  
            identicalTrees(a->left, b->left) &&  
            identicalTrees(a->right, b->right)  
        );  
    } 
    /* 3. one empty, one not -> false */
    return 0;  
}

and then use it as:

if(identicalTrees(a, b))  
        cout << "Both tree are identical.";  
    else
        cout << "Trees are not identical.";  
Related