Chapter11-1-二叉搜索树-2学时.pptVIP

  • 6
  • 0
  • 约7.42千字
  • 约 39页
  • 2019-01-08 发布于湖北
  • 举报
* * 索引BST的操作(续1) 当将元素插入到带索引的二叉搜索树中时,与普通二叉搜索树过程类似,但此时需要更新从根节点到新插入节点路径上的所有节点的LeftSize值。 带索引二叉搜索树的删除:首先按索引进行搜索,找到被删除元素,然后将其删除,如果需要的话,还需要修改从根至被删除元素路径上所有节点的LeftSize域。 * * 课后练习 编码完成: Page327:练习5,定义带索引的BST,实现其搜索、插入、删除等操作 * * 3、二叉搜索树的应用:P354 直方图问题:从一个具有n个关键值的集合开始,要求输出不同关键值的列表以及每个关键值在集合中出现的次数(频率)。 例如:n=10,key=[2, 4, 2 , 2 , 3, 4 , 2 , 6, 4 ,2] * * 直方图的一般统计方法 当关键值为从0到r范围内的整数且r 的值足够小时,可以在线性时间内,用一个相当简单的过程产生直方图。在该过程中用数组元素h[i ]代表关键值i 的频率 参见Page355:程序11-6,h[key]++ 局限性:当关键值类型不是整型(如关键值类型是实数)或关键值范围变化很大时,该方法不可用。 * * 基于二叉搜索树的方法 扩展BSTree类模板:增加插入同时计数的方法 BSTreeE,K InsertVisit (const Ee, void(*Visit)(Eu)); 算法的平均复杂性为O(nlogm),其中m为关键值的数量。 参见Page356:程序11-7 * * 使用二叉搜索树的直方图算法: 11-7 class eType { friend void main(void); friend void Add1(eType); friend ostream operator (ostream, eType); public: operator int( ) const {return key;} private: int key, // element value count; // frequency }; ostream operator(ostream out, eType x) { out x.key x.count ; return out; } void Add1(eType e) { e.count++; } * * void main(void) { // Histogram using a search tree. BSTreeeType, int T; int n; // number of elements cout Enter number of elements endl; cin n; // input elements and enter into tree for (int i = 1; i = n; i++) { eType e; // input element cout Enter element i endl; cin e.key; e.count = 1; * * // put e in tree unless match already there // in latter case increase count by 1 try { T.InsertVisit(e, Add1); } catch (NoMem) {cout Out of memory endl; exit(1);} } // output distinct elements and their counts cout Distinct elements and frequencies are“ endl; T.Ascend( ); } * * 本节小结 二叉搜索树的定义 二叉搜索树的操作 搜索 插入 删除 带索引的二叉搜索树 应用:直方图 * * 1、如果使用除数为D的链表,那么能在(D+n) 的时间内取出元素,在O (nlogn) 时间内完成排序和在O(n) 时间内输出, 因此共需时间O (D+nlogn) * * 查找指定顺序的元素! LOGO LOGO * * Chapter11 搜索树 * * 11.1 二叉搜索树 11.2 AVL树 11.3 红黑树 11.4 B-树

文档评论(0)

1亿VIP精品文档

相关文档