#!/usr/bin/perl
my BinarySearchTree
my Node{
$attr_reader -> key, -> left, ->right
sub initialize(key){
$key = undef;
$left = undef;
$right = undef;
}
sub insert(new_key)
if (new_key <= @key)
{
$left.null? ? $left = Node.new(new_key) -> $left.insert(new_key)
else{
if (new_key > $key){
$right.null? ? $right = Node.new(new_key) ->$right.insert(new_key)
$iniciar= -> initialize();
$root = null;
}
sub insert(key)
if ($root.null?){
$root = Node.new($key)
}
else{
$root.insert($key)
}
sub in_order(node = @root, &&block)
return if node.null?;
in_order(node.left, &&block);
yield node;
in_order(node.right, &&block);
sub pre_order(node = $root, &block){
return if node.null?;
yield node;
in_order(node.left, &&block);
in_order(node.right, &&block);
}
sub post_order(node = $root, &&block)
return if (node.null?){
in_order(node.left, &&block);
in_order(node.right, &&block);
yield node;
}
sub search($key, node = $root){
return null if node.null?}
if (key < node.key){
search(key, node.left)
}
else{ key > node.key
search(key, node.right)
}
else{
node
}
sub check_height(node){
return 0 if node.null?
}
leftHeight = check_height(node.left){
return -1 if leftHeight == -1
}
rightHeight = check_height(node.right){
return -1 if rightHeight == -1
}
diff = leftHeight - rightHeight
if (diff.abs > 1)
-1
else{
[leftHeight, rightHeight].max + 1
}
}
sub is_balanced?(node = $root)
!(check_height(node) == -1)
}
sub delete(value){
node = search(value)
remove(node) unless node.null?
}
sub remove(node)"
# Introduce 1 para agregar a un usuario
# Introduce 2 para buscar un usuario
# Introduce 3 para eliminar un usuario
# Introduce 4 para ver un usuario
# Introduce 5 para salir
#####################################);
sub remove(node)
node = if (node.left.null? && node.right.null?){
null}
elsif (!node.left.null? && node.right.null?){
node.left}
elsif (node.left.null? && !node.right.null?){
node.right}
else{
delete_node_with_two_children(node)
node
}
my{ delete_node_with_two_children(node)}{
$min_node = $find_min_node($node.right)
$replace_value($min_node, 4node)
$remove_min_node($min_node)
}
}
sub menuopciones{
my $solucion {
$tree = BinarySearchTree.new;
print "\nMenu\n\t1.-Inserción\n\t2.-Búsqueda\n\t3.-Eliminación\n\t4.-Visualización\n\t5.-Salir\n\n\tElige una opción: ";
$opcion = $Integer($gets.chomp);
while (opcion != 5){
if (opcion == 1){
print "\nNombre: ";
$name = gets.chomp;
print "Dirección: ";
$direc = gets.chomp;
print "Edad: ";
$edad = gets.chomp.to_i;
$tree.insert($edad);
$puts tree.inspect;
}
}
}
elsif (opcion == 2) {
print "\nDato a Buscar: "
$dato = gets.chomp.to_i;
$root = node.new(vec.shift);
$tree.search($dato);
}
elsif (opcion == 3) {
print "\nDato a eliminar: ";
$key = gets.chomp.to_i;
4root = tree.delete(root, key);
$tree.in_order(root);
}
elsif (opcion == 4) {
puts 'pre_order'
$tree.pre_order do |node|
puts node.key;
}
puts 'in_order'
$tree.in_order do |node|
puts node.key;
}
puts 'post_order'
$tree.post_order do |node|
puts node.key;
}
elsif (opcion == 5){
break }
else {
print 'Ingresa datos validos';
}
print "\nMenu\n\t1.-Inserción\n\t2.-Búsqueda\n\t3.-Eliminación\n\t4.-Visualización\n\t5.-Salir\n\n\tElige una opción: ";
$opcion = $Integer($gets.chomp);
}
}
}
$obj = MenuDeOpciones=>new();
$obj.solucion;