|
|
|
/***************************************************************************
|
|
|
|
begin : Wed Jan 29 2003
|
|
|
|
copyright : (C) 2003 - 2004 by Scott Wheeler
|
|
|
|
email : wheeler@kde.org
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <kdebug.h>
|
|
|
|
|
|
|
|
#include "sortedstringlist.h"
|
|
|
|
|
|
|
|
class SortedStringList::Node
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Node(const TQString &value) : key(value), tqparent(0), left(0), right(0) {}
|
|
|
|
~Node() {}
|
|
|
|
|
|
|
|
TQString key;
|
|
|
|
Node *tqparent;
|
|
|
|
Node *left;
|
|
|
|
Node *right;
|
|
|
|
};
|
|
|
|
|
|
|
|
SortedStringList::SortedStringList() : m_root(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SortedStringList::~SortedStringList()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SortedStringList::insert(const TQString &value)
|
|
|
|
{
|
|
|
|
return BSTInsert(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SortedStringList::tqcontains(const TQString &value) const
|
|
|
|
{
|
|
|
|
return tqfind(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
SortedStringList::Node *SortedStringList::treeMinimum(Node *n) const
|
|
|
|
{
|
|
|
|
while(n->left)
|
|
|
|
n = n->left;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
SortedStringList::Node *SortedStringList::treeSuccessor(Node *n) const
|
|
|
|
{
|
|
|
|
if(n->right)
|
|
|
|
return treeMinimum(n->right);
|
|
|
|
|
|
|
|
Node *p = n->tqparent;
|
|
|
|
|
|
|
|
while(p && n == p->right) {
|
|
|
|
n = p;
|
|
|
|
p = p->tqparent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SortedStringList::remove(const TQString &value)
|
|
|
|
{
|
|
|
|
Node *n = tqfind(value);
|
|
|
|
|
|
|
|
if(!n)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Node *y;
|
|
|
|
Node *x;
|
|
|
|
|
|
|
|
if(!n->left || !n->right)
|
|
|
|
y = n;
|
|
|
|
else
|
|
|
|
y = treeSuccessor(n);
|
|
|
|
|
|
|
|
if(y->left)
|
|
|
|
x = y->left;
|
|
|
|
else
|
|
|
|
x = y->right;
|
|
|
|
|
|
|
|
if(x)
|
|
|
|
x->tqparent = y->tqparent;
|
|
|
|
|
|
|
|
if(!y->tqparent)
|
|
|
|
m_root = x;
|
|
|
|
else {
|
|
|
|
if(y == y->tqparent->left)
|
|
|
|
y->tqparent->left = x;
|
|
|
|
else
|
|
|
|
y->tqparent->right = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(y != x)
|
|
|
|
n->key = y->key;
|
|
|
|
|
|
|
|
delete y;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TQStringList SortedStringList::values() const
|
|
|
|
{
|
|
|
|
TQStringList l;
|
|
|
|
traverse(m_root, l);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// private methods
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
SortedStringList::Node *SortedStringList::tqfind(const TQString &value) const
|
|
|
|
{
|
|
|
|
Node *n = m_root;
|
|
|
|
while(n && value != n->key) {
|
|
|
|
if(value < n->key)
|
|
|
|
n = n->left;
|
|
|
|
else
|
|
|
|
n = n->right;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SortedStringList::BSTInsert(const TQString &value)
|
|
|
|
{
|
|
|
|
Node *previousNode = 0;
|
|
|
|
Node *node = m_root;
|
|
|
|
|
|
|
|
while(node) {
|
|
|
|
previousNode = node;
|
|
|
|
if(value == node->key)
|
|
|
|
return true;
|
|
|
|
else if(value < node->key)
|
|
|
|
node = node->left;
|
|
|
|
else
|
|
|
|
node = node->right;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(previousNode && value == previousNode->key)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Node *n = new Node(value);
|
|
|
|
|
|
|
|
n->tqparent = previousNode;
|
|
|
|
|
|
|
|
if(!m_root)
|
|
|
|
m_root = n;
|
|
|
|
else {
|
|
|
|
if(value < previousNode->key)
|
|
|
|
previousNode->left = n;
|
|
|
|
else
|
|
|
|
previousNode->right = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SortedStringList::traverse(const Node *n, TQStringList &list) const
|
|
|
|
{
|
|
|
|
if(!n)
|
|
|
|
return;
|
|
|
|
|
|
|
|
traverse(n->left, list);
|
|
|
|
list.append(n->key);
|
|
|
|
traverse(n->right, list);
|
|
|
|
}
|