1

I'm trying to write a JavaScript function that compares two binary trees defined by TreeNodes a and b and returns true if they are equal in structure and in value and false otherwise.

for example example of comparing both values and structure of two binary trees

Given the following class:

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

Here is the code i tried writing so far comapring TreeNode a and b.

const binaryTreeCompare = (a, b) => {
  if(a==null && b==null){
    return true;
  }else if(a!=null && b!=null){
    return(
      a.data == b.data && binaryTreeCompare(a.left, b.left) && binaryTreeCompare(a.right, b.right)
    );
  }
    else return false;
}

I expected an ouput of either true or false but this is what i get:

ReferenceError: compare is not defined
    at Context.it (test.js:116:16)
lasabahebwa
  • 281
  • 2
  • 14
  • 1
    where is `compare` from? – Nina Scholz May 19 '19 at 08:14
  • `compare` is not anywhere in my code, I'm running my code against some tests remotely that's why I'm getting a ReferenceError with `compare` – lasabahebwa May 19 '19 at 08:49
  • So those remote tests expect a `compare` function to be defined? If you don't use `compare` in your code, the error is not your fault. (Or you were expected to define `compare`, but did define it with the name `binaryTreeCompare` instead). – Bergi May 19 '19 at 14:52
  • that's right, i was expected to define compare though it wasn't indicated anywhere in the instructions. When i defined compare, all the tests passed. – lasabahebwa May 19 '19 at 14:56

2 Answers2

8

Solution to my own question after serious research is shown in the snippet below.

function compare(a, b){
  if (!a && !b) {
      return true;
   } else if (!a || !b) {
      return false;
   } else {
      return a.val === b.val && compare(a.left, b.left) && compare(a.right, b.right);
   }
}
lasabahebwa
  • 281
  • 2
  • 14
0

One quick-and-dirty approach could be to define a canonical serialization for trees, and then compare them.

The simplest approach would be to JSON.stringify each tree. You'd need to implement a custom toJSON method for TreeNode.

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

  toJSON() {
    return JSON.stringify({ data: this.data, left: this.left, right: this.right });
  }
}

Then, binaryTreeCompare becomes trivial.

EDIT: once you've defined a custom toJSON method on TreeNode, then binaryTreeCompare becomes this:

function binaryTreeCompare(a, b) {
    return JSON.stringify(a) === JSON.stringify(b)
}

However, the error message you report has nothing to do with your algorithm. It's hard to know for sure what the problem is, because the error message references something that doesn't appear in your sample code. I suspect your real code differs from the code you posted in a way that is critical to the problem.

Tom
  • 8,509
  • 7
  • 49
  • 78
  • please try to make use of the `binaryTreeCompare` function. Thanks for the JSON.stringify approach though – lasabahebwa May 19 '19 at 07:54
  • As I said, the error message you posted makes it pretty clear that the real problem is something else. My read of your binaryTreeCompare is that it should work. I think the code you posted is different from the code you're really using, and that difference appears to be relevant to the error message and thus your problem. We can't help you if you're keeping important information secret. – Tom May 19 '19 at 16:50