Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using namespace std; #include <stdio.h> #include <string.h> #include <assert.h>

ID: 3576522 • Letter: U

Question

using namespace std;

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <iostream.h>

#define MAX_STR_LEN 100

// int strcmp (const char* str1, const char* str2); // string.h
// Compares the C string str1 to the C string str2.
// This function starts comparing the first character of each string.
// If they are equal to each other, it continues with the following pairs
// until the characters differ or until a terminating null-character is reached.
// This function performs a binary comparison of the characters.
// For a function that takes into account locale-specific rules, see strcoll.

// Do not change "class CTreeNode"
class CTreeNode {
public:
   char m_strKeyString [MAX_STR_LEN];  
   CTreeNode* m_pLeftChild;
   CTreeNode* m_pRightChild;
  
   CTreeNode () {
       m_strKeyString [0] = '';   // initially the key string is empty
       m_pLeftChild = NULL;
       m_pRightChild = NULL;
   };  
  
  

int main (int argc, char* argv [])
{
   char* pstrIntput [] = {"move", "look", "new", "apple", "printer,", "program", "special", "eagle", "cheese", "zoo", "climb", "class"};
   int nInputCount = 12;
  
   // printf ("%s ", pstrIntput [0]);
  
   /****************************** Problem 1 (10 points) ********************************/
  
  
  
  
   // Construct a Binary Search Tree (BSTree) using CTreeNode with pstrIntput as a base input.
   // Once the BSTree is constructed, print out the key strings using inorder traversal.
   // Must use "CTreeNode" to contstruct BSTree. Only print out the collect order of the string will not grant any point.
   // No Partial Credit will be given!
  
  
  
  
   /****************************** Problem 2 (10 points) ********************************/
   // Construct a Min Heap CTreeNode with pstrIntput as a base input. -- Therefore, apple should be the root
   // Once the tree is constructed is constructed, print out the key strings using preorder traversal.
   // Must use "CTreeNode" to contstruct the heap tree. Only print out the collect order of the string will not grant any point.
   // No Partial Credit will be given!
  
  
  

   return 0;
}

// need urgent help

Explanation / Answer

using namespace std;
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#define MAX_STR_LEN 100
// int strcmp (const char* str1, const char* str2); // string.h
// Compares the C string str1 to the C string str2.
// This function starts comparing the first character of each string.
// If they are equal to each other, it continues with the following pairs
// until the characters differ or until a terminating null-character is reached.
// This function performs a binary comparison of the characters.
// For a function that takes into account locale-specific rules, see strcoll.
// Do not change "class CTreeNode"
class CTreeNode {
public:
char m_strKeyString [MAX_STR_LEN];
CTreeNode* m_pLeftChild;
CTreeNode* m_pRightChild;
  
CTreeNode () {
m_strKeyString [0] = ''; // initially the key string is empty
m_pLeftChild = NULL;
m_pRightChild = NULL;
};

};   


  
// A utility function to do inorder traversal of BST
void inorder(CTreeNode *root)
{
if (root != NULL)
{
inorder(root->m_pLeftChild);
printf("%s ", root->m_strKeyString);
inorder(root->m_pRightChild);
}
}
  
/* A utility function to insert a new node with given key in BST */
CTreeNode * insert(CTreeNode * node, char * key)
{
/* If the tree is empty, return a new node */
if (node == NULL){

CTreeNode * tree = new CTreeNode();
for (int i = 0; i < strlen(key); ++i)
{
tree->m_strKeyString[i] = *(key+i);
}

return tree;
}

/* Otherwise, recur down the tree */
if (strcmp(key, node->m_strKeyString) < 0)
node->m_pLeftChild = insert(node->m_pLeftChild, key);
else if (strcmp(key, node->m_strKeyString) > 0)
node->m_pRightChild = insert(node->m_pRightChild, key);   

/* return the (unchanged) node pointer */
return node;
}

  
int main (int argc, char* argv [])
{
char* pstrIntput [] = {"move", "look", "new", "apple", "printer,", "program", "special", "eagle", "cheese", "zoo", "climb", "class"};
int nInputCount = 12;
CTreeNode * tree = new CTreeNode();
for (int i = 0; i < nInputCount; ++i)
{
insert(tree, pstrIntput[i]);
}
cout << "sorted strings are following " << endl;
inorder(tree);

  

  
  
return 0;
}

OUTPUT

sorted strings are following

apple
cheese
class
climb
eagle
look
move
new
printer,
program
special
zoo