Question

In: Computer Science

DEVELOP A C++ PROGRAM IN PROCEDURAL CODE that will recursively alphabetize a set of strings in...

DEVELOP A C++ PROGRAM IN PROCEDURAL CODE that will recursively alphabetize a set of strings in a user-specified file using the tree sort algorithm explained in the lectures while maintaining a balanced binary tree at all times.

The following will test your program with an input file containing:

Max Hank Jet Frisky Chata Richard Nan Sam Thomas Karen Gerri Ingrid Alan Dana

When done print out the contents of the tree with inorder traversal in a tabular format similar to the following:

NODE

LEFT

RIGHT

HEIGHT

BALANCE

Clarise

null

null

1

0

Fred

Clarise

Henry

2

0

Henry

null

null

1

0

Jane

Fred

Nan

4

-1

Mark

null

null

1

0

Nan

Mark

Susan

3

-1

Ryan

null

null

1

0

Susan

Ryan

Tammy

2

0

Tammy

null

null

1

0

enhance your program to allow for duplicates in the input data (but do NOT put duplicate entries in the tree). Expand the above table to contain a column for MULTIPLICITY, which keeps track of the number of duplicates encountered for each node in the tree. Turn in a SEPARATE program for this option, called enhance.cpp.

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

// Structure for tree
struct Node
{
   string name;//string of key will be taken from file
   struct Node *lC, *rC; // left and right children
   int height, balance, multiplicity;// height, balance, multiplicity
};

struct Node* getNode( string realName )// function to the struct Node pointing to getNode
{
   struct Node* node = new Node;// dynamic allocation of a new node with its' attributes
  
   node -> name = realName;
   node -> lC = NULL;
   node -> rC = NULL;
   node -> balance = 0;
   node -> multiplicity = 1;
   node -> height = 1;
   return node;
}

int height( struct Node *root )// Finds height
{
if ( root == NULL ) return 0;

   return root -> height;
}

int total( int x, int y ) // Finds total
{
   return( x > y ) ? x : y; // bitwise operators
}

struct Node *shiftRight( struct Node *root ) // Shifts to the right
{
   struct Node *t1 = root -> lC;
   struct Node *t2 = t1 -> rC;
  
   // Shifting occurs
   t1 -> rC = root;
   root -> lC = t2;
  
   // height update
   root -> height = total( height( root -> lC ), height( root -> rC ) ) + 1;
   t1 -> height = total( height( t1 -> lC ), height( t1 -> rC ) ) + 1;

   return t1;
}

struct Node *shiftLeft( struct Node *root ) // Shifts to the left
{
   struct Node *t1 = root -> rC;
   struct Node *t2 = t1 -> lC;

   // Shifting occurs
   t1 -> lC = root;
   root -> rC = t2;

   // Height update
   root -> height = total( height( root -> lC ), height( root -> rC ) ) + 1;
   t1 -> height = total( height( t1 -> lC ), height( t1 -> rC ) ) + 1;

   return t1;
}


int balance( struct Node *root ) // Get Balance
{
   if ( root == NULL) return 0;

   return height( root -> lC ) - height( root -> rC );
}

struct Node* addNode( struct Node* root, string realName ) // Adds nodes
{
  
   if ( root == NULL ) return( getNode( realName ) );

   if ( realName < root -> name ) root -> lC = addNode( root -> lC, realName );

   else if ( realName > root -> name )
       root -> rC = addNode( root -> rC, realName );

   else
   {
       root -> multiplicity++;
       return root;
   }

  
   root -> height = total( height( root -> lC ), height( root -> rC ) ) + 1;
   int b = balance( root );

/*******************************************************************
                       FOUR IMBALANCES
*******************************************************************/

   // L - L Imbalance
   if ( b > 1 && realName < ( root -> lC ) -> name ) return shiftRight( root );

   // R - R Imbalance
   if ( b < -1 && realName > ( root -> rC ) -> name ) return shiftLeft( root );

   // L - R Imbalance
   if ( b > 1 && realName > ( root -> lC ) -> name )
   {
       root -> lC = shiftLeft( root -> lC );
       return shiftRight( root );
   }

   // R - L Imbalance
   if ( b < -1 && realName < ( root -> rC ) -> name )
       {
           root -> rC = shiftRight( root -> rC );
           return shiftLeft( root );
       }

