数据结构与程序设计王丽苹25二分查找树_第1页
数据结构与程序设计王丽苹25二分查找树_第2页
数据结构与程序设计王丽苹25二分查找树_第3页
数据结构与程序设计王丽苹25二分查找树_第4页
数据结构与程序设计王丽苹25二分查找树_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、10/21/2021数据结构与程序设计 1数据结构与程序设计数据结构与程序设计(25)chapter10.2 binary search tree 王丽苹 10/21/2021数据结构与程序设计 2binary search trees p444np445 definition: a binary search tree (二分查找树二分查找树) is a binary tree that is either empty or in which the data entry of every node has a key and satisfies the conditions:n1. the

2、 key of the left child of a node (if it exists) is less than the key of its parent node.n2. the key of the right child of a node (if it exists) is greater than the key of its parent node.n3. the left and right subtrees of the root are again binary search trees.10/21/2021数据结构与程序设计 3binary search tree

3、snno two entries in a binary search tree may have equal keys.nwe may regard binary search trees as a specialization of binary trees.n同时二分查找树主要用于构建ordered list。方便对关键码的查找。p44610/21/2021数据结构与程序设计 4binary search trees数据结构数据结构ntemplate nclass search_tree: public binary_tree npublic:nerror_code insert(con

4、st record &new_data);nerror_code remove(const record &target);nerror_code tree_search(record &target) const;nprivate: / add auxiliary function prototypes here.n;10/21/2021数据结构与程序设计 5implement of binary search trees10.2.2 tree search 查找操作查找操作nerror_code tree_search(record &target) const;n方法:方法:n将将tar

5、get与树根节点比较,如果相等则找到,程序结束。与树根节点比较,如果相等则找到,程序结束。n如果比根节点大,则进入右子树继续查找。如果比根节点大,则进入右子树继续查找。n如果比根节点小,则进入左子树继续查找。如果比根节点小,则进入左子树继续查找。10/21/2021数据结构与程序设计 6implement of binary search trees10.2.2 tree search 查找操作查找操作 p447example:查找:查找kim的过程的过程10/21/2021数据结构与程序设计 7implement of binary search treestree search 查找操作查

6、找操作 实现实现n#include binary_tree.cppntemplate nclass search_tree: public binary_tree npublic:nerror_code insert(const record &new_data);nerror_code remove(const record &target);nerror_code tree_search(record &target /* out */) const;n /查找成功,查找成功,target的内容为查找树种节点的信息。返回的内容为查找树种节点的信息。返回success。n /查找失败,返回查

7、找失败,返回notpresentnprivate: / add auxiliary function prototypes here. binary_node * search_for_node(binary_node* sub_root, const record &target) const; p448;10/21/2021数据结构与程序设计 8tree search 查找操作查找操作 实现实现binary_node * search_for_node(binary_node* sub_root, const record &target) const; p448nsearch_for_n

8、ode是一个查找子函数。是一个查找子函数。nsub_root: 为空或者指向一棵子树。为空或者指向一棵子树。npostcondition: 当当target不在子树不在子树sub_root中存在时,返回中存在时,返回null,当找到当找到target时返回指向时返回指向target的指针。的指针。10/21/2021数据结构与程序设计 9implement of binary search trees查找操作查找操作 实现实现template error_code search_tree : tree_search(record &target) consterror_code result

9、= success;binary_node *found = search_for_node(root, target);if (found = null)result = not_present;elsetarget = found-data;return result;10/21/2021数据结构与程序设计 10implement of binary search trees查找操作查找操作 实现实现-递归递归template binary_node *search_tree : search_for_node(binary_node* sub_root, const record &ta

10、rget) constif (sub_root = null | sub_root-data = target)return sub_root;else if (sub_root-data right, target);else return search_for_node(sub_root-left, target);10/21/2021数据结构与程序设计 11implement of binary search trees查找操作查找操作 实现实现 非递归非递归/nonrecursive version:template binary_node *search_tree : search_

