Homework/HW07/bin-trees-B.cc
void buildABinaryTree (nodePtr &rootp) // Parameter passed by REFERENCE!
{
    char action;
    
    while(1)  // Endless loop; there are two "return" statements in the loop.
              // They are executed whenever the user wants to move up the tree.
    {
        if (rootp == NULL)  // The tree or subtree pointed to by rootp is empty.
        {
            do  // This loop continues until the user responds with 'U' or 'C'.
            {
                cout << "\nYou are currently at an empty tree. If you wish to ";
                cout << "create\na new node at this position, type C and press";
                cout << " Enter.\nOtherwise ";
                cout << "type U to go up to the parent node (if any).  ";
                readAndEchoCharResponse (action);
            } while (action != 'C' && action!= 'c' && action!= 'U' 
                     && action!= 'u');
            if (action == 'U' || action == 'u')
                return; // Return control to the calling function
            else
            {
                rootp = new node;
                testDynamicAllocation (rootp);
                rootp->left  = rootp->right = NULL;
                cout << "Enter an integer for the new node:  ";
                readAndEchoIntResponse (rootp->datum);
            }	
        }
        else  // tree is not NULL
        {
            do  // This loop continues until user gives a suitable response.
            {      
                cout << "\nThe datum of the node at which you have arrived is\n";
                cout << "         ";
                cout << rootp->datum << endl;
                cout << "Enter  U  to go up to the parent node,\n";
                cout << "       L  to go down to the left,\n";
                cout << "       R  to go down to the right,\n";
                cout << "       M  to modify value of the datum stored here.\n";
                readAndEchoCharResponse (action);
            }
            while (action != 'U' && action !='u' && action !='L' && action !='l' 
                   && action != 'R' && action !='r' && action !='M' && action !='m'
                   && action != 'P' && action != 'p' && action != 'D' && action != 'd');
      
            switch (action)
            { 
            case 'U': case 'u': return;   // Go back to the calling function.
            case 'L': case 'l': buildABinaryTree (rootp->left);  break;
            case 'R': case 'r': buildABinaryTree (rootp->right); break;
            case 'P': case 'p': graphPrint(rootp); break;
            case 'M': case 'm': cout << "Enter a new datum value to replace"
                    " the current value.  ";
                readAndEchoIntResponse (rootp->datum);
                break;
            case 'D': case 'd': destroyABinaryTree(rootp); // the parameter is 
                // also passed by reference
            } // end of "switch"
        }   // end of "else"     
    }     // end of "while(1)" 
}
/*****************  B U I L D   A   B I N A R Y   T R E E  ******************
                                                                           
The following function allows an interactive user to construct a binary   
tree whose nodes hold integer data values.  The function is recursive. 
Documented and coded by W.Knight. 
Added by D. Vrajitoru in 2012: an option to delete the subtree when the 
rootp pointer is not null. */
B U I L D   A   B I N A R Y   T R E E
The following function allows an interactive user to construct a binary
tree whose nodes hold integer data values.  The function is recursive.
Documented and coded by W.Knight.
Added by D. Vrajitoru in 2012: an option to delete the subtree when the
rootp pointer is not null.
B U I L D A B I N A R Y T R E E The following function allows an interactive user to construct a binary tree whose nodes hold integer data values . The function is recursive . Documented and coded by W . Knight . Added by D . Vrajitoru in 2012 : an option to delete the subtree when the rootp pointer is not null .
void buildABinaryTree (nodePtr &rootp) 
{
    char action;
    while(1)  
    {
        if (rootp == NULL)  
        {
            do  
            {
                cout << "\nYou are currently at an empty tree. If you wish to ";
                cout << "create\na new node at this position, type C and press";
                cout << " Enter.\nOtherwise ";
                cout << "type U to go up to the parent node (if any).  ";
                readAndEchoCharResponse (action);
            } while (action != 'C' && action!= 'c' && action!= 'U' 
                     && action!= 'u');
            if (action == 'U' || action == 'u')
                return; 
            else
            {
                rootp = new node;
                testDynamicAllocation (rootp);
                rootp->left  = rootp->right = NULL;
                cout << "Enter an integer for the new node:  ";
                readAndEchoIntResponse (rootp->datum);
            }	
        }
        else  
        {
            do  
            {      
                cout << "\nThe datum of the node at which you have arrived is\n";
                cout << "         ";
                cout << rootp->datum << endl;
                cout << "Enter  U  to go up to the parent node,\n";
                cout << "       L  to go down to the left,\n";
                cout << "       R  to go down to the right,\n";
                cout << "       M  to modify value of the datum stored here.\n";
                readAndEchoCharResponse (action);
            }
            while (action != 'U' && action !='u' && action !='L' && action !='l' 
                   && action != 'R' && action !='r' && action !='M' && action !='m'
                   && action != 'P' && action != 'p' && action != 'D' && action != 'd');
            switch (action)
            { 
            case 'U': case 'u': return;   
            case 'L': case 'l': buildABinaryTree (rootp->left);  break;
            case 'R': case 'r': buildABinaryTree (rootp->right); break;
            case 'P': case 'p': graphPrint(rootp); break;
            case 'M': case 'm': cout << "Enter a new datum value to replace"
                    " the current value.  ";
                readAndEchoIntResponse (rootp->datum);
                break;
            case 'D': case 'd': destroyABinaryTree(rootp); 
            } 
        }   
    }     
}
void buildABinaryTree ( nodePtr & rootp ) { char action ; while ( 1 ) { if ( rootp == NULL ) { do { cout << " \n " ; cout << " \n " ; cout << " \n " ; cout << " " ; readAndEchoCharResponse ( action ) ; } while ( action != ' ' && action != ' ' && action != ' ' && action != ' ' ) ; if ( action == ' ' || action == ' ' ) return ; else { rootp = new node ; testDynamicAllocation ( rootp ) ; rootp -> left = rootp -> right = NULL ; cout << " " ; readAndEchoIntResponse ( rootp -> datum ) ; } } else { do { cout << " \n \n " ; cout << " " ; cout << rootp -> datum << endl ; cout << " \n " ; cout << " \n " ; cout << " \n " ; cout << " \n " ; readAndEchoCharResponse ( action ) ; } while ( action != ' ' && action != ' ' && action != ' ' && action != ' ' && action != ' ' && action != ' ' && action != ' ' && action != ' ' && action != ' ' && action != ' ' && action != ' ' && action != ' ' ) ; switch ( action ) { case ' ' : case ' ' : return ; case ' ' : case ' ' : buildABinaryTree ( rootp -> left ) ; break ; case ' ' : case ' ' : buildABinaryTree ( rootp -> right ) ; break ; case ' ' : case ' ' : graphPrint ( rootp ) ; break ; case ' ' : case ' ' : cout << " " " " ; readAndEchoIntResponse ( rootp -> datum ) ; break ; case ' ' : case ' ' : destroyABinaryTree ( rootp ) ; } } } }
B U I L D   A   B I N A R Y   T R E E
The following function allows an interactive user to construct a binary
tree whose nodes hold integer data values.
B U I L D A B I N A R Y T R E E The following function allows an interactive user to construct a binary tree whose nodes hold integer data values .
// Parameter passed by REFERENCE! // Endless loop; there are two "return" statements in the loop. // They are executed whenever the user wants to move up the tree. // The tree or subtree pointed to by rootp is empty. // This loop continues until the user responds with 'U' or 'C'. // Return control to the calling function // tree is not NULL // This loop continues until user gives a suitable response. // Go back to the calling function. // the parameter is  // also passed by reference // end of "switch" // end of "else"      // end of "while(1)" 