Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone tell me what should I have to fill the last 2 lines? There's no errors at all, but just need to fill the

Can someone tell me what should I have to fill the last 2 lines? There's no errors at all, but just need to fill the last 2 lines in this code. However, I typed couple words in there but it didn't work as the sample output. Below is sample output and the code.

Small Example:

Inorder Traversal:

ate:1 cat:1 dog:1 leashed:1

rabid:1 the:2 whole:1

Depth of tree: 4

Total used nodes: 7

Total possible nodes: 15

Percentage of nodes used:47%

=========================================================

#include

#include

#include

#include "BinaryTrees.h"

using namespace std;

// Creates the initial tree by setting root equal to NULL

void BinaryTrees::create()

{

root = NULL;

}

// Destroys the tree by going through and deleting each subtree

void BinaryTrees::destroy(TREEPTR &subtree)

{

if (subtree != NULL)

{

destroy(subtree->left);

destroy(subtree->right);

delete subtree;

subtree = NULL;

}

}

// Boolean check to see if the tree is empty

bool BinaryTrees::empty()

{

return (root == NULL);

}

// Boolean check to see if there is space left in memory for more nodes

bool BinaryTrees::full()

{

TREEPTR temp;

temp = new TreeNode;

if (temp == NULL)

return true;

else

return false;

}

// Inserts new words as new nodes, adds to word count, tracks total node count

void BinaryTrees::insert(TREEPTR& subtree, string word, int& nodeCount)

{

if (subtree == NULL)

{

subtree = new TreeNode;

subtree->wordCount = 1;

nodeCount++;

subtree->word = word;

subtree->left = NULL;

subtree->right = NULL;

}

else if (subtree->word.compare(word) == 0)

subtree->wordCount++;

else if (subtree->word.compare(word) > 0)

{

insert(subtree->left, word, nodeCount);

}

else

{

insert(subtree->right, word, nodeCount);

}

}

// Prints out the Preorder Traversal of the tree

void BinaryTrees::preorder(TREEPTR subtree)

{

if (subtree != NULL)

{

cout << subtree->word;

cout << ": " << subtree->wordCount << endl;

preorder(subtree->left);

preorder(subtree->right);

}

}

// Prints out the Postorder Traversal of the tree

void BinaryTrees::postorder(TREEPTR subtree)

{

if (subtree != NULL)

{

postorder(subtree->left);

postorder(subtree->right);

cout << subtree->word;

cout << ": " << subtree->wordCount << endl;

}

}

// Prints out the Inorder Traversal of the tree

void BinaryTrees::inorder(TREEPTR subtree)

{

// This recurses left, then visits the node, then it recurse right

if (subtree != NULL)

{

inorder(subtree->left);

cout << subtree->word;

cout << ": " << subtree->wordCount << endl;

inorder(subtree->right);

}

}

// Preorder, you visit the node, you recurse left, then you recurse right

// Posorder, you recurse left, you recurse right, you visit the node

// Counts the total depth of the tree

int BinaryTrees::depth(TREEPTR subtree)

{

int left, right;

if (subtree == NULL)

{

return 0;

}

else

{

left = depth(subtree->left);

right = depth(subtree->right);

if (left > right)

return left + 1;

else

return right + 1;

}

}

// Performs a search on the tree to determine if a given string is present

TREEPTR BinaryTrees::search(TREEPTR subtree, string key)

{

if (subtree == NULL)

return NULL;

else if (subtree->word.compare(key) == 0)

return subtree;

else if (subtree->word > key)

return search(subtree->left, key);

else

return search(subtree->right, key);

}

#ifndef BINARYTREES_H_ #define BINARYTREES_H_

using namespace std;

struct TreeNode { int wordCount; string word; struct TreeNode *left, *right; };

typedef struct TreeNode *TREEPTR;

class BinaryTrees { public: TREEPTR root;

void create(); void destroy(TREEPTR&); bool empty(); bool full(); void insert(TREEPTR&, string, int&); void preorder(TREEPTR); void postorder(TREEPTR); void inorder(TREEPTR); int depth(TREEPTR); TREEPTR search(TREEPTR, string);

};

#endif /* BINARYTREES_H_ */

#include

#include "Parser.h"

using namespace std;

// Converts each valid character into upper case letters

void Parser::upperCase(string &s)

{

for (unsigned int n = 0; n < s.length(); n++)

s[n] = toupper(s[n]);

}

// Boolean check to determine which characters qualify as valid input

bool Parser::blackSpace(char ch)

{

return (ch == (char)39 || (ch >= 'A' && ch <= 'Z'));

}

// Boolean check on content that is not blackspace (Not used but an important concept for future classes)

bool Parser::whiteSpace(char ch)

{

return !blackSpace(ch);

}

// Cleans each string by removing characters that are not considered valid input

string Parser::cleanString(string s)

{

string result = "";

for (unsigned int n = 0; n < s.length(); n++)

{

if (blackSpace(s[n]))

result = result + s[n];

}

return result;

}

#ifndef PARSER_H_

#define PARSER_H_

#include

using namespace std;

class Parser

{

public:

void upperCase(string&);

bool blackSpace(char);

bool whiteSpace(char);

string cleanString(string);

};

#endif /* PARSER_H_ */

#include

#include

#include

#include "BinaryTrees.h"

#include "Parser.h"

using namespace std;

void traversals();

int main()

{ traversals();

return 0;

}

void traversals()

{

BinaryTrees tree;

Parser parse;

string buffer;

ifstream fin;

int nodeCount = 0;

TREEPTR tp;

tree.create();

fin.open("lorem.txt");

while(!fin.eof())

{

fin >> buffer;

parse.upperCase(buffer);

buffer = parse.cleanString(buffer);

if (!tree.full() && buffer.length() != 0)

tree.insert(tree.root, buffer, nodeCount);

} // end while

cout << "Inorder Traversal:" << endl;

tree.inorder(tree.root);

cout << endl;

cout << "Preorder Traversal:" << endl;

tree.preorder(tree.root);

cout << endl;

cout << "Postorder Traversal:" << endl;

tree.postorder(tree.root);

cout << endl;

cout << "Depth of Tree: " << tree.depth(tree.root) << endl;

cout << "Total Used Nodes: " << nodeCount << endl;

cout << "Total Possible Nodes: " << //fill this

cout << "Percentage of Nodes Used: " << //fill this

tree.destroy(tree.root);

fin.close();

} // end traversals

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago