Ruby实现的最优二叉查找树算法

 更新时间:2015年05月22日 11:14:33   投稿:junjie  
这篇文章主要介绍了Ruby实现的最优二叉查找树算法,本文直接给出实现代码,需要的朋友可以参考下

算法导论上的伪码改写而成,加上导论的课后练习第一题的解的构造函数。

复制代码 代码如下:

#encoding: utf-8
=begin
author: xu jin
date: Nov 11, 2012
Optimal Binary Search Tree
to find by using EditDistance algorithm
refer to <<introduction to algorithms>>
example output:
"k2 is the root of the tree."
"k1 is the left child of k2."
"d0 is the left child of k1."
"d1 is the right child of k1."
"k5 is the right child of k2."
"k4 is the left child of k5."
"k3 is the left child of k4."
"d2 is the left child of k3."
"d3 is the right child of k3."
"d4 is the right child of k4."
"d5 is the right child of k5."

The expected cost is 2.75. 
=end

INFINTIY = 1 / 0.0
a = ['', 'k1', 'k2', 'k3', 'k4', 'k5']
p = [0, 0.15, 0.10, 0.05, 0.10, 0.20]
q = [0.05, 0.10, 0.05, 0.05, 0.05 ,0.10]
e = Array.new(a.size + 1){Array.new(a.size + 1)}
root = Array.new(a.size + 1){Array.new(a.size + 1)}

def optimalBST(p, q, n, e, root)
  w = Array.new(p.size + 1){Array.new(p.size + 1)}
  for i in (1..n + 1)
    e[i][i - 1] = q[i - 1]
    w[i][i - 1] = q[i - 1]
  end
  for l in (1..n)
    for i in (1..n - l + 1)
      j = i + l -1
      e[i][j] = 1 / 0.0
      w[i][j] = w[i][j - 1] + p[j] + q[j]
      for r in (i..j)
        t = e[i][r - 1] + e[r + 1][j] + w[i][j]
        if t < e[i][j]
          e[i][j] = t
          root[i][j] = r
        end
      end
    end
  end
end

def printBST(root, i ,j, signal)
  return if i > j
  if signal == 0
   p "k#{root[i][j]} is the root of the tree."
   signal = 1
  end
  r = root[i][j]
  #left child
  if r - 1< i
    p "d#{r - 1} is the left child of k#{r}."
  else
    p "k#{root[i][r - 1]} is the left child of k#{r}."
    printBST(root, i, r - 1, 1 )
  end
  #right child
  if r >= j
     p "d#{r} is the right child of k#{r}."
  else
    p "k#{root[r + 1][j]} is the right child of k#{r}."
    printBST(root, r + 1, j, 1)
  end
 
end

optimalBST(p, q, p.size - 1, e, root)
printBST(root, 1, a.size-1, 0)
puts "\nThe expected cost is #{e[1][a.size-1]}."

相关文章

  • Ruby是什么以及如何使用

    Ruby是什么以及如何使用

    这篇文章主要介绍了Ruby是什么以及如何使用,Ruby和Ruby on Rails 在早期就出现在Web开发领域了,然而,虽然现在JavaScript和Python都占据了主导地位,Ruby还是仍然占有一席之地
    2017-04-04
  • ruby on rails 代码技巧

    ruby on rails 代码技巧

    对于rails的一些使用技巧的代码
    2009-01-01
  • Ruby简明教程之方法(Method)介绍

    Ruby简明教程之方法(Method)介绍

    这篇文章主要介绍了Ruby简明教程之方法(Method)介绍,ruby的方法分为实例方法、类方法、函数方法等,本文分别做了讲解,需要的朋友可以参考下
    2014-06-06
  • Ruby中proc和lambda的两个区别

    Ruby中proc和lambda的两个区别

    这篇文章主要介绍了Ruby中proc和lambda的两个区别,本文讲解了在proc和lambda中,return关键字有不同含义、检查参数的方式不同两个重要区别,需要的朋友可以参考下
    2015-05-05
  • Ruby中嵌套对象转换成json的方法

    Ruby中嵌套对象转换成json的方法

    这篇文章主要介绍了Ruby中嵌套对象转换成json的方法,同时介绍了普通对象to_json的方法,需要的朋友可以参考下
    2014-06-06
  • Ruby 多线程的潜力和弱点分析

    Ruby 多线程的潜力和弱点分析

    这篇文章主要介绍了Ruby 多线程的潜力和弱点分析,本文讲解了Ruby 多线程和 IO Block、Ruby GIL 的影响、JRuby 去除了 GIL、Ruby 多线程总结等内容,需要的朋友可以参考下
    2015-03-03
  • 实例解析Ruby设计模式编程中Strategy策略模式的使用

    实例解析Ruby设计模式编程中Strategy策略模式的使用

    这篇文章主要介绍了Ruby设计模式编程中Strategy策略模式的使用实例,Strategy模式在Ruby on Rails框架开发中也经常用到,需要的朋友可以参考下
    2016-03-03
  • ruby 简单例子

    ruby 简单例子

    ruby 简单例子...
    2007-11-11
  • 深入理解Ruby中的block概念

    深入理解Ruby中的block概念

    这篇文章主要介绍了深入理解Ruby中的block概念,文中给出了Javascript代码块与Ruby代码块的对比,需要的朋友可以参考下
    2015-04-04
  • Ruby 迭代器知识汇总

    Ruby 迭代器知识汇总

    这篇文章主要介绍了Ruby 迭代器的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
    2020-06-06

最新评论