[算法导论 12章练习答案] 12.2 12.3

12.3

12.3
 1 //12.3-1
 2 TREE-INSERT(T,z)
 3     if root[T]=NIL
 4         root[t]=z
 5         return;
 6     x=root[T]
 7     if key(z)<key[x]
 8         TREE-INSERT(left[T],z) // here left[T] is a pointer to a tree, for we may modify the content of the tree
 9     else
10         TREE-INSERT(right[T],z)
11 
12 //12.3-2
13 // 论证:为在树中查找一个key,所检查的节点数等于插入该key时所检查的结点数+1
14 // 论证过程:假设插入该key时检查的结点路径是n0n1n2...nk,则查找该key时的路径也一定是n0n1...nk,不可能有第二条路径。
15 // 再加上该key结点,因此总共要检查k+1个节点数
16 
17 //12.3-3
18 /*
19     最好情况是:nlgn
20     最坏情况: n2
21 */
22 
23 //12.3-4 另外有一种数据结构包含指向BST某结点y的指针。用过程tree-delete删除y的前趋z,这样做会出现什么问题.如何改写tree-delete来解决这些问题
24 /*
25     会出现问题:y的指针失效
26     改写方法:tree-delete中,除了被删除结点的指针,其他结点的指针不变
27 */
28 // original version
29 TREE-DELETE(T,z)
30     if(left[z]=NIL or right[z]=NIL)
31         y=z
32     else
33         y=TREE-SUCCESSOR(z) // y is the node to be delete. and y should have at most one child. Can we use TREE-PREDECESSOR?
34     if left[y]!=NIL
35         x=left[y]
36     else
37         x=right[y]
38     if x!=NIL
39         p[x]=p[y]
40     if p[y]==NIL  // the tree is empty
41         root[T]=x
42     else 
43         if y=left[p[y]]
44             left[p[y]]=x
45         else
46             right[p[y]]=x
47     if y!=z
48         key[z]=key[y]
49         // and copy the satellite data from y to z
50     return y
51     
52 // modified version
53 TREE-DELETE(T,z)
54     if(left[z]=NIL or right[z]=NIL)
55         y=z
56     else
57         y=TREE-SUCCESSOR(z) // y is the node to be delete. and y should have at most one child. Can we use TREE-PREDECESSOR?
58     if left[y]!=NIL
59         x=left[y]
60     else
61         x=right[y]
62     if x!=NIL
63         p[x]=p[y]
64     
65     // 已经删除了要删除的结点
66     if p[y]==NIL  // the tree is empty
67         root[T]=x
68     else 
69         if y=left[p[y]]
70             left[p[y]]=x
71         else
72             right[p[y]]=x
73             
74     if y!=z
75         left[y]=left[z]
76         right[y]=right[z]
77         if z=left[p[z]]
78             left[p[z]]=y
79         else
80             right[p[z]]=y
81     return y
82     
83 //12.3-6
84 //修改策略:如何选择前趋或后继结点作为被拼接的结点,使得有更好的经验性能
85 //假设被删除的结点是z,有两个子节点。其前趋是p,后继是s。则一定有h(z)<h(p),h(z)<h(s)
86 // 从p、s中选择高度较大的结点作为被拼接的结点。这样可减少z的两棵子树的高度差,使得树更加平衡

转载于:https://www.cnblogs.com/chenhuanfa/archive/2012/11/29/2794134.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录(Table of Contents)   前言(Preface)   第一部分(Part I) 基础(Foundations)   第一章 计算中算法的角色(The Role of Algorithms in Computing)   第二章 开始(Getting Started)   第三章 函数的增长率(Growth of Functions)   第四章 递归(Recurrences)   第五章 概率分析与随机化算法(Probabilistic Analysis and Randomized Algorithms)   第二部分(Part II) 排序与顺序统计(Sorting and Order Statistics)   第六章 堆排序(Heapsort)   第七章 快速排序(Quicksort)   第八章 线性时间中的排序(Sorting in Linear Time)   第九章 中值与顺序统计(Medians and Order Statistics)   第三部分(Part III) 数据结构(Data Structures)   第十章 基本的数据结构(Elementary Data Structures)   第十一章 散列表(Hash Tables)   第十二章 二叉查找树(Binary Search Trees)   第十三章 红-黑树(Red-Black Trees)   第十四章 扩充的数据结构(Augmenting Data Structures)   第四部分(Part IV) 高级的设计与分析技术(Advanced Design and Analysis Techniques)   第十五章 动态规划(Dynamic Programming)   第十六章 贪婪算法(Greedy Algorithms)   第十七章 分摊分析(Amortized Analysis)   第五部分(Part V) 高级的数据结构(Advanced Data Structures)   第十八章 B-树(B-Trees)   第十九章 二项式堆(Binomial Heaps)   第二十章 斐波纳契堆(Fibonacci Heaps)   第二十一章 不相交集的数据结构(Data Structures for Disjoint Sets)   第六部分(Part VI) 图算法(Graph Algorithms)   第二十二章 基本的图算法(Elementary Graph Algorithms)   第二十三章 最小生成树(Minimum Spanning Trees)   第二十四章 单源最短路径(Single-Source Shortest Paths)   第二十五章 全对的最短路径(All-Pairs Shortest Paths)   第二十六章 最大流(Maximum Flow)   第七部分(Part VII) 精选的主题(Selected Topics)   第二十七章 排序网络(Sorting Networks)   第二十八章 矩阵运算(Matrix Operations)   第二十九章 线性规划(Linear Programming)   第三十章 多项式与快速傅里叶变换(Polynomials and the FFT)   第三十一章 数论算法(Number-Theoretic Algorithms)   第三十二章 字符串匹配(String Matching) ......................................................

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值