Difference between revisions of "TADM2E 3.21"

From Algorithm Wiki
Jump to: navigation, search
(Recovering wiki)
 
(Recovering wiki)
 
Line 1: Line 1:
<pre>
+
<pre>
 
bool compare(struct node* a, struct node* b) {
 
bool compare(struct node* a, struct node* b) {
  
   // 1. both empty -&gt; true
+
   // 1. both empty -> true
   if (a==NULL &amp;&amp; b==NULL) return(true);   
+
   if (a==NULL && b==NULL) return(true);   
  
// 2. both non-empty -&gt; compare them
+
// 2. both non-empty -> compare them
   else if (a!=NULL &amp;&amp; b!=NULL) {
+
   else if (a!=NULL && b!=NULL) {
 
     return(
 
     return(
       a-&gt;data == b-&gt;data &amp;&amp;
+
       a->data == b->data &&
       compare(a-&gt;left, b-&gt;left) &amp;&amp;
+
       compare(a->left, b->left) &&
       compare(a-&gt;right, b-&gt;right)
+
       compare(a->right, b->right)
 
     );
 
     );
 
   }
 
   }
  
   // 3. one empty, one not -&gt; false
+
   // 3. one empty, one not -> false
 
   else return(false);
 
   else return(false);
 
}   
 
}   
&lt;/pre&gt;
+
</pre>
 
--[[User:Max|Max]] 06:41, 25 June 2010 (EDT)
 
--[[User:Max|Max]] 06:41, 25 June 2010 (EDT)

Latest revision as of 18:22, 11 September 2014

bool compare(struct node* a, struct node* b) {

  // 1. both empty -> true
  if (a==NULL && b==NULL) return(true);   

// 2. both non-empty -> compare them
  else if (a!=NULL && b!=NULL) {
    return(
      a->data == b->data &&
      compare(a->left, b->left) &&
      compare(a->right, b->right)
    );
  }

  // 3. one empty, one not -> false
  else return(false);
}  

--Max 06:41, 25 June 2010 (EDT)