-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.pm
75 lines (51 loc) · 1.18 KB
/
BinarySearchTree.pm
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
package BinarySearchTree;
use strict;
use warnings;
use parent 'Displayable';
use Data::Dumper qw(Dumper);
use Carp;
use BinarySearchTree::Node;
use POSIX;
sub new {
my ($class, $sentinel, $min_valid_key) = @_;
my $self = {
root => BinarySearchTree::Node->new($sentinel, undef, DBL_MAX),
min_valid_key => $min_valid_key,
};
bless $self, $class;
return $self;
}
sub add_content {
my ($self, $content) = @_;
my $node = $self->{root}->find_node($content);
confess "found duplicate for $content" if defined $node;
return $self->{root}->add($content);
}
sub remove_content {
my ($self, $content) = @_;
my $node = $self->{root}->find_node($content);
$node->remove();
return;
}
sub remove_node {
my ($node) = @_;
return $node->remove();
}
sub find_content {
my ($self, $key) = @_;
my $node = $self->{root}->find_node($key);
return $node->content() if defined $node;
return;
}
sub nodes_loop {
my ($self, $start_key, $end_key, $routine) = @_;
$start_key = $self->{min_valid_key} unless defined $start_key;
$self->{root}->nodes_loop($start_key, $end_key, $routine);
return;
}
sub save_svg {
my ($self, $filename) = @_;
$self->{root}->save_svg($filename);
return;
}
1;