Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Printing Trees using C programming

Write a recursive function outputTree to display a binary tree on the
screen. The function should output the tree row-by-row with the top of the tree at the left of the
screen and the bottom of the tree toward the right of the screen. Each row is output vertically. For
example, the binary tree output as follows:

Note the rightmost leaf node appears at the top of the output in the rightmost column, and the
root node appears at the left of the output. Each column of output starts five spaces to the right of
the previous column. Function outputTree should receive as arguments a pointer to the root node
of the tree and an integer totalSpaces representing the number of spaces preceding the value to be
output (this variable should start at zero so the root node is output at the left of the screen). The
function uses a modified inorder traversal to output the tree—it starts at the rightmost node in the
tree and works back to the left. The algorithm is as follows:
While the pointer to the current node is not null
Recursively call outputTree with the current node’s right subtree and totalSpaces + 5
Use a for statement to count from 1 to totalSpaces and output spaces
Output the value in the current node
Set the pointer to the current node to point to the left subtree of the current node
Increment totalSpaces by 5.

Answer:


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// TreeNode structure definition
struct TreeNode { 
   struct TreeNode *leftPtr;  // pointer to left subtree
   int data; // node data
   struct TreeNode *rightPtr; // pointer to right subtree
}; // end struct TreeNode

typedef struct TreeNode TreeNode;
typedef TreeNode *TreeNodePtr;

// function prototypes
void insertNode( TreeNodePtr *treePtr, int value );
void outputTree( TreeNodePtr treePtr, int spaces );

int main( void )
{ 
   int i; // loop counter
   int item; // random value to be inserted
   int totalSpaces = 0; // spaces preceding output
   TreeNodePtr rootPtr = NULL; // points to the tree root

   srand( time( NULL ) ); // randomize
   puts( "The numbers being placed in the tree are:" );

   // insert random values between 1 and 10 in the tree
   for ( i = 1; i <= 10; ++i ) { item = rand() % 15;
 printf( "%3d", item );
 insertNode( &rootPtr, item ); } // end for
 puts( "\n" ); 
outputTree( rootPtr, totalSpaces ); // display tree
 } // end main 
// insert a node into the tree 
void insertNode( TreeNodePtr *treePtr, int value ) {
 // if treePtr is NULL
 if ( *treePtr == NULL ) { 
// dynamically allocate memory
 *treePtr = malloc( sizeof( TreeNode ) );
 // if memory was allocated, insert node
 if ( *treePtr != NULL ) { ( *treePtr )->data = value;
         ( *treePtr )->leftPtr = NULL;
         ( *treePtr )->rightPtr = NULL;
      } // end if
      else {
         printf( "%d not inserted. No memory available.\n", value );
      } // end else
   } // end if
   else { // recursively call insertNode
      // insert node in left subtree
      if ( value < ( *treePtr )->data ) {
         insertNode( &( ( *treePtr )->leftPtr ), value );
      } // end if
      else {
         // insert node in right subtree
         if ( value > ( *treePtr )->data ) {
            insertNode( &( ( *treePtr )->rightPtr ), value );
         } // end if
         else { // duplicate value
            printf( "%s", "dup" );
         } // end else
      } // end else
   } // end else
} // end function insertNode

// display the tree
void outputTree( TreeNodePtr treePtr, int spaces )
{ 
   int loop; // loop counter

   // while not the end of tree
   while ( treePtr != NULL ) { 

      // recursive call with right subtree
      outputTree( treePtr->rightPtr, spaces + 5 );

      // loop and output spaces
      for ( loop = 1; loop <= spaces; ++loop ) { printf( "%s", " " ); } // end for 
      printf( "%d\n", treePtr->data );

      // set pointer to left subtree and make recursive call
      outputTree( treePtr->leftPtr, spaces + 5 );
      treePtr = NULL;
   } // end while
} // end function outputTree

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!