Arbol

Run Settings
LanguagePerl
Language Version
Run Command
use strict; use feature qw(say); my($root, $n); while ($n++ < 20) { insertar($root, int(rand(1000)))} print "Nodos totales ($n)\n"; print "Preorden: "; pre_orden($root); print "\n"; print "En orden: "; en_orden($root); print "\n"; print "Post orden: "; post_orden($root); print "\n"; for (print "¿Buscar? "; <>; print "¿Buscar? ") { chomp; my $Encontrado = buscar($root, $_); if ($Encontrado) { print "Encontrado $_ en $Encontrado, $Encontrado->{VALOR}\n" } else { print "No está $_ en el árbol\n" } } exit; sub insertar { my($arbol, $valor) = @_; unless ($arbol) { $arbol = {}; $arbol->{VALOR} = $valor; $arbol->{IZQUIERDA} = undef; $arbol->{DERECHA} = undef; $_[0] = $arbol; return; } if ($arbol->{VALOR} > $valor) { insertar($arbol->{IZQUIERDA}, $valor) } elsif ($arbol->{VALOR} < $valor) { insertar($arbol->{DERECHA}, $valor) } else { warn "insertar dup del $valor\n" } } sub en_orden { my($arbol) = @_; return unless $arbol; en_orden($arbol->{IZQUIERDA}); print $arbol->{VALOR}, " "; en_orden($arbol->{DERECHA}); } sub pre_orden { my($arbol) = @_; return unless $arbol; print $arbol->{VALOR}, " "; pre_orden($arbol->{IZQUIERDA}); pre_orden($arbol->{DERECHA}); } sub post_orden { my($arbol) = @_; return unless $arbol; post_orden($arbol->{IZQUIERDA}); post_orden($arbol->{DERECHA}); print $arbol->{VALOR}, " "; } sub buscar { my($arbol, $valor) = @_; return unless $arbol; if ($arbol->{VALOR} == $valor) { return $arbol; } buscar($arbol->{ ($valor < $arbol->{VALOR}) ? "IZQUIERDA" : "DERECHA"}, $valor) }
Editor Settings
Theme
Key bindings
Full width
Lines