If /g/ is so smart answer this
>Write a recursive method compTree(t1, t2) that compares two binary trees. compTree(t1,
t2) should return TRUE or FALSE, depending on whether the two trees are the same or not.
>>58437277
/g/ - homework
Do your own homework, Raj.
var compTree = function(tree1, tree2) {
var same = true;
if (tree1.left !== tree2.left) {
same = false;
}
if (tree1.right !== tree2.right) {
same = false;
}
return same && compTree(tree1.left, tree2.left) && compTree(tree1.right, tree2.right)
}
btw this will not work
Okstruct tree {
int payload;
struct tree *left;
struct tree *right;
};
bool compTree (struct tree *t1, struct tree *t2)
{
if ((t1 == NULL) && (t2 == NULL))
return true;
if ((t1 == NULL) ^ (t2 == NULL))
return false;
return (t1->payload == t2->payload) && compTree(t1->left, t2->left) && compTree(t1->right, t2->right);
}
bool
compTree(Tree t1, Tree t2)
{
return t1 == t2;
}
If they have the same root and the same pointers to their left and right subtrees, then they are the same.
Now back to the homework board:
>>>/hm/
void compTree(t1, t2) {
// int128 so we can compute BIG numbers 4u
int128 t1, t2;
// this is where the magic happens
while (i=t1/t2) do {
recurse(int128.to.const_char*(t1), int128.to.const_char*(t2));
}
return 0;
}
;; assume we have tree?, tree-left, and tree-right to
;; check if is tree, access left child, and access right child,
(define (compTree t1 t2)
(if (and (tree? t1) (tree? t2))
(and (compTree (tree-left t1) (tree-left t2))
(compTree (tree-right t1) (tree-right t2)))
(eq? t1 t2)))
Doing this efficiently (without blowing the stack) is left as an exercise to the reader.