-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.cpp
150 lines (130 loc) · 3.77 KB
/
bst.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "bst.h"
#include <QDebug>
#include <stack>
BSTNode::BSTNode(QString word, FileWordNode *pos_in_file)
{
this->word = word;
this->word_head = pos_in_file;
this->word_last = pos_in_file;
this->leftChild = NULL;
this->rightChild = NULL;
}
BSTNode::BSTNode()
{
}
BST::BST()
{
this->root = NULL;
}
void BST::add(QString word, FileWordNode *pos_in_file)
{
BSTNode *newNode = new BSTNode;
newNode->leftChild = NULL;
newNode->rightChild = NULL;
newNode->word = word;
if(this->root)
{
BSTNode *pointer = this->root;
BSTNode *parent = NULL;
while(pointer)
{
parent = pointer;
if(QString::compare(pointer->word, word, Qt::CaseInsensitive) > 0)
{
pointer = pointer->rightChild;
}
else if(QString::compare(pointer->word, word, Qt::CaseInsensitive) < 0)
{
pointer = pointer->leftChild;
}
else // Two nodes have equal vaules
{
pointer->word_last->next_equal = pos_in_file;
pos_in_file->last_equal = pointer->word_last;
pointer->word_last = pos_in_file;
pos_in_file->next_equal = NULL;
break;
}
}
if(QString::compare(parent->word, word, Qt::CaseInsensitive) < 0)
{
parent->leftChild = newNode;
parent->leftChild->word_head = pos_in_file;
parent->leftChild->word_last = pos_in_file;
}
else if(QString::compare(parent->word, word, Qt::CaseInsensitive) > 0)
{
parent->rightChild = newNode;
parent->rightChild->word_head = pos_in_file;
parent->rightChild->word_last = pos_in_file;
}
}
else
{
this->root = new BSTNode(word,pos_in_file);
}
}
FileWordPositionWrapper BST::search(QString word)
{
BSTNode *pointer = this->root;
while(pointer)
{
if(QString::compare(pointer->word,word, Qt::CaseInsensitive) == 0)
{
qDebug() << pointer->word;
qDebug() << pointer->word_head->last->word;
qDebug() << pointer->word_last->word;
if(pointer->word_last->last_equal != NULL)
qDebug() << "Thiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii sis the buh";
return FileWordPositionWrapper(pointer->word_head, pointer->word_last);
}
else if(QString::compare(pointer->word,word, Qt::CaseInsensitive) < 0)
{
pointer = pointer->leftChild;
}
else if(QString::compare(pointer->word,word, Qt::CaseInsensitive) > 0)
{
pointer = pointer->rightChild;
}
}
return FileWordPositionWrapper(NULL, NULL);
}
QStringList BST::listAllWords()
{
QStringList wordList;
listAllWordsInOrderWithFileName(this->root,wordList);
return wordList;
}
void BST::listAllWordsInOrderWithFileName(BSTNode *root, QStringList &list)
{
// if(root = NULL)
// return;
// listAllWordsInOrderWithFileName(root->leftChild, list);
// list.append(root->word);
// listAllWordsInOrderWithFileName(root->rightChild, list);
std::stack<BSTNode *> nodeStack;
BSTNode *node = this->root;
while(true)
{
if(node)
{
nodeStack.push(node);
node = node->leftChild;
}
else
{
if (!nodeStack.empty())
{
node = nodeStack.top();
list.append(node->word);
nodeStack.pop();
node = node->rightChild;
}
else
{
// Stack is empty and there is no pointer to travers so it's done
break;
}
}
}
}