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

所属分类: 脚本专栏 / ruby专题 阅读数: 657
收藏 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]}."

更多精彩内容其他人还在看

Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略

这篇文章主要介绍了Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略,用到了RVM,此种服务器搭建配置极力推荐!需要的朋友可以参考下
收藏 0 赞 0 分享

在Ruby on Rails中使用Markdown的方法

这篇文章主要介绍了在Ruby on Rails中使用Markdown的方法,不过依赖于pygments.rb这个工具,事先得安装Python,需要的朋友可以参考下
收藏 0 赞 0 分享

在博客中屏蔽垃圾留言的简单方法

这篇文章主要介绍了在博客中屏蔽垃圾留言的简单方法,作者以Ruby on Rails搭建的博客应用为例,需要的朋友可以参考下
收藏 0 赞 0 分享

快速安装Ruby on Rails的简明指南

这篇文章主要介绍了快速安装Ruby on Rails的简明指南,Rails是Ruby上人气绝对最高的web开发框架,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby版本管理工具RVM的安装和使用教程

这篇文章主要介绍了Ruby版本管理工具RVM的安装和使用教程,本文示例基于类Unix的系统环境,需要的朋友可以参考下
收藏 0 赞 0 分享

使用rbenv来管理Ruby版本的方法

这篇文章主要介绍了使用rbenv来管理Ruby版本的方法,文中示例基于Mac OS系统进行演示,需要的朋友可以参考下
收藏 0 赞 0 分享

浅析Ruby的源代码布局及其编程风格

这篇文章主要介绍了浅析Ruby的源代码布局及其编程风格,意为给大家推荐一种最为普通的Ruby代码编写风格,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby编程中的语法使用风格推荐

这篇文章主要介绍了Ruby编程中的语法使用风格推荐,好的代码书写风格有助于debug等工作的进行,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby编程中的命名风格指南

这篇文章主要介绍了Ruby编程中的命名风格指南,包括变量和自定义方法等的常用命名格式,需要的朋友可以参考下
收藏 0 赞 0 分享

编写Ruby代码注释时需要注意的一些问题

这篇文章主要介绍了编写Ruby代码注释时需要注意的一些问题,特别是在团队协作时好的注释能大大增加代码的可读性,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多