-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlphabetNode.cpp
More file actions
107 lines (86 loc) · 2.5 KB
/
Copy pathAlphabetNode.cpp
File metadata and controls
107 lines (86 loc) · 2.5 KB
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
102
103
104
105
106
107
//
// Created by Kyle on 7/27/2020.
//
#include <iostream>
#include "AlphabetNode.h"
using namespace _AlphabetNode;
using namespace std;
// Default constructor
AlphabetNode::AlphabetNode() : letter(0){
count = 0;
subletters = map<char, AlphabetNode*>();
parent = nullptr;
}
// Standard Constructor
AlphabetNode::AlphabetNode(char letter, AlphabetNode* parent) : letter(letter){
this->parent = parent;
count = 0;
subletters = map<char, AlphabetNode*>();
}
//Copy Constructor
AlphabetNode::AlphabetNode(AlphabetNode *pNode) : letter(pNode->getLetter()){
this->count = pNode->count;
this->parent = pNode->parent;
this->subletters = pNode->subletters;
}
AlphabetNode::~AlphabetNode() {
this->parent->delSubletter(this->letter);
}
char AlphabetNode::getLetter() const {
return letter;
}
unsigned int AlphabetNode::getCount() const{
return count;
}
void AlphabetNode::incCount(){
count++;
}
// Decrements the count and deletes the node if count gets to 0 and there are no subletters
void AlphabetNode::decCount() {
if(count > 0){
count--;
return;
}
else if(getSubletters()->empty()){
delete this;
}
}
// Return the pointer to the subletter
AlphabetNode* AlphabetNode::getSubletter(char subletter){
if(subletters.count(subletter) == 0)
return nullptr;
return subletters[subletter];
}
// Creates subletter node if one doesn't already exist, always returns a reference to the requested AlphabetNode. Does not increment count
AlphabetNode *AlphabetNode::addSubletter(char subletter) {
if(subletters.count(subletter) == 0){
subletters[subletter] = new AlphabetNode(subletter, this);
}
return subletters[subletter];
}
void AlphabetNode::delSubletter(char subletter) {
if(subletters.find(subletter) != subletters.end())
subletters.erase(subletter);
// Check if there is a reason to keep this node
if(count > 0)
return;
else if(subletters.empty()){
delete this;
}
}
map<char, AlphabetNode*>* AlphabetNode::getSubletters() {
return &subletters;
}
// Prepends this->letter to the outString
void AlphabetNode::selfToAncestorsString(string* outString) {
if (letter == -1 || letter == 0)
return;
outString->insert(0, 1, letter);
parent->selfToAncestorsString(outString);
}
// Returns the Root-to-Self string, aka a word in correct order
string AlphabetNode::getRootToSelfString(){
string outString;
selfToAncestorsString(&outString);
return outString;
}