   root -> balance = balance( root );
  
   return root;
}

void balanceSearch( Node *root ) // Finds Balance
{
if ( root != NULL )
   {
       balanceSearch( root -> lC );
       root -> balance = balance( root );
       balanceSearch( root -> rC );
   }
}

void spacing( int c ) // tabular formatting
{
   for ( int i = 0; i < (12 - c); i++ ) cout << " ";
}

void printInOrder( struct Node *root ) // In-Order Notation
{
   if ( root != NULL )
   {
       printInOrder( root -> lC );
       cout << "\n " << root -> name;
       spacing( root -> name.length() );

       string left, right;

       if ( root -> lC == NULL ) left = "NULL";

       else left = ( root -> lC ) -> name;
          
       if ( root -> rC == NULL ) right = "NULL";

       else right = ( root -> rC ) -> name;

       cout << left;
       spacing( left.length() );
       cout << right;
       spacing( right.length() );
       cout << root -> height << " \t " << root -> balance << " \t     " << root -> multiplicity;
       printInOrder( root -> rC );
   }
}


int main()
{
   struct Node *tree = NULL;
   string input;     // user input
   ifstream infile; // user-specified file

   cout << "Enter The Requested File: "; cin >> input;

   infile.open( input.c_str(), ios :: in );

   while ( !infile.eof() )
   {
       infile >> input;
       tree = addNode( tree, input );
   }

   infile.close();
   balanceSearch( tree );
   cout << " Node        Left        Right       Height     Balance     Multiplicity\n";
   printInOrder( tree );
   cout << "\n" ;

   return 0;
}


Related Solutions

Constant Current Code for Arduino Develop a Constant Current Program in C and upload it into...
Constant Current Code for Arduino Develop a Constant Current Program in C and upload it into your Arduino #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Q) You have been asked to develop a program which processes C++ null-terminated strings to extract...
Q) You have been asked to develop a program which processes C++ null-terminated strings to extract character and numerical data separately. Assume that the users runs the application (examTest.exe) from the command line as follows: examTest.exe X-Axis:10,Y-Axis:17,Z-Axis:-6 Your application should step through the input arguments and extract the values of the XAxis, Y-Axis and Z-Axis commands. The user must always enter the arguments in the specified order, however your application should check they are correct. At the end of your...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
Power Factor Code Arduino Develop a Power factor Program in C and upload it into your...
Power Factor Code Arduino Develop a Power factor Program in C and upload it into your Arduino #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
MUST BE DONE IN C (NOT C++) This program should utilize the basics of strings. First,...
MUST BE DONE IN C (NOT C++) This program should utilize the basics of strings. First, declare and initialize a string. You can name the variable whichever way you want and you can initialize it to whichever value you want. Then, use a for loop to print its characters; one at a time, until you reach the null character. After this, go ahead and declare a second string (since you are not initializing it right away, you will have to...
Write a program in C++ that will make changes in the list of strings by modifying...
Write a program in C++ that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and...
IN C LANGUAGE This program will read in a series of strings and print only the...
IN C LANGUAGE This program will read in a series of strings and print only the consonants (including Y) until the word "stop" appears. No string will be longer than 100 characters. A consonant is any letter that is not a vowel. Don't forget to follow the standard read pattern! Examples Enter a string: Hello Hll Enter a string: World! Wrld Enter a string: 123! Enter a string: stop Enter a string: stop
Write C++ code that prompts the user for a username and password (strings), and it calls...
Write C++ code that prompts the user for a username and password (strings), and it calls the login() function with the username and password as arguments. You may assume that username and password have been declared elsewhere. Your code should print error messages if login() generates an exception: LoginException: "Username not found or password incorrect" ServerException: "The login server is busy and you cannot log in" AccessException: "Your account is forbidden from logging into this server" Any other exception: "Unknown...
Write C++ code that prompts the user for a username and password (strings), and it calls...
Write C++ code that prompts the user for a username and password (strings), and it calls the login() function with the username and password as arguments. You may assume that username and password have been declared elsewhere. Use virtual functions, pure virtual functions, templates, and exceptions wherever possible. Please explain the code. Your code should print error messages if login() generates an exception: LoginException: "Username not found or password incorrect" ServerException: "The login server is busy and you cannot log...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT