Ch. 13- Balanced Search Trees.ppt
《Ch. 13- Balanced Search Trees.ppt》由会员分享,可在线阅读,更多相关《Ch. 13- Balanced Search Trees.ppt(19页珍藏版)》请在麦多课文档分享上搜索。
1、Ch. 13: Balanced Search Trees,Symbol table: insert, delete, find, pred, succ, sort, Binary Search Tree review: What is a BST? binary tree with a key at each node for any node, the keys in the left subtree are less than the key of the current node, and those in the right subtree greater How do you im
2、plement these operations in a BST? find insert delete pred What is the average runtime of each operation? What is the worst case?,Balanced Search Trees (Ch. 13),To implement a symbol table, Binary Search Trees work pretty well, exceptthe worst case is O(n) and it is embarassingly likely to happen in
3、 practice if the keys are sorted, or there are lots of duplicates, or various kinds of structure Ideally we would want to keep a search tree perfectly balanced, like a heap How can we insert or delete in O(log n) time and re-balance the whole tree?,234 Intro,234 Trees are are worst-case optimal: Q(l
4、og n) per operation Idea: nodes have 1, 2, or 3 keys and 2, 3, or 4 links. Subtrees have keys ordered analogously to a binary search tree. A balanced 234 search tree has all leaves at the same level. How would search work? How would insertion work? split nodes on the way back up? or split 4-nodes on
5、 the way down?,Top-down vs. Bottom-up,Top-down 2-3-4 trees split nodes on the way down. But splitting a node means pushing a key back up, and it may have to be pushed all the way back up to the root. Its easier to split any 4-node on the way down.,2-node with 4-node child: split into 3-node with two
6、 2-node children3-node with 4-node child: split into 4-node with two 2-node childrenThus, all searches end up at a node with space for insertion,Construction Example,234 Balance,All paths from the top to the bottom are the same heightWhat is that height?worst case: lgN (all 2-nodes)best case: lgN/2
7、(all 4-nodes) height 10-20 for a million nodes; 15-30 for a billionOptimal! (But is it fast?),Implementation Details,Actually, there are many 234-tree variants: splitting on the way up vs. down 2-3 vs. 2-3-4 trees Implementation is complicated because of the large number of cases that have to be con
8、sidered. What would happen if we used even more children of each node? (B-Trees) Can we improve the optimal balanced-tree approach, for fewer cases and strictly binary nodes? (Red-black Trees),B-Trees,What about using even more keys? B-trees Like a 234 tree, but with many keys, say b=100 or 500 Usua
9、lly enough keys to fill a 4k or 16k disk block Time to find an item: O(logbn) E.g. b=500: can locate an item in 500 with one disk access, 250,000 with 2, 125,000,000 with 3Used for database indexes, disk directory structures, etc., where the tree is too large for memory and each step is a disk acces
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CH13BALANCEDSEARCHTREESPPT
