sync void buildTree( void )  // assumes #==N procs.
{
 pr int c, w = -1;
 root = $;     // concurrent write, proc.(#-1) wins 
 myParent = root;               // on priority CRCW PRAM
 lchild[$] = rchild[$] = N;
 if ($!=root) {
   while (w!=$) {
     farm // evaluate condition separately:
       c = (key[$]<key[myParent]
           || (key[$]==key[myParent] && $<myParent) );
     if ( c )
     { // I must go to the left of myParent:
       lchild[myParent] = $;  // (arbitrary) conc. write
       IAmALeftChild = 1;
       w = lchild[myParent];  // read what has been written
       if (w!=$)         // someone else (w) was successful:
          myParent = w;  // w becomes my new parent node
       // else I am the new parent node: I am done
     }
     else { // I must go to the right of myParent:
       rchild[myParent] = $; // select one processor arbitrarily
       IAmALeftChild = 0;
       w = rchild[myParent];
       if (w!=$)
          myParent = w;
     }
   } // while ($!=w);
 }
#if SHOWTREE
 seq showTree( 0, root );
#endif
}
