in reply to Re^7: How am i doing?
in thread How am i doing?

Tree traversal is naturally recursive and easily could be deep enough to hit the default limit.

package Node; sub new { my ($class, $value) = @_; my $self = { value => $value, left => undef, right => undef, }; bless $self, $class; return $self; } # Recursive inorder traversal function sub inorder_traversal { my ($node) = @_; return unless $node; # Base case: return if node is undefined # Traverse the left subtree inorder_traversal($node->{left}); # Visit the root node print $node->{value}, " "; # Traverse the right subtree inorder_traversal($node->{right}); } # Example usage package main; # Create a sample binary tree my $root = Node->new(1); $root->{left} = Node->new(2); $root->{right} = Node->new(3); $root->{left}->{left} = Node->new(4); $root->{left}->{right} = Node->new(5); say "Inorder Traversal: ", inorder_traversal($root);

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^9: How am i doing?
by choroba (Cardinal) on Jul 30, 2025 at 20:00 UTC
    I was going to write "Tree traversal". Now I don't need to. Great example. Of course, it's possible to replace it with a loop, but if you can do it, you already know what recursion is and when it's good to use it.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]