开发者

Implementation Tree and other data structure with ruby [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

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 question

I 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