I would like to use AVL tree structure as a base under the dictionary. My class:
template <typename keyType, typename dataType>
class AVLTree
{
class Node
{
public:
keyType key;
dataType data;
short int balanceFactor;
Node * left;
Node * right;
Node(const keyType & newKey, const dataType & newData) : key(newKey), data(newData), balanceFactor(0), left(NULL), right(NULL) {};
};
Node * Root;
***methods
void insert(AVLTree<keyType, dataType>::Node* & subtreeRoot, const keyType & newKey, const dataType & newData); //insert new element
void clear(AVLTree<keyType, dataType>::Node* & subtreeRoot); //clear whole tree
void graph(AVLTree<keyType, dataType>::Node* const & subtreeRoot, int indent) const; //print tree
void printDes(AVLTree<keyType, dataType>::Node* const & subtreeRoot) const; //print in descending order
void printAsc(AVLTree<keyType, dataType>::Node* const & subtreeRoot) const; //print in ascending order
...
public:
void search(const keyType & findKey) const;
...};
Let's say I got a sentence:
I like green grass and green trees.
When I got rid of punctuation ('.') and I lower all words I get:
i like green grass and green trees
I would like to put every word in the structure of AVL tree and print it as ascending list:
and- 1
grass- 1
green- 2
i -1
like- 1
trees- 1
where the second 'parameter' is number of occurrences, i.e. in case of green it's 2.
I already did it as a mapping of the singly linked list, but now I want to implement AVL tree and I wonder what should be next steps. I plan to make an outside class COUNTER, with several methods, including this one. My idea:
Iterate through the sentence; if word (key) doesn't exist (search method returns the notification- should be turned into boolean?), add it (insert method) to the tree.
If it existed already, increment second parameter (in this case, data)--- how? If it hasn't, add it to the tree with data=1.
So, final question is: is my idea correct? If so, how to increment second parameter of template then? Is iterator a solution to that?
EDIT: Here is my implementation using Singly Linked List, but without using INFO:
template <typename key>
class counter {
sequence<key> list;
std::map<string, int> map;
public:
void add_word(key new_word) {
list.insertEnd(new_word);
auto iterator = map.find(new_word);
if (iterator != map.end()) {
iterator->second++;
} else {
map.insert({new_word, 1});
}
}
What I wonder it might work:
template <typename key, typename info>
class counter {
AVLTree<key, info> dict;
std::map<string, int> map;
public:
void add_word(key new_word) {
dict.insert(new_word,1);
auto iterator = map.find(new_word);
if (iterator != map.end()) {
iterator->second++;
} else {
map.insert({new_word, 1});
}
}
...}
but I guess it won't work like that