Sha256: ba703d737101a96bf17a06dc8c6c9858ae3d00159df82732b1d753f0acec4b92

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

class BinaryTrees
  def self.main(args:String[])
    n = 0
    n = Integer.parseInt(args[0]) if args.length > 0

    maxDepth = (6 > n) ? 6 : n
    stretchDepth = maxDepth + 1

    check = TreeNode.bottomUpTree(0, stretchDepth).itemCheck
    puts "stretch tree of depth #{stretchDepth}\t check: #{check}"

    longLivedTree = TreeNode.bottomUpTree 0, maxDepth

    depth = 4
    while depth <= maxDepth
      iterations = 1 << (maxDepth - depth + 4)
      check = 0

      i = 1
      while i <= iterations
        check += TreeNode.bottomUpTree(i, depth).itemCheck
        check += TreeNode.bottomUpTree(-i,depth).itemCheck
        i += 1
      end
      
      puts "#{iterations * 2}\t trees of depth #{depth}\t check: #{check}"
      depth += 2
    end

    puts "long lived tree of depth #{maxDepth}\t check: #{longLivedTree.itemCheck}"
  end
end

class TreeNode
  def initialize(left:TreeNode, right:TreeNode, item:int)
    @item = item
    @left = left
    @right = right
  end

  def initialize(item:int)
    @left = TreeNode(nil)
    @right = TreeNode(nil)
    @item = item
  end

  def self.bottomUpTree(item:int, depth:int)
    if depth > 0
      TreeNode.new(
        TreeNode.bottomUpTree(2*item-1, depth-1),
        TreeNode.bottomUpTree(2*item, depth-1),
        item)
    else
      TreeNode.new(item)
    end
  end

  def itemCheck
    # if necessary deallocate here
    if @left == nil
      @item
    else
      @item + @left.itemCheck - @right.itemCheck
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
mirah-0.0.4-java examples/bintrees.mirah
duby-0.0.3-java examples/bintrees.duby