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

所属分类: 脚本专栏 / ruby专题 阅读数: 678
收藏 0 赞 0 分享

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

复制代码 代码如下:

#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中用线程实现经典的生产者消费者问题代码实例,本文直接给出实现代码和运行效果,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby中常用的字符串处理函数使用实例

这篇文章主要介绍了Ruby中常用的字符串处理函数使用实例,本文总结了Ruby中最常用的字符串处理函数,如返回字符串的长度、判断字符串中是否包含另一个串、字符串插入、字符串分隔、默认分隔符为空格等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows下ruby语言安装教程

这篇文章主要介绍了Windows下ruby语言安装教程,本文使用rubyinstaller提供的安装包安装,并给出图文说明,非常简单,需要的朋友可以参考下
收藏 0 赞 0 分享

ruby环境中自动编译sass教程

这篇文章主要介绍了ruby环境中自动编译sass教程,本文讲解了ruby环境的安装、sass环境的安装以及sass的常用编译命令使用示例,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby中相等性判断的4种方法

这篇文章主要介绍了Ruby中相等性判断的4种方法,本文讲解了“==” 最常见的相等性判断、“===” 用于 case 语句的相容判断、“equal?” 相同对象判断、“eql?” 对象 hash 值判断等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Rails中使用MySQL分区表一个提升性能的方法

这篇文章主要介绍了Rails中使用MySQL分区表一个提升性能的方法,本文总结出了一个简单的方法实现避免扫描全部的分区表,从而提升性能,需要的朋友可以参考下
收藏 0 赞 0 分享

Rails应用程序中同时修改操作冲突问题的解决方案

这篇文章主要介绍了Rails应用程序中同时修改操作冲突问题的解决方案,本文讲解使用Rails 的 乐观锁解决这个问题并给出了代码救命,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby中的p和puts的使用区别浅析

这篇文章主要介绍了Ruby中的p和puts的使用区别浅析,本文用一个实例讲解了它们之间的区别,并总结出结论,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby中的block、proc、lambda区别总结

这篇文章主要介绍了Ruby中的block、proc、lambda区别总结,本文讲解了yield 和 block call 的区别、block 和 proc、lambda 的区别、proc 和 lambda 的区别,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby的运算符和语句优先级介绍

这篇文章主要介绍了Ruby的运算符和语句优先级介绍,本文先是给出了一些小例子来验证运算符和语句优先级,然后总结出一个优先级表,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多