Embedding tree and other ruby ​​data structure

I am the new Ruby programmar. when I was in C ++, I could implement data structures with a pointer, but now in Ruby I don’t know how to implement these data structures (like a tree). Can someone help me (for example, introduce me to a good link or take a good example). Special thanks to.

+7
source share
1 answer

Ruby does not have and does not require 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 node tree can be just a structure, with a parent and left and right siblings:

 > 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 can see, for example:

RubyTree:
http://rubytree.rubyforge.org/rdoc/

SimpleTree:
https://github.com/ealdent/simple-tree/blob/master/lib/simple_tree.rb

+10
source

All Articles