1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
using namespace std;
#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>
#include <algorithm>
string lastWord;
int getLeftChildIndex(int index)
{
return (2*index)+1;
}
int getRightChildIndex(int index)
{
return (2*index)+2;
}
int getParentIndex(int index)
{
return (index-1)/2;
}
void padString(string &Tree, int index)
{
Tree += std::string((Tree.size() - index)+1, ' ');
}
unsigned char insertOrderedPoint(string &Tree, char CharToInsert, int index)
{
//calculate and add needed space to string
padString(Tree, index);
unsigned char category = 0;
if(Tree[index] == ' ')
{
Tree[index] = CharToInsert;
}
else if (CharToInsert < Tree[index])
{
insertOrderedPoint(Tree, CharToInsert, getLeftChildIndex(index));
}
else
{
insertOrderedPoint(Tree, CharToInsert, getRightChildIndex(index));
}
// category = balance(Tree, index);
return category;
}
unsigned char insertToTree(string &Tree, char CharToInsert)
{
unsigned char category = 0;
if(Tree.empty())
{
Tree += CharToInsert;
}
else
{
category = insertOrderedPoint(Tree, CharToInsert, 0);
}
return category;
}
//To insert each character in the string one at a time into the heap
unsigned char BuildTree(string word)
{
int category = 0;
string Tree, charList;
for(int i = 0; i < word.length(); i++)
{
if(charList.find(word[i])==string::npos)
{
word[i] = tolower(word[i]);
charList += word[i];
}
}
if(lastWord==charList)
{
return category;
}
for(int i = 0; i < charList.length(); i++)
{
category = insertToTree(Tree, charList[i]);
}
lastWord = charList;
return category;
}
int main()
{
//reading in each word
ifstream dictionary;
string word = "abced";
unsigned char category = BuildTree("abced");
return 0;
}
|