All pastes #370974 Raw Edit

yure

public text v1 · immutable
#370974 ·published 2007-02-25 00:53 UTC
rendered paste body
#
# This should insert a node with 4 empty children
# however it fails...
#

sub insert {
	my($tree, $value, $children) = @_;
	unless($tree){ #build initial root
		$tree= {}
	}

	my $branch = $tree;
	my $child = $value % $children;

	do{	
		if(!$branch->{VALUE}){
				for my $index (0 .. $children-1) { #build node
					$branch->{$index} = undef;
				}
				$branch->{VALUE} = $value;
				$_[0] = $tree;
				return;
			}	
	}while($branch = $branch->{$child});
}