From: Martin Date: 2007-04-21T11:28:41+09:00 Subject: Binary Tree Hi all! I'm a newbie in Ruby and I am trying to implement a Binary Tree. However, it doesn't work. Could someone please help me? Thank you ! class Node attr_accessor :left, :right, :key, :data def initialize(key, data) @left = nil @right = nil @key = key @data = data end def to_s() print "Key = ", @key, " Data = ", @data end end class Tree attr_accessor :root public def initialize() @root = nil print "Root Object Id ", @root.object_id, "\n" end def insert(newNode) insertNode(@root, newNode) end def visit() visitNode(@root) end private def insertNode(node, newNode) print "Node Object Id ", node.object_id, "\n" print "New Node Object Id ", newNode.object_id, "\n" if (node == nil) node = newNode print "Node Affected Object Id ", node.object_id, "\n" print @root print node else if (newNode.data <= node.data) insertNode(node.left, newNode) print "Gauche" else insertNode(node.right, newNode) print "Droite" end end end def visitNode(node) if (node != nil) print node.key if (node.left != nil) visitNode(node.left) end if (node.right != nil) visitNode(node.right ) end end end end arbre = Tree.new arbre.insert(Node.new(1, "un")) arbre.insert(Node.new(2, "deux")) arbre.insert(Node.new(3, "trois")) arbre.visit() -- Posted via http://www.ruby-forum.com/.