• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > Ruby实现的最优二叉查找树算法

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

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了ruby,ruby rose,ruby什么意思,max and ruby,ruby语言等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

算法导论上的伪码改写而成,加上导论的课后练习第一题的解的构造函数。
#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]}."

</div>

</div>

您可能想查找下面的文章:

  • ruby迭代map的简洁写法实现原理分析
  • Ruby中访问SQL Server数据库的配置实例
  • ruby声明式语法的实现例子
  • Ruby中的Range对象学习笔记
  • Ruby中的String对象学习笔记
  • Ruby的基本语法学习总结
  • Ruby中的方法(函数)学习总结
  • Ruby中的变量学习总结
  • Ruby数组(Array)学习笔记
  • Ruby和元编程之万物皆为对象

相关文章

  • ruby 面向对象思维 概念
  • 详解Ruby on Rails中的Cucumber使用
  • 举例初步讲解Ruby中的正则表达式
  • 详解Ruby中范围的概念
  • 在Ruby on Rails上使用Redis Store的方法
  • Ruby简明教程之循环语句介绍
  • Ruby基础知识之类
  • Ruby基础知识之基本流程控制
  • Rails命令行常用操作命令简明总结
  • 使用Ruby实现FTP密码破解

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 利用Ruby的SOAP4R编写SOAP服务器的教程
    • Ruby实现邮件主动推送触发程序
    • Ruby on Rails实现最基本的用户注册和登录功能的教程
    • rudy 重载方法 详解
    • Ruby中使用SWIG编写ruby扩展模块实例
    • ruby中执行周期性任务(定时任务)的3种方法
    • 在Ruby on Rails上使用Redis Store的方法
    • ruby 简单例子
    • 以MVC的思维方式来理解Ruby on Rails框架的设计结构
    • Ruby的安装与运行

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有