tests for non-recursive binary tree traversal in perl6
perl
last edit: Jan, 31st 2012 | jump to bottom
# written for CSC206 by Jonathan Buhacoff on 31 Jan 2012 use Algorithm::BinaryTree; sub node($value,$left?,$right?) { } # $root is the root node if a binary tree # $resultStr is the order the nodes are expected, separated by spaces. Ex: "D E B F G C A" sub test($root,$resultStr) { my @result = (); my @resultNR = (); my $tree = BinaryTreeWalker.new; my $treeNR = NonRecursiveBinaryTreeWalker.new; say "ok: $resultStr" if "{@result}" eq "{@resultNR}" eq $resultStr; } my $balancedTree3 = node("A", node("B", node("D"), node("E") ), node("C", node("F"), node("G") ) ); test($balancedTree3,"D E B F G C A"); test(Nil, ""); my $singleNode = node("A"); test($singleNode, "A"); my $singleLeftLeaf = node("A",node("B")); test($singleLeftLeaf, "B A"); my $singleRightLeaf = node("A",Nil,node("C")); test($singleRightLeaf, "C A"); my $balancedHeight2 = node("A",node("B"),node("C")); test($balancedHeight2, "C B A"); my $longLeftOnlyBranch = node("A",node("B",node("C",node("D",node("E"))))); test($longLeftOnlyBranch, "E D C B A"); my $longRightOnlyBranch = node("A",Nil,node("B",Nil,node("C",Nil,node("D",Nil,node("E"))))); test($longRightOnlyBranch, "E D C B A"); my $slightlyRightThenLeftBranch = node("A",Nil,node("B",node("C",node("D",node("E"))))); test($slightlyRightThenLeftBranch, "E D C B A"); my $slightlyLeftThenRightBranch = node("A",node("B",Nil,node("C",Nil,node("D",Nil,node("E"))))); test($slightlyLeftThenRightBranch, "E D C B A"); my $waveBranch = node("A",node("B",Nil,node("C",node("D",Nil,node("E"))))); test($waveBranch, "E D C B A");
24 views




