1
/***************************************************************************
2
begin : Wed Jan 29 2003
3
copyright : (C) 2003 - 2004 by Scott Wheeler
4
email : wheeler@kde.org
5
***************************************************************************/
7
/***************************************************************************
9
* This program is free software; you can redistribute it and/or modify *
10
* it under the terms of the GNU General Public License as published by *
11
* the Free Software Foundation; either version 2 of the License, or *
12
* (at your option) any later version. *
14
***************************************************************************/
18
#include "sortedstringlist.h"
20
class SortedStringList::Node
23
Node(const QString &value) : key(value), parent(0), left(0), right(0) {}
32
SortedStringList::SortedStringList() : m_root(0)
37
SortedStringList::~SortedStringList()
42
bool SortedStringList::insert(const QString &value)
44
return BSTInsert(value);
47
bool SortedStringList::contains(const QString &value) const
52
SortedStringList::Node *SortedStringList::treeMinimum(Node *n) const
59
SortedStringList::Node *SortedStringList::treeSuccessor(Node *n) const
62
return treeMinimum(n->right);
66
while(p && n == p->right) {
74
bool SortedStringList::remove(const QString &value)
76
Node *n = find(value);
84
if(!n->left || !n->right)
95
x->parent = y->parent;
100
if(y == y->parent->left)
103
y->parent->right = x;
114
QStringList SortedStringList::values() const
121
////////////////////////////////////////////////////////////////////////////////
123
////////////////////////////////////////////////////////////////////////////////
125
SortedStringList::Node *SortedStringList::find(const QString &value) const
128
while(n && value != n->key) {
138
bool SortedStringList::BSTInsert(const QString &value)
140
Node *previousNode = 0;
145
if(value == node->key)
147
else if(value < node->key)
153
if(previousNode && value == previousNode->key)
156
Node *n = new Node(value);
158
n->parent = previousNode;
163
if(value < previousNode->key)
164
previousNode->left = n;
166
previousNode->right = n;
172
void SortedStringList::traverse(const Node *n, QStringList &list) const
177
traverse(n->left, list);
179
traverse(n->right, list);