Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with write pseudocode for this program and big O notations as well #include #include #include #include #include #include using namespace std; //

I need help with write pseudocode for this program and big O notations as well

#include #include #include #include #include #include using namespace std;

// An AVL tree Node struct Node { int key; struct Node *left, *right; int height, count; };

// Function to isnert a key in AVL Tree, if key is already present, // then it increments count in key's node. struct Node* insert(struct Node* Node, int key);

// This function puts inorder traversal of AVL Tree in arr[] void inorder(int arr[], struct Node *root, int *index_ptr);

// An AVL tree based sorting function for sorting an array with // duplicates void sorted(int arr[],int n) { // Create an empty AVL Tree struct Node *root = NULL;

// Insert all nodes one by one in AVL tree. The insert function // increments count if key is already present for (int i=0; i

// Do inorder traversal to put elements back in sorted order int index = 0; inorder(arr, root, &index); }

// This function puts inorder traversal of AVL Tree in arr[] void inorder(int arr[], struct Node *root, int *index_ptr) { if (root != NULL) { // Recur for left child inorder(arr, root->left, index_ptr);

// Put all occurrences of root's key in arr[] for (int i=0; icount; i++) { arr[*index_ptr] = root->key; (*index_ptr)++; }

// Recur for right child inorder(arr, root->right, index_ptr); } }

// A utility function to get height of the tree int height(struct Node *N) { if (N == NULL) return 0; return N->height; }

// Helper function that allocates a new Node struct Node* newNode(int key) { struct Node* node = new Node; node->key = key; node->left = node->right = NULL; node->height = node->count = 1; return(node); }

// A utility function to right rotate subtree rooted // with y. struct Node *rightRotate(struct Node *y) { struct Node *x = y->left; struct Node *T2 = x->right;

// Perform rotation x->right = y; y->left = T2;

// Update heights y->height = max(height(y->left), height(y->right))+1; x->height = max(height(x->left), height(x->right))+1;

// Return new root return x; }

// A utility function to left rotate subtree rooted with x struct Node *leftRotate(struct Node *x) { struct Node *y = x->right; struct Node *T2 = y->left;

// Perform rotation y->left = x; x->right = T2;

// Update heights x->height = max(height(x->left), height(x->right))+1; y->height = max(height(y->left), height(y->right))+1;

// Return new root return y; }

// Get Balance factor of Node N int getBalance(struct Node *N) { if (N == NULL) return 0; return height(N->left) - height(N->right); }

// Function to insert a key in AVL Tree, if key is already // present, then it increments count in key's node. struct Node* insert(struct Node* Node, int key) { /* 1. Perform the normal BST rotation */ if (Node == NULL) return (newNode(key));

// If key already exists in BST, icnrement count and return if (key == Node->key) { (Node->count)++; return Node; }

/* Otherwise, recur down the tree */ if (key < Node->key) Node->left = insert(Node->left, key); else Node->right = insert(Node->right, key);

/* 2. Update height of this ancestor Node */ Node->height = max(height(Node->left), height(Node->right)) + 1;

/* 3. Get the balance factor of this ancestor Node to check whether this Node became unbalanced */ int balance = getBalance(Node);

// If this Node becomes unbalanced, then there are 4 cases

// Left Left Case if (balance > 1 && key < Node->left->key) return rightRotate(Node);

// Right Right Case if (balance < -1 && key > Node->right->key) return leftRotate(Node);

// Left Right Case if (balance > 1 && key > Node->left->key) { Node->left = leftRotate(Node->left); return rightRotate(Node); }

// Right Left Case if (balance < -1 && key < Node->right->key) { Node->right = rightRotate(Node->right); return leftRotate(Node); }

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

using namespace std;

int checkWord(string [],string); string removePunctuation(string); void sortAsAlphabet(string [],int [],int,int); string removeDuplicatesFromString(string);

int main() {

string line,word; string words[1000]; string filename; int count[1000],i=0,j; int numWords=0;

//string next;

std::set word_c;

// asking for filename from console cout << "Enter file name: "; cin>>filename;

ifstream infile(filename);

// if any error occurs while opening file if(!infile) { cout<<"Unable to open the file..exiting!"<

// otherwise else { while (getline(infile,line)) {

stringstream ss ( line ); while(ss>>word) { word=removePunctuation(word); removeDuplicatesFromString(word); for(int i=0;i

if (i==0) { words[i]=word; count[i]=1; } else { int pos=checkWord(words,word); if(pos!=9999) { count[pos]+=1; i=i-1; }

else { words[i]=word; count[i]=1; } } ++i; ++numWords; }

// counting total number of words

} }

// printing total number of words cout<<" Total number of words: "<

for (int k = 0; k < i; ++k) { for (int j = k + 1; j < i; ++j) { if (count[k] < count[j]) { int a = count[k]; string s=words[k]; count[k] = count[j]; words[k]=words[j]; count[j] = a; words[j]=s; } } } int max=count[0]; int co=1,from=99999,to=0; do { for(int j=0;j1)

sorted(count,0); co++; }while(co<=max);

// displaying first fifteen words cout<<" First fifteen words with their count : "<

// displaying last fifteen words cout<<" Last fifteen words with their count : "<

// checkword function int checkWord(string s[1000],string w) { for(int i=0;!s[i].empty();i++) { if(s[i]==w) return i; } return 9999; }

// removePunctuation function string removePunctuation(string w) { for (int j = 0, len = w.size(); j < len; j++) { // check whether parsing character is punctuation or not if (ispunct(w[j])) { w.erase(j--, 1); len = w.size(); } }

return w;

} string removeDuplicatesFromString(string str) {

// keeps track of visited characters int counter = 0;

int i = 0; int size = str.size();

// gets character value int x;

// keeps track of length of resultant string int length = 0;

while (i < size) { x = str[i] - 97;

// check if Xth bit of counter is unset if ((counter & (1 << x)) == 0) {

str[length] = 'a' + x;

// mark current character as visited counter = counter | (1 << x);

length++; } i++; }

return str.substr(0, length);

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

Have pattern like hair on their skin?

Answered: 1 week ago

Question

What are aquatic animal?

Answered: 1 week ago