返回顶部

收藏

to array

更多
class Tree

  attr_accessor :left
  attr_accessor :right
  attr_accessor :data

  def initialize(x=nil)
    @left = nil
    @right = nil
    @data = x
  end

  def insert(x)
    list = []
    if @data == nil
      @data = x
    elsif @left == nil
      @left = Tree.new(x)
    elsif @right == nil
      @right = Tree.new(x)
    else
      list << @left
      list << @right
      loop do
        node = list.shift
        if node.left == nil
          node.insert(x)
          break
        else
          list << node.left
        end
        if node.right == nil
          node.insert(x)
          break
        else
          list << node.right
        end
      end
    end
  end

  def traverse()
    list = []
    yield @data
    list << @left if @left != nil
    list << @right if @right != nil
    loop do
      break if list.empty?
      node = list.shift
      yield node.data
      list << node.left if node.left != nil
      list << node.right if node.right != nil
    end
  end

  def insert(x)
    if @data == nil
      @data = x
    elsif x <= @data
      if @left == nil
        @left = Tree.new x
      else
        @left.insert x
      end
    else
      if @right == nil
        @right = Tree.new x
      else
        @right.insert x
      end
    end
  end

  def inorder()
    @left.inorder {|y| yield y} if @left != nil
    yield @data
    @right.inorder {|y| yield y} if @right != nil
  end

  def preorder()
    yield @data
    @left.preorder {|y| yield y} if @left != nil
    @right.preorder {|y| yield y} if @right != nil
  end

  def postorder()
    @left.postorder {|y| yield y} if @left != nil
    @right.postorder {|y| yield y} if @right != nil
    yield @data
  end

  def search(x)
    if self.data == x
      return self
    elsif x < self.data
      return left != nil ? left.search(x) : nil
    else
      return right != nil ? right.search(x) : nil
    end
  end

  def to_s
    '[' +
    if left then left.to_s + ',' else '' end +
    data.inspect +
    if right then ',' + right.to_s else '' end + ']'
  end

  def to_a
    temp = []
    temp << left.to_a if left
    temp << data
    temp << right.to_a if right
    temp
  end

end

items = %w[bongo grimace monoid jewel plover nexus synergy]

tree = Tree.new
items.each {|x| tree.insert x}

str = tree.to_s
# str is now:
# 'bongo,grimace,jewel,monoid,nexus,plover,synergy'

arr = tree.to_a
# arr is now:
# ['bongo',['grimace',[['jewel'],'monoid',[['nexus'],'plover',
#  ['synergy']]]]]

标签:Ruby,集合

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2015-12-06 21:03:01Set length, size and empty? by LeoSun
  2. 2016-08-28 09:17:16Implementing Enumerable - Write One Method, Get 22 Free by digua
  3. 2016-08-28 12:59:01Divide a set by its element's class by 胡晋
  4. 2016-02-04 11:37:45Collect in place by 永明
  5. 2016-04-24 14:08:00Map elements in a set by digua
  6. 2016-08-28 09:29:05Sort on two arrays by jeffsui
  7. 2016-08-28 13:17:44Convert a hash to set and divide by zetaliang
  8. 2016-09-20 09:21:04Divide a set by absolute value by 好好学习啊
  9. 2016-04-24 14:16:11Create Your stack by Loli控
  10. 2016-08-28 09:48:44extend Forwardable by 好好学习啊
  11. 2016-08-28 13:39:21Add set operation to Array by 永明
相关聚客文章
  1. 博主 发表 2013-06-07 16:00:00 Java笔记:集合框架实现原理
  2. bachue 发表 2013-04-24 07:29:41 Ruby class variable另一奇怪现象的可能的解释
  3. 博主 发表 2013-11-29 00:40:00 《七周七语言》之Ruby
  4. 博主 发表 2013-07-29 03:43:00 Ruby/Rails小技巧收集
  5. wxianfeng 发表 2013-05-12 07:57:15 Rails try method
  6. root 发表 2015-04-19 07:46:01 《JAVA集合框架(JCF)》
  7. 博主 发表 2015-03-22 15:14:00 ruby 中的 4 种相等性判断方法
  8. ziggear 发表 2014-02-03 16:29:45 Programming Is Hard, Let’s Go Scripting… Part.2
  9. Philip Howard 发表 2013-04-28 00:00:00 Ruby with Nginx/Passenger
  10. 有梦最美,希望相随 发表 2013-05-31 07:01:13 七周七语言-Ruby(第二天自习)
  11. 博主 发表 2014-01-08 00:00:00 Ruby on rails
  12. 透明de面具 发表 2013-12-17 21:36:46 Project X吐槽集合

发表评论