返回   cpper编程论坛 > 算法
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2006-05-01
高级会员
 
注册日期: 2002-09-19
帖子: 840
文章: 7
tomato 正向着好的方向发展
默认 请问sicp 2.63题

Exercise 2.63. Each of the following two procedures converts a binary tree to a list.
lisp 代码:
  1. (define (tree->list-1 tree)
  2.   (if (null? tree)
  3.       '()
  4.       (append (tree->list-1 (left-branch tree))
  5.               (cons (entry tree)
  6.                     (tree->list-1 (right-branch tree))))))
  7. (define (tree->list-2 tree)
  8.   (define (copy-to-list tree result-list)
  9.     (if (null? tree)
  10.         result-list
  11.         (copy-to-list (left-branch tree)
  12.                       (cons (entry tree)
  13.                             (copy-to-list (right-branch tree)
  14.                                           result-list)))))
  15.   (copy-to-list tree '()))
b. Do the two procedures have the same order of growth in the number of steps required to convert a balanced tree with n elements to a list? If not, which one grows more slowly?

第一种方法是T(n) = 2 * T(n/2) + 1
由master theorem得到时间复杂度是O(n)
但第二种方法是T(n, m) = T(n/2, T(n/2, m)) + 1
这个我就不知道怎么算时间复杂度
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2006-05-01
高级会员
 
注册日期: 2002-09-19
帖子: 840
文章: 7
tomato 正向着好的方向发展
默认

晕,1算错了,append是O(n),所以1是O(nlog(n))
那么2该怎么算?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2006-05-02
普通会员
 
注册日期: 2006-03-28
帖子: 61
colinzhengj 正向着好的方向发展
默认

Should be
T(n, m) = T(n/2, Length(n/2, m)) + T(n/2, m) + c1
T(1, m) = c2
=>T(n, m) = theta(n)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #4 (permalink)  
旧 2006-05-03
高级会员
 
注册日期: 2002-09-19
帖子: 840
文章: 7
tomato 正向着好的方向发展
默认

谢谢
明白了,呵呵
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为格林尼治时间 +9。现在的时间是 07:30 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2009,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0