11、for_node(binary_node* sub_root, const record &target) constwhile (sub_root != null & sub_root-data != target)if (sub_root-data right;else sub_root = sub_root-left;return sub_root;10/21/2021数据结构与程序设计 12查找操作查找操作 实现分析实现分析 p449页页 需要注意:同样的关键码,可能产生很多不同的查找树需要注意:同样的关键码,可能产生很多不同的查找树结构。图结构。图(a) (b) (c) (d) (e

12、)为由七个字母构成的二分为由七个字母构成的二分查找树的结构。查找树的结构。对于查找操作对于查找操作:整棵树越浓密,则查找操作的效率越高。整棵树越浓密,则查找操作的效率越高。 最好的结构最好的结构最常见的结构最常见的结构10/21/2021数据结构与程序设计 13查找操作查找操作 实现分析实现分析 p449页页最坏的结构最坏的结构10/21/2021数据结构与程序设计 1410.2.3 insert into a binary search tree 二分查找树的插入操作n向二分查找树中插入节点的过程分析:向二分查找树中插入节点的过程分析:l如果二叉查找树为空,则新结点作为根结点。如果二叉查找树

13、为空,则新结点作为根结点。l如果二叉查找树非空,则将新结点的关键码与根结如果二叉查找树非空,则将新结点的关键码与根结点的关键码比较:点的关键码比较:l若相等表示该结点已在二叉排序树中插入失败;若相等表示该结点已在二叉排序树中插入失败;l若小于根结点的关键码,则将新结点插入到根结点的左子若小于根结点的关键码,则将新结点插入到根结点的左子树中;树中;l否则,插入到右子树中。否则,插入到右子树中。l 子树中的插入过程和树中的插入过程相同,如此进子树中的插入过程和树中的插入过程相同,如此进行下去,直到找到该结点,或者直到左或右子树为行下去,直到找到该结点,或者直到左或右子树为空二叉树,新结点插入成为叶

14、子结点为止。空二叉树,新结点插入成为叶子结点为止。10/21/2021数据结构与程序设计 15p451 e,b,d,f,a,g,c逐一插入的过程逐一插入的过程10/21/2021数据结构与程序设计 1610.2.3 insert into a binary search tree 插入操作分析n(1) 不同的插入序列可能会产生相同的二不同的插入序列可能会产生相同的二分查找树的结构。分查找树的结构。n如如 e,f,g,b,a,d,c and e,b,d,c,a,f,gn(2) 如果插入的序列已经有序,那么生成如果插入的序列已经有序,那么生成的查找树的结构是最坏的。的查找树的结构是最坏的。n如如a

15、,b,c,d,e,f,g, 将生成图将生成图10.8(e)的结构的结构10/21/2021数据结构与程序设计 17implement of binary search treestree search 插入操作插入操作 实现实现n#include binary_tree.cppntemplate nclass search_tree: public binary_tree npublic:nerror_code insert(const record &new_data);nn error_code remove(const record &target);nerror_code tree_s

16、earch(record &target /* out */) const;n nprivate: / add auxiliary function prototypes here. error_code search_and_insert( binary_node * &sub_root, const record &new_data);10/21/2021数据结构与程序设计 18implement of binary search trees插入操作插入操作 实现实现template error_code search_tree : insert( const record &new_da

17、ta)/插入成功返回插入成功返回 success /插入失败返回插入失败返回 duplicate_errorerror_code result=search_and_insert(root, new_data);if(result=success)count+;return result;10/21/2021数据结构与程序设计 19implement of binary search trees插入操作插入操作 实现实现template error_code search_tree : search_and_insert( binary_node * &sub_root /*inout*/,

18、const record &new_data /*in*/)if (sub_root = null) sub_root = new binary_node(new_data);return success;else if (new_data data)return search_and_insert(sub_root-left, new_data);else if (new_data sub_root-data)return search_and_insert(sub_root-right, new_data);else return duplicate_error;10/21/2021数据结

19、构与程序设计 2010.2.4 tree sort n对于一棵二叉查找树,中序遍历的序列即为有对于一棵二叉查找树,中序遍历的序列即为有序序列。序序列。n因此,二叉查找树的插入过程也可以看成是排因此,二叉查找树的插入过程也可以看成是排序的过程。即序的过程。即n首先,将无序序列逐一插入二叉排序树。首先,将无序序列逐一插入二叉排序树。n然后,对二叉排序树进行中序遍历。完成排序然后,对二叉排序树进行中序遍历。完成排序ntree sort比较适合于无序的序列,对于基本有比较适合于无序的序列,对于基本有序的序列效率较低序的序列效率较低10/21/2021数据结构与程序设计 2110.2.5 removal

20、 from a binary search tree 删除操作讨论n(1) 删除的节点是叶子结点:replace the link to the removed node by null.10/21/2021数据结构与程序设计 2210.2.5 removal from a binary search tree 删除操作讨论n(2) 删除的节点只有一棵非空子树:adjust the link from the parent of the removed node to point to the root of the nonempty subtree.10/21/2021数据结构与程序设计 2

21、310.2.5 removal from a binary search tree 删除操作讨论n(3) 删除的节点左右子树都非空:找到被删除节点的中序遍历的前驱(左子树中最右边的孩子),用前驱节点代替被删除点.10/21/2021数据结构与程序设计 24implement of binary search treestree search 删除操作删除操作 实现实现#include binary_tree.cpptemplate class search_tree: public binary_tree public:。 error_code remove(const record &tar

22、get);private: / add auxiliary function prototypes here. error_code remove_root( binary_node * &sub_root/*inout*/) error_code search_and_destroy( binary_node* &sub_root, const record &target);10/21/2021数据结构与程序设计 25implement of binary search treestree search 删除操作删除操作 实现实现 p456error_code remove_root( b

23、inary_node * &sub_root/*inout*/)/remove_root直接将直接将sub_root指向的节点从查找树中删除。指向的节点从查找树中删除。e.g.if the node at left of x is to be removed, the call should be: remove_root(x-left);if the root of the tree is to be removed, the call should be: remove_root(root);10/21/2021数据结构与程序设计 26implement of binary search

