Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Ruby equivalent of Java's TreeSet<Integer> (self balancing binary tree)?

I want to use a self balancing binary tree to play around with some algorithms, but I'm having difficulty finding the Ruby equivalent of Java's TreeSet (or C#'s SortedSet).

I have found web code like:

https://github.com/nahi/avl_tree#readme

https://github.com/MishaConway/binary_search_tree

http://blog.mikedll.com/2009/09/balanced-avl-binary-search-tree.html

I'd rather rely on something that is in Ruby's Standard Library. Isn't there some class in Ruby's Standard Library for this? I can't seem to find much besides RubyTree, which I don't believe is self-balancing.

(I'll keep Googling til I find it, or someone on this forum points me in the right direction :) )

like image 656
Foolish Chap Avatar asked Nov 02 '25 07:11

Foolish Chap


1 Answers

You can use SortedSet from set as shown below

require 'set'
 s = SortedSet.new([8,2,9,3])
 => #<SortedSet: {2, 3, 8, 9}>

pass argument array as paramter

like image 56
Babasaheb Gosavi Avatar answered Nov 04 '25 04:11

Babasaheb Gosavi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!