[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

If /g/ is so smart answer this >Write a recursive method

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 8
Thread images: 1

File: Really.jpg (41KB, 601x601px)
Really.jpg
41KB, 601x601px
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
>>
Ok
struct 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.
Thread posts: 8
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.