1 / 35

数据结构

数据结构. 辅导教师 倪政林. 参考学习站点 : 自考网首页 : http://student.zjzk.cn/course_ware/ data_structure/web/main.htm 天极 yesky : http://www.yesky.com/SoftChannel/ 72348977504190464/20031118/1745923.shtml 维 C 世界 (C 版 ):http://www.vcok.com/class/

Download Presentation

数据结构

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 数据结构 辅导教师 倪政林

  2. 参考学习站点: 自考网首页: http://student.zjzk.cn/course_ware/ data_structure/web/main.htm 天极yesky : http://www.yesky.com/SoftChannel/ 72348977504190464/20031118/1745923.shtml 维C世界(C版):http://www.vcok.com/class/ index.asp?classid=9

  3. 数据结构 第一章 绪论 第二章 线性表 第三章 稀疏矩阵和广义表 第四章 栈和队列 第五章 树和二叉树 第六章 二叉树的应用 第七章 图 第八章 查找 第九章 排序

  4. 二叉树的应用 第六讲 二叉搜索树 堆 哈夫曼

  5. 6.1 二叉搜索树 1.定义 2.查找算法及性能的分析 3.插入算法 4.删除算法

  6. 1.定义 又称二叉排序树,它或是一棵空树,或者是具有如下 特点的非空二叉树: (1)若它是左子树非空,则左子树上所有结点的关键字 均小于根结点的关键字; (2)若它的右子树非空,则右子树 上所有结点的关键字均大于 根结点的关键字; (3)左、右子树又各是 一棵二叉搜索树。 30 52 15 12 23 74 63 26 18

  7. 30 性质: 52 15 中序是一个有序的结点序列。 12 23 74 63 26 中序序列是什么 18 12, 15, 18, 23, 26, 30, 52, 63, 74

  8. 二叉链表作为二叉搜索树的存储结构 struct BTreeNode { ElemType data; BTreeNode * left; BTreeNode * right; };

  9. 2.查找算法 A.算法设计: Bool find (Btreenode * bst, elemtype& item) {if (bst == null) return false; Else {if (item== Bst->data) {item = bst->data; return true;} else if (item < bst->data) return find(bst->left,item); else return find(bst->right,item); } }

  10. 30 B.算法分析: 最好: 最坏: 一般: 52 15 O(log2n) 12 23 74 O(n) O(log2n) 63 26 18 思考题: 为什么会出现三种不同的复杂度 答案: 最好的条件: 理想平衡二叉树 最坏的条件: 单支树(线性表) 平均的条件: 近似平衡二叉树

  11. 3. 二叉搜索树的插入算法 • “插入”操作在查找不成功时才 进行; • 若二叉搜索树为空树,则新插入的结点为根结点;否则,新插入的结点必为一个叶子结点,其插入位置由查找过程得到。

  12. 14 81 30 1.如何插入15? 2.如何插入81、14? 插入的结点具有 什么特点? 52 15 23 12 74 18 26 63 答:都是叶子结点。

  13. 插入算法设计 Void insert(btreenode * bst, const elemtype item) {if(bst == null) {btreenode* P=new btreenode; p->data = item; p->left =p->right =null; bst = p;} else if ( item < bst->data ) insert(bst->left,item); else insert(Bst->right,item); }

  14. (1)被删除的结点是叶子; (2)被删除的结点只有左子树 或者只有右子树; (3)被删除的结点既有左子树, 也有右子树。 4.二叉搜索树的删除算法 可分三种情况讨论:

  15. p A (1)被删除的结点是: 叶子结点 其双亲结点相应指针域改为“空” p S S B S = p->left; p->left = null ; delete(s); / S = p->right; p->left = null ; delete(s);

  16. (2)被删除的结点只有单支子树 其双亲结点的相应指针域改为 “指向被删除结点的左子树或 右子树”。

  17. S p p S A B A.右子树为空只需重接它的左子树 s=p->left; p->left =q ; delete(s); s=p->right; p->right=q ; delete(s); q q

  18. S p p S A B q q B.左子树为空只需重接它的右子树 s=p->left; p->left =q ; delete(s); s=p->right; p->right=q ; delete(s);

  19. 前驱结点 被删结点 (3)被删除的结点有左右子树 第一种方法:其右了树接到其第一前驱的右边,左子树接到其父结点的下边。 第一前驱:左子树中“最右下”的结点 (左指针可能不为空)

  20. 30 30 52 52 15 15 待删结点 12 12 23 74 18 74 63 26 63 11 21 13 11 13 18 26 28 21 调整前 调整后 28

  21. 第二种方法: 把其中序前驱结点的值赋给该结点,删除前驱结点并把其左子树链到前驱所在的位置即可。 注:右子树中“最左下”的结点 左指针可能不为空。

  22. 30 30 待删结点 52 52 15 14 12 12 23 74 23 74 63 26 63 11 26 11 14 18 13 18 28 13 28 调整后 调整前

  23. 6.2 堆 小根堆:若根结点存在左孩子, 则根值小于左孩子值;若根有右孩子, 则根值小于右孩子; 左右孩子又是一个堆。 大根堆相反。 74 42 53 18 25 36 35 20 35 26 73 48 60 18 22 小根堆 大根堆

  24. 图例

  25. 两个问题: 如何“建堆”? 完全二叉树 如何“筛选”? 调整为堆

  26. 堆是满足下列性质的数列{r1, r2, …,rn}: 或 (小顶堆) (大顶堆) 对于完全二叉树r2i是 ri的左孩子 r2i+1是 ri的右孩子

  27. 堆的存储结构 --顺序存储(还有链式) Struct Heap{elemtype heap[maxsize];int size;} 插入得法(小根堆): Void insertheap(heap&hbt, const elemtype item) {hbt.heap[hbt.size]=item; Hbt.size++; elemtype x=item; int i=hbt.size-1; While(I != 0) {int j=|| (i-1)/2 || ; if (x>=hbt.heap[j]) break; hbt.heap[i]=hbt.heap[j]; i=j;} Hbt.heap[i]=x; }

  28. 图例 最小堆的向上调整

  29. 删除小根堆顶点的算法: Elemtype deleheap(heap& hbt) {if(hbt.size==0){cerr<<“heap is null !”<<endl; exit(1);} Elemtype temp=hbt.heap[0]; Hbt.size - - ; if (hbt.size==0) return temp; Elemtype x = hbt.heap[hbt.size]; Int i=0; int j=2*i+1; While(j<=hbt.size-1) {if (j < hbt.size-1 && hbt.heap[j] > hbt.heap[j+1] ) j++; If(x <= hbt.heap[j]) break; Hbt.heap[i] = hbt.heap[j]; i=j; j=2*i+1;} Hbt.heap[i]=x; return temp; }

  30. 第二 第一 18 60 35 35 26 26 73 73 48 60 48 60 26 26 35 60 35 48 73 48 73 60 第三 第四

  31. 建堆实例: Void main() {int a[8]={23,56,40,62,38,55,10,16}; Heap b; initheap(b); int I,x; For(i=0;i<8;i++) insert(b,a[i]; While(!heapempty(b)) {x=deleteheap(b); Cout<<x; If(!heapempty(b)) cout<<‘,’;} Cout<<endl; }

  32. 图例 自下向上逐步调整为最小堆

  33. 哈夫曼树 • 最优树的定义 • 如何构造最优树

  34. 最优树的定义 / 哈夫曼树 概念: 路径长度、 带权路径长度 1 9 2 5 3 3 4 2 WPL=9*1+5*2+4*3+2*3

  35. 哈夫曼树的构造过程

More Related