Subject: | Entire branch is deleted instead of single nodes in some cases |
Hello,
in some cases the sub delete() does delete an entire branch instead of a
single node only.
The attached test script yields following output:
7
L ( K ( ) P ( N ( M ( ) O ( ) ) Q ( ) ) )
5
N ( K ( ) P ( O ( ) Q ( ) ) )
While deleting node "L" the node "N" becomes the root and it's left
child "M" gets lost. The size is reduced to 5 nodes instead of 6.
This bug makes the sub delete virtually useless and leads silently to
data losses. Please fix this bug as soon as possible.
Thanks
Karl
Subject: | test-tree.pl |
# Test case : entire branch is deleted instead of single node only
#
# Copyright (C) Karl Kästner - Berlin, Germany
# Sun Apr 5 07:24:11 MSD 2009
#
# 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use Tree::Binary::Search;
my $tree = Tree::Binary::Search->new();
$tree->useStringComparison();
$tree->insert("L", "L");
$tree->insert("K", "K");
$tree->insert("P", "P");
$tree->insert("N", "N");
$tree->insert("M", "M");
$tree->insert("O", "O");
$tree->insert("Q", "Q");
print $tree->size()."\n";
_print(Tree::Binary::Search::getTree($tree));
print "\n";
$tree->delete("L");
print $tree->size()."\n";
_print(Tree::Binary::Search::getTree($tree));
sub _print
{
my $self = shift;
# rekursiv linken und rechten Ast ausgeben
if (defined $self)
{
my $value = $self->getNodeValue();
print $value." ";
print " ( ";
_print($self->getLeft);
_print($self->getRight);
print " ) ";
}
}