24、trees删除操作删除操作 实现实现 p458template error_code search_tree : remove(const record &target)/* post: if a record with a key matching that of target belongs to the search tree a code of success is returned and the corresponding node is removed from thetree. otherwise, a code of not present is returned.uses:

25、 function search_and_destroy */error_code result=search_and_destroy(root, target);if(result=success)count-;return result;10/21/2021数据结构与程序设计 27implement of binary search trees删除操作删除操作 实现实现 p458template error_code search_tree : search_and_destroy( binary_node* &sub_root, const record &target)/*post:

26、if the key of target is not in the subtree, a code of not_present is returned. otherwise, a code of success is returned and the subtree node containing target has been removed in such a way that the properties of a binary search tree have been preserved.uses: functions search and destroy recursively

27、 and remove root */if (sub_root = null | sub_root-data = target)return remove_root(sub_root);else if (target data)return search_and_destroy(sub_root-left, target);elsereturn search_and_destroy(sub_root-right, target);10/21/2021数据结构与程序设计 28删除操作删除操作 实现实现 p457template error_code search_tree : remove_ro

28、ot(binary_node * &sub_root)if (sub_root = null) return not_present;binary_node *to_delete = sub_root; / remember node to delete at end./处理只有一棵子树或者没有孩子节点的情况处理只有一棵子树或者没有孩子节点的情况 if (sub_root-right = null) sub_root = sub_root-left;else if (sub_root-left = null) sub_root = sub_root-right;else / neither s

29、ubtree is empty.to_delete = sub_root-left; / move left to find predecessor. 记录待删除的节点记录待删除的节点binary_node *parent = sub_root; / parent of to_deletewhile (to_delete-right != null) / to_delete is not the predecessor.parent = to_delete;to_delete = to_delete-right; sub_root-data = to_delete-data; / move from to_delete to root.if (parent = sub_root) sub_root-left = to_delete-left;else parent-right = to_delete-left; delete to_delete; / remove to_delete from tree.return success;10/21/2021数据结构与程序设计 2

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论