Implementation Tree and other data structure with ruby [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
开发者_JAVA百科 Improve this questionI am new Ruby programmar. when I was in C++ I could implement data structures with pointer but now in Ruby I don't know how can I implement these data structures ( for example tree ). Can someone help me ( for example introduce me a good reference or take a good example ). Special thanks.
Ruby doesn't have nor need pointers since most things are simply passed by reference.
> a = "hello"
> b = a
> a.object_id == b.object_id
=> true
In its simplest form a tree node could just be a struct, with a parent and a left and right sibling:
> Node = Struct.new(:parent, :left, :right)
> root = Node.new
> n1 = Node.new(root, "hello", "world")
> root.left = n1
...
> root.left.left
=> "hello"
> root.left.right
=> "world"
For more complete implementations you could look at for example:
RubyTree:
http://rubytree.rubyforge.org/rdoc/
SimpleTree:
https://github.com/ealdent/simple-tree/blob/master/lib/simple_tree.rb
精彩评论