Advertisement
avv210

Pre Order Traversal Bug

May 18th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     int data;
  8.     int height;
  9.     Node* left;
  10.     Node* right;
  11.     Node* parent;
  12.  
  13.     Node( int val )
  14.     {
  15.         this->data = val;
  16.         left = right = parent = NULL;
  17.         height = 1;
  18.     }
  19. };
  20.  
  21. Node* addData( Node* node, int value, Node* parent )
  22. {
  23.     if ( node == NULL )
  24.     {
  25.         Node* newNode = new Node( value );
  26.         if ( parent == NULL )
  27.             newNode->parent = NULL;
  28.        
  29.         else
  30.             newNode->parent = parent;
  31.        
  32.         return newNode;
  33.     }
  34.  
  35.     if ( value < node->data )
  36.         node->left = addData( node->left, value, node );
  37.    
  38.     else if ( value > node->data )
  39.         node->right = addData( node->right, value, node );
  40.    
  41.     else
  42.     {
  43.         printf( "Data cannot be duplicated!!\n" );
  44.         return node;
  45.     }
  46.  
  47.     return node;  
  48. }
  49.  
  50. void preOrder( Node* node )
  51. {
  52.     if ( node == NULL )
  53.         return;
  54.    
  55.     // Algorithm of preOrder: VLR
  56.     cout << node->data << " ";  // visit
  57.     preOrder( node->left );     // left node
  58.     preOrder( node->right );    // right node
  59. }
  60.  
  61. int main()
  62. {
  63.     short option;
  64.     Node* root = NULL;
  65.     int x;
  66.  
  67.     do
  68.     {
  69.         system( "CLS" );
  70.         printf( "Menu\n" );
  71.         printf( "1. Insert data to tree\n" );
  72.         printf( "2. Search data\n" );
  73.         printf( "3. Delete data from tree - deletion by Merging\n" );
  74.         printf( "4. Delete data from tree - deletion by Copying\n" );
  75.         printf( "5. Pre-order traversal\n" );
  76.         printf( "6. Post-order traversal\n" );
  77.         printf( "7. In-order traversal\n" );
  78.         printf( "8. Binary Search Tree\n" );
  79.         printf( "9. Delete Leaf\n" );
  80.         printf( "10. Delete Node with one child\n" );
  81.         printf( "0. exit\n\n" );
  82.  
  83.         printf( "Your choice : " );
  84.         scanf( "%d", &option );
  85.  
  86.         int i = 0;
  87.         switch ( option )
  88.         {
  89.             case 0:
  90.                 printf( "Thank you for using this program!\n" );
  91.                 exit(0);
  92.                 break;
  93.  
  94.             case 1:
  95.                 // Input data: 50 25 75 10 35 60 85 5 15 30 40 55 65 80 90
  96.                 printf( "Input Data, to end, press 0\n\n" );
  97.                 do
  98.                 {
  99.                     printf( "Input data-%d : ", ++i );
  100.                     scanf( "%d", &x );
  101.                 } while( x > 0 );
  102.  
  103.                 root = addData( root, x, NULL );
  104.                 printf( "\nInput Data Process is Finish!\n" );
  105.                 break;
  106.  
  107.             case 5:
  108.                 printf( "Pre Order Traversal: " );
  109.                 preOrder( root ); // BUG: always shows 0. Why??
  110.                 printf( "\n\n" );
  111.                 break;
  112.  
  113.             default:
  114.                 break;
  115.         }
  116.     } while ( option != 0 );
  117.  
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement