blib/lib/Btrees.pm

filter: Statementstmt 84.9% 96 / 113 Branchbran 84.0% 42 / 50 Conditioncond 71.1% 32 / 45 MC/DCmcdc 45.4% 10 / 22 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 13 totaltotal 74.6% 191 / 256 CC 43 SCAR 18.6
CC
43
Cov
82%
List
12.0
swing_left
8.1
swing_right
8.1
CRAP 6.4
↑ Summary
1  
2 package Btrees;
3 1 $VERSION=1.00;
4  
5 1 require 5.000;
6 1 require Exporter;
7  
8 =head1 NAME
9  
10 Btrees - Binary trees using the AVL balancing method.
11  
12 =head1 SYNOPSIS
13  
14 # yes, do USE the package ...
15 use Btrees;
16  
17 # no constructors
18  
19 # traverse a tree and invoke a function
20 traverse( $tree, $func );
21  
22 # find a node in a balanced tree
23 $node = bal_tree_find( $tree, $val $cmp );
24  
25 # add a node in a balanced tree, rebalancing if required
26 ($tree, $node) = bal_tree_add( $tree, $val, $cmp )
27  
28 # delete a node in a balanced tree, rebalancing if required
29 ($tree, $node) = bal_tree_del( $tree, $val , $cmp )
30  
31 =head1 DESCRIPTION
32  
33 Btrees uses the AVL balancing method, by G. M. Adelson-Velskii
34 and E.M. Landis. Bit scavenging, as done in low level languages like
35 C, is not used for height balancing since this is too expensive for
36 an interpreter. Instead the actual height of each subtree is stored
37 at each node. A null pointer has a height of zero. A leaf a height of
38 1. A nonleaf a height of 1 greater than the height of its two children.
39  
40 =head1 AUTHOR
41  
42 Ron Squiers (ron@broadcom.com). Adapted from "Mastering Algorithms with
43 Perl" by Jon Orwant, Jarkko Hietaniemi & John Macdonald. Copyright
44 1999 O'Reilly and Associates, Inc. All right reserved. ISBN: 1-56592-398-7
45  
46 =cut
47  
48 1 @ISA = qw(Exporter);
49 1 @EXPORT = qw( traverse bal_tree_find bal_tree_add bal_tree_del list );
50  
51 #########################################
52 #
53 # Method: list
54 #
55 # List $tree in order in turn
56 #
57 # list( $tree );
58 #
59 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod undocumented
60 0 ▶ my $tree = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $tree = shift() or return undef 0 0 0
61  
62 0 local $max = $tree->{height};
63 0 ▶ sub List {
Subroutine List: not called CC=3 SCAR=24.8
Pod undocumented
64 0 my $tree = shift;
65  
66 0 ▶ my $height = $tree->{height} || $max;
Condition0%0/3
$tree->{'height'} || $max
l!l&&r!l&&!r
000
MC/DC0%0/2
$tree->{'height'} || $max
$tree->{'height'} $max
Truth table0%0/3
A || B
A= $tree->{'height'}B= $max
ABresult
1X1
011
000
67 0 ▶ while( $max - $height ) { print " "; $height++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($max - $height) 0 0 0
68 0 printf("0x%x\n", $tree->{val});
69 }
70 0 my $func = \&List;
71 0 traverse( $tree, $func );
72 }
73  
74 #########################################
75 #
76 # Method: traverse
77 #
78 # Traverse $tree in order, calling $func() for each element.
79 # in turn
80 # traverse( $tree, $func );
81 #
82 165 ▶sub traverse {
Subroutine traverse: called CC=2 SCAR=6.9
Pod undocumented
83 165 ▶ my $tree = shift or return; # skip undef pointers
Branch: 1 branch
BranchTrueFalseTotal
my $tree = shift() or return 79 86 165
84 79 my $func = shift;
85  
86 79 traverse( $tree->{left}, $func );
87 79 &$func( $tree );
88 79 traverse( $tree->{right}, $func );
89 }
90  
91 #########################################
92 #
93 # Method: bal_tree_find
94 #
95 # Traverse $tree in order, calling $func() for each element.
96 # in turn
97 # $node = bal_tree_find( $tree, $val[, $cmp ] );
98 #
99 109 ▶sub bal_tree_find {
Subroutine bal_tree_find: called CC=5 SCAR=16.1
Pod undocumented
100 109 my( $tree, $val, $cmp) = @_;
101 109 my $result;
102  
103 109 ▶ while ( $tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree) 328 50 378
104 my $relation = defined $cmp
105 ? $cmp->( $val, $tree->{val} )
106 328 ▶ : $val <=> $tree->{val};
Branch: 1 branch
BranchTrueFalseTotal
defined $cmp ? : 322 6 328
107  
108 ### Stop when the desired node if found.
109 328 ▶ return $tree if $relation == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $relation == 0 59 269 328
110  
111 ### Go down the correct subtree.
112 269 ▶ $tree = $relation < 0 ? $tree->{left} : $tree->{right};
Branch: 1 branch
BranchTrueFalseTotal
$relation < 0 ? : 141 128 269
113 }
114  
115 ### The desired node doesn't exist.
116 50 return undef;
117 }
118  
119 #########################################
120 #
121 # Method: bal_tree_add
122 #
123 # Search $tree looking for a node that has the value $val,
124 # add it if it does not already exist.
125 # If provided, $cmp compares values instead of <=>.
126 #
127 # ($tree, $node) = bal_tree_add( $tree, $val, $cmp )
128 # the return values:
129 # $tree points to the (possible new or changed) subtree that
130 # has resulted from the add operation.
131 # $node points to the (possibly new) node that contains $val
132 #
133 377 ▶sub bal_tree_add {
Subroutine bal_tree_add: called CC=5 SCAR=16.1
Pod undocumented
134 377 my( $tree, $val, $cmp) = @_;
135 377 my $result;
136  
137 377 ▶ unless ( $tree ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree) 98 279 377
138 98 $result = {
139 left => undef,
140 right => undef,
141 val => $val,
142 height => 1
143 };
144 98 return( $result, $result );
145 }
146  
147 my $relation = defined $cmp
148 ? $cmp->( $val, $tree->{val} )
149 279 ▶ : $val <=> $tree->{val};
Branch: 1 branch
BranchTrueFalseTotal
defined $cmp ? : 174 105 279
150  
151 ### Stop when the desired node if found.
152 279 ▶ return ( $tree, $tree ) if $relation == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $relation == 0 1 278 279
153  
154 ### Add to the correct subtree.
155 278 ▶ if( $relation < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relation < 0) { } 82 196 278
156 ($tree->{left}, $result) =
157 82 bal_tree_add ( $tree->{left}, $val, $cmp );
158 } else {
159 ($tree->{right}, $result) =
160 196 bal_tree_add ( $tree->{right}, $val, $cmp );
161 }
162  
163 ### Make sure that this level is balanced, return the
164 ### (possibly changed) top and the (possibly new) selected node.
165 278 return ( balance_tree( $tree ), $result );
166 }
167  
168 #########################################
169 #
170 # Method: bal_tree_del
171 #
172 # Search $tree looking for a node that has the value $val,
173 # and delete it if it does not already exist.
174 # If provided, $cmp compares values instead of <=>.
175 #
176 # ($tree, $node) = bal_tree_del( $tree, $val , $cmp )
177 #
178 # the return values:
179 # $tree points to the (possible empty or changed) subtree that
180 # has resulted from the delete operation.
181 # if found, $node points to the node that contains $val
182 # if not found, $node is undef
183 #
184 39 ▶sub bal_tree_del {
Subroutine bal_tree_del: called CC=6 SCAR=17.9
Pod undocumented
185 # An empty (sub)tree does not contain the target.
186 39 ▶ my $tree = shift or return (undef,undef);
Branch: 1 branch
BranchTrueFalseTotal
my $tree = shift() or return undef, undef 38 1 39
187  
188 38 my ($val, $cmp) = @_;
189 38 my $node;
190  
191 my $relation = defined $cmp
192 ? $cmp->( $val, $tree->{val} )
193 38 ▶ : $val <=> $tree->{val};
Branch: 1 branch
BranchTrueFalseTotal
defined $cmp ? : 0 38 38
194  
195 38 ▶ if( $relation != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relation != 0) { } 21 17 38
196 ### Not this node, go down the tree.
197 21 ▶ if( $relation < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relation < 0) { } 17 4 21
198 ($tree->{left}, $node) =
199 17 bal_tree_del ( $tree->{left}, $val, $cmp );
200 } else {
201 ($tree->{right}, $node) =
202 4 bal_tree_del ( $tree->{right}, $val, $cmp );
203 }
204  
205 ### No balancing required if it wasn't found.
206 21 ▶ return ( $tree, undef ) unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 3 18 21
207 } else {
208 # Must delete this node. Remember it to return it,
209 17 $node = $tree;
210  
211 # but splice the rest of the tree back together first
212 17 $tree = bal_tree_join( $tree->{left}, $tree->{right} );
213  
214 # and make the deleted node forget its children (precaution
215 # in case the caller tries to use the node).
216 17 $node->{left} = $node->{right} = undef;
217 }
218  
219 ### Make sure that this level is balanced, return the
220 ### (possibly undef) selected node.
221 35 return ( balance_tree($tree), $node );
222 }
223  
224 #########################################
225 #
226 # Method: bal_tree_join
227 #
228 # Join two trees together into a single tree
229 #
230 # the return values:
231 # $tree points to the joined subtrees that has resulted from
232 # the join operation.
233 #
234 17 ▶sub bal_tree_join {
Subroutine bal_tree_join: called CC=4 SCAR=20.7
Pod undocumented
235 17 my ($l, $r) = @_;
236  
237 ### Simple case - onr or both is null.
238 17 ▶ return $l unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 10 7 17
239 7 ▶ return $r unless defined $l;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l 7 0 7
240  
241 ### Nope - we've got two real trees to merge here.
242 0 my $top;
243  
244 0 ▶ if ( $l->{height} > $r->{height} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->{'height'} > $r->{'height'}) { } 0 0 0
245 0 $top = $l;
246 0 $top->{right} = bal_tree_join( $top->{right}, $r );
247 } else {
248 0 $top = $r;
249 0 $top->{left} = bal_tree_join( $l, $top->{left} );
250 }
251 0 return balance_tree( $top );
252 }
253  
254 #########################################
255 #
256 # Method: balance_tree
257 #
258 # Balance a potentially out of balance tree
259 #
260 # the return values:
261 # $tree points to the balanced tree root
262 #
263 313 ▶sub balance_tree {
Subroutine balance_tree: called CC=6 SCAR=18.0
Pod undocumented
264 ### An empty tree is balanced already.
265 313 ▶ my $tree = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $tree = shift() or return undef 303 10 313
266  
267 ### An empty link is height 0.
268 303 ▶ my $lh = defined $tree->{left} && $tree->{left}{height};
Condition66%2/3
defined $tree->{'left'} && $tree->{'left'}{'height'}
!ll&&!rl&&r
1070196
MC/DC50%1/2
defined $tree->{'left'} && $tree->{'left'}{'height'}
defined $tree->{'left'} $tree->{'left'}{'height'}
Truth table66%2/3
A && B
A= defined $tree->{'left'}B= $tree->{'left'}{'height'}
ABresult
0X0
100
111
269 303 ▶ my $rh = defined $tree->{right} && $tree->{right}{height};
Condition66%2/3
defined $tree->{'right'} && $tree->{'right'}{'height'}
!ll&&!rl&&r
270276
MC/DC50%1/2
defined $tree->{'right'} && $tree->{'right'}{'height'}
defined $tree->{'right'} $tree->{'right'}{'height'}
Truth table66%2/3
A && B
A= defined $tree->{'right'}B= $tree->{'right'}{'height'}
ABresult
0X0
100
111
270  
271 ### Rebalance if needed, return the (possibly changed) root.
272 303 ▶ if ( $lh > 1+$rh ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lh > 1 + $rh) { } 6 297 303
elsif ($lh + 1 < $rh) { } 39 258 297
273 6 return swing_right( $tree );
274 } elsif ( $lh+1 < $rh ) {
275 39 return swing_left( $tree );
276 } else {
277 ### Tree is either perfectly balanced or off by one.
278 ### Just fix its height.
279 258 set_height( $tree );
280 258 return $tree;
281 }
282 }
283  
284 #########################################
285 #
286 # Method: set_height
287 #
288 # Set height of a node
289 #
290 366 ▶sub set_height {
Subroutine set_height: called CC=4 SCAR=14.0
Pod undocumented
291 366 my $tree = shift;
292  
293 366 my $p;
294 ### get heights, an undef node is height 0.
295 366 ▶ my $lh = defined ( $p = $tree->{left} ) && $p->{height};
Condition66%2/3
defined($p = $tree->{'left'}) && $p->{'height'}
!ll&&!rl&&r
1190247
MC/DC50%1/2
defined($p = $tree->{'left'}) && $p->{'height'}
defined($p = $tree->{'left'}) $p->{'height'}
Truth table66%2/3
A && B
A= defined($p = $tree->{'left'})B= $p->{'height'}
ABresult
0X0
100
111
296 366 ▶ my $rh = defined ( $p = $tree->{right} ) && $p->{height};
Condition66%2/3
defined($p = $tree->{'right'}) && $p->{'height'}
!ll&&!rl&&r
640302
MC/DC50%1/2
defined($p = $tree->{'right'}) && $p->{'height'}
defined($p = $tree->{'right'}) $p->{'height'}
Truth table66%2/3
A && B
A= defined($p = $tree->{'right'})B= $p->{'height'}
ABresult
0X0
100
111
297 366 ▶ $tree->{height} = $lh < $rh ? $rh+1 : $lh+1;
Branch: 1 branch
BranchTrueFalseTotal
$lh < $rh ? : 143 223 366
298 }
299  
300 #########################################
301 #
302 # Method: $tree = swing_left( $tree )
303 #
304 # Change t to r or rl
305 # / \ / \ / \
306 # l r t rr t r
307 # / \ / \ / \ / \
308 # rl rr l rl l rll rlr rr
309 # / \ / \
310 # rll rlr rll rlr
311 #
312 # t and r must both exist.
313 # The second form is used if height of rl is greater than height of rr
314 # (since the form would then lead to the height of t at least 2 more
315 # than the height of rr).
316 #
317 # changing to the second form is done in two steps, with first a move_right(r)
318 # and then a move_left(t), so it goes:
319 #
320 # Change t to t and then to rl
321 # / \ / \ / \
322 # l r l rl t r
323 # / \ / \ / \ / \
324 # rl rr rll r l rll rlr rr
325 # / \ / \
326 # rll rlr rlr rr
327 #
328 39 ▶sub swing_left {
Subroutine swing_left: called CC=8 SCAR=20.9
Pod undocumented
329 39 my $tree = shift;
330  
331 39 my $r = $tree->{right}; # must exist
332 39 my $rl = $r->{left}; # might exist
333 39 my $rr = $r->{right}; # might exist
334 39 my $l = $tree->{left}; # might exist
335  
336 ### get heights, an undef node has height 0
337 39 ▶ my $lh = $l && $l->{height} || 0;
Condition80%4/5
$l && $l->{'height'} || 0
l!l
930
$l and $l->{"height"}
!ll&&!rl&&r
3009
MC/DC50%1/2
$l && $l->{'height'} || 0
$l $l->{"height"}
Truth table66%2/3
A and B
A= $lB= $l->{"height"}
ABresult
111
0X0
100
338 39 ▶ my $rlh = $rl && $rl->{height} || 0;
Condition80%4/5
$rl && $rl->{'height'} || 0
l!l
1326
$rl and $rl->{"height"}
!ll&&!rl&&r
26013
MC/DC50%1/2
$rl && $rl->{'height'} || 0
$rl $rl->{"height"}
Truth table66%2/3
A and B
A= $rlB= $rl->{"height"}
ABresult
111
0X0
100
339 39 ▶ my $rrh = $rr && $rr->{height} || 0;
Condition80%4/5
$rr && $rr->{'height'} || 0
l!l
363
$rr and $rr->{"height"}
!ll&&!rl&&r
3036
MC/DC50%1/2
$rr && $rr->{'height'} || 0
$rr $rr->{"height"}
Truth table66%2/3
A and B
A= $rrB= $rr->{"height"}
ABresult
111
0X0
100
340  
341 39 ▶ if ( $rlh > $rrh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rlh > $rrh) 4 35 39
342 4 $tree->{right} = move_right( $r );
343 }
344  
345 39 return move_left( $tree );
346 }
347  
348 # and the opposite swing
349  
350 6 ▶sub swing_right {
Subroutine swing_right: called CC=8 SCAR=20.9
Pod undocumented
351 6 my $tree = shift;
352  
353 6 my $l = $tree->{left}; # must exist
354 6 my $lr = $l->{right}; # might exist
355 6 my $ll = $l->{left}; # might exist
356 6 my $r = $tree->{right}; # might exist
357  
358 ### get heights, an undef node has height 0
359 6 ▶ my $rh = $r && $r->{height} || 0;
Condition80%4/5
$r && $r->{'height'} || 0
l!l
33
$r and $r->{"height"}
!ll&&!rl&&r
303
MC/DC50%1/2
$r && $r->{'height'} || 0
$r $r->{"height"}
Truth table66%2/3
A and B
A= $rB= $r->{"height"}
ABresult
111
0X0
100
360 6 ▶ my $lrh = $lr && $lr->{height} || 0;
Condition80%4/5
$lr && $lr->{'height'} || 0
l!l
51
$lr and $lr->{"height"}
!ll&&!rl&&r
105
MC/DC50%1/2
$lr && $lr->{'height'} || 0
$lr $lr->{"height"}
Truth table66%2/3
A and B
A= $lrB= $lr->{"height"}
ABresult
111
0X0
100
361 6 ▶ my $llh = $ll && $ll->{height} || 0;
Condition80%4/5
$ll && $ll->{'height'} || 0
l!l
42
$ll and $ll->{"height"}
!ll&&!rl&&r
204
MC/DC50%1/2
$ll && $ll->{'height'} || 0
$ll $ll->{"height"}
Truth table66%2/3
A and B
A= $llB= $ll->{"height"}
ABresult
111
0X0
100
362  
363 6 ▶ if ( $lrh > $llh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lrh > $llh) 5 1 6
364 5 $tree->{left} = move_left( $l );
365 }
366  
367 6 return move_right( $tree );
368 }
369  
370 #########################################
371 #
372 # Method: $tree = move_left( $tree )
373 #
374 # Change t to r
375 # / \ / \
376 # l r t rr
377 # / \ / \
378 # rl rr l rl
379 #
380 # caller has determined that t and r both exist
381 # (l can be undef, so can one of rl and rr)
382 #
383 44 ▶sub move_left {
Subroutine move_left: called CC=1 SCAR=0.0
Pod undocumented
384 44 my $tree = shift;
385 44 my $r = $tree->{right};
386 44 my $rl = $r->{left};
387  
388 44 $tree->{right} = $rl;
389 44 $r->{left} = $tree;
390 44 set_height( $tree );
391 44 set_height( $r );
392 44 return $r;
393 }
394  
395 #########################################
396 #
397 # Method: $tree = move_right( $tree )
398 #
399 # Change t to l
400 # / \ / \
401 # l r ll t
402 # / \ / \
403 # ll lr lr r
404 #
405 # caller has determined that t and l both exist
406 # (r can be undef, so can one of ll and lr)
407 #
408 10 ▶sub move_right {
Subroutine move_right: called CC=1 SCAR=0.0
Pod undocumented
409 10 my $tree = shift;
410 10 my $l = $tree->{left};
411 10 my $lr = $l->{right};
412  
413 10 $tree->{left} = $lr;
414 10 $l->{right} = $tree;
415 10 set_height( $tree );
416 10 set_height( $l );
417 10 return $l;
418 }
419  
420 #########################################
421 # That's all folks ...
422 #########################################
423 #
424 1 1; # so that use() returns true
425  
↑ Summary