Data Str &amp; Algorithm HW Solutions 40

# Data Str &amp; Algorithm HW Solutions 40 -...

This preview shows page 1. Sign up to view the full content.

6 General Trees 6.1 The following algorithm is linear on the size of the two trees. // Return TRUE iff t1 and t2 are roots of identical // general trees template <class Elem> bool Compare(GTNode<Elem>* t1, GTNode<Elem>* t2) { GTNode<Elem> *c1, *c2; return false; if ((t1 == NULL) & (t2 == NULL)) return true; if (t1->val() != t2->val()) return false; c1 = t1->leftmost_child(); c2 = t2->leftmost_child();
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if (!Compare(c1, c2)) return false; if (c1 != NULL) c1 = c1->right_sibling(); if (c2 != NULL) c2 = c2->right_sibling(); }} 6.2 The following algorithm is Θ( n 2 ) . // Return true iff t1 and t2 are roots of identical // binary trees template <class Elem> bool Compare2(BinNode<Elem>* t1, BinNode<Elem* t2) { BinNode<Elem> *c1, *c2; if (((t1 == NULL) && (t2 != NULL)) || ((t2 == NULL) && (t1 != NULL))) return false; if ((t1 == NULL) && (t2 == NULL)) return true; 40...
View Full Document

## This note was uploaded on 12/27/2011 for the course MAP 2302 taught by Professor Bell,d during the Fall '08 term at UNF.

Ask a homework question - tutors are online