+ All Categories
Home > Documents > Data S tructures and Algorithms

Data S tructures and Algorithms

Date post: 24-Feb-2016
Category:
Upload: bambi
View: 33 times
Download: 1 times
Share this document with a friend
Description:
Data S tructures and Algorithms. Course’s slides: Hierarchical data structures www.mif.vu.lt /~ algis. Trees. Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)?. - PowerPoint PPT Presentation
159
Data Structures and Algorithms Course’s slides: Hierarchical data structures www.mif.vu.lt/~algis
Transcript

Data Structures and Algorithms

Data StructuresandAlgorithmsCourses slides: Hierarchical data structures

www.mif.vu.lt/~algisTreesLinear access time of linked lists is prohibitiveDoes there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)?

TreesA tree is a collection of nodesThe collection can be empty(recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r

Some TerminologiesChild and parentEvery node except the root has one parentA node can have an arbitrary number of childrenLeavesNodes with no children Siblingnodes with same parent

Some TerminologiesPathLengthnumber of edges on the pathDepth of a nodelength of the unique path from the root to that nodeThe depth of a tree is equal to the depth of the deepest leafHeight of a nodelength of the longest path from that node to a leafall leaves are at height 0The height of a tree is equal to the height of the rootAncestor and descendantProper ancestor and proper descendantExample: UNIX Directory

Binary TreesA tree in which no node can have more than two children

The depth of an average binary tree is considerably smaller than N, eventhough in the worst case, the depth can be as large as N 1.

Example: Expression TreesLeaves are operands (constants or variables)The other nodes (internal nodes) contain operatorsWill not be a binary tree if some operators are not binary

Tree traversalUsed to print out the data in a tree in a certain orderPre-order traversalPrint the data at the rootRecursively print out all data in the left subtreeRecursively print out all data in the right subtreePreorder, Postorder and InorderPreorder traversalnode, left, rightprefix expression++a*bc*+*defg

Preorder, Postorder and InorderPostorder traversalleft, right, nodepostfix expressionabc*+de*f+g*+Inorder traversalleft, node, right.infix expressiona+b*c+d*e+f*g

Preorder, Postorder and Inorder

Binary TreesPossible operations on the Binary Tree ADTparentleft_child, right_childsiblingroot, etcImplementationBecause a binary tree has at most two children, we can keep direct pointers to them

Compare: Implementation of a general tree

Binary Search TreesStores keys in the nodes in a way so that searching, insertion and deletion can be done efficiently.Binary search tree propertyFor every node X, all the keys in its left subtree are smaller than the key value in X, and all the keys in its right subtree are larger than the key value in X

Binary Search Trees

A binary search treeNot a binary search treeBinary search treesAverage depth of a node is O(log N); maximum depth of a node is O(N)

Two binary search trees representing the same set:Searching BSTIf we are searching for 15, then we are done.If we are searching for a key < 15, then we should search in the left subtree.If we are searching for a key > 15, then we should search in the right subtree.

Inorder traversal of BSTPrint out all the keys in sorted order

Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20findMin/findMaxReturn the node containing the smallest element in the treeStart at the root and go left as long as there is a left child. The stopping point is the smallest elementSimilarly for findMaxTime complexity = O(height of the tree)

InsertProceed down the tree as you would with a findIf X is found, do nothing (or update something)Otherwise, insert X at the last spot on the path traversedTime complexity = O(height of the tree)

DeleteWhen we delete a node, we need to consider how we take care of the children of the deleted node.This has to be done such that the property of the search tree is maintained.

DeleteThree cases:(1) the node is a leafDelete it immediately(2) the node has one childAdjust a pointer from the parent to bypass that node

Delete(3) the node has 2 childrenreplace the key of that node with the minimum element at the right subtree delete the minimum element Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2Time complexity = O(height of the tree)

12/26/03AVL Trees - Lecture 825Binary search tree best timeAll BST operations are O(d), where d is tree depthminimum d is for a binary tree with N nodesWhat is the best case tree? What is the worst case tree?So, best case running time of BST operations is O(log N)

12/26/03AVL Trees - Lecture 826Binary Search Tree - Worst TimeWorst case running time is O(N) What happens when you insert elements in ascending order?Insert: 2, 4, 6, 8, 10, 12 into an empty BSTProblem: Lack of balance: compare depths of left and right subtreeUnbalanced degenerate tree12/26/03AVL Trees - Lecture 827Balanced and unbalanced BST4251315243764265713Is this balanced?12/26/03AVL Trees - Lecture 828Approaches to balancing treesDon't balanceMay end up with some nodes very deepStrict balanceThe tree must always be balanced perfectlyPretty good balanceOnly allow a little out of balanceAdjust on accessSelf-adjusting12/26/03AVL Trees - Lecture 829Balancing binary search treesMany algorithms exist for keeping binary search trees balancedAdelson-Velskii and Landis (AVL) trees (height-balanced trees) Splay trees and other self-adjusting treesB-trees and other multiway search trees12/26/03AVL Trees - Lecture 830Perfect balanceWant a complete tree after every operationtree is full except possibly in the lower rightThis is expensiveFor example, insert 2 in the tree on the left and then rebuild as a complete treeInsert 2 &complete tree649815528691412/26/03AVL Trees - Lecture 831AVL - good but not perfect balanceAVL trees are height-balanced binary search treesBalance factor of a nodeheight(left subtree) - height(right subtree)An AVL tree has balance factor calculated at every nodeFor every node, heights of left and right subtree can differ by no more than 1Store current heights in each node12/26/03AVL Trees - Lecture 832Height of an AVL treeN(h) = minimum number of nodes in an AVL tree of height h.BasisN(0) = 1, N(1) = 2InductionN(h) = N(h-1) + N(h-2) + 1Solution (recall Fibonacci analysis)N(h) > h ( 1.62)h-1h-2h12/26/03AVL Trees - Lecture 833Height of an AVL TreeN(h) > h ( 1.62)Suppose we have n nodes in an AVL tree of height h.n > N(h) (because N(h) was the minimum)n > h hence log n > h (relatively well balanced tree!!)h < 1.44 log2n (i.e., Find takes O(logn))12/26/03AVL Trees - Lecture 834Node Heights100206498151height of node = hbalance factor = hleft-hrightempty height = -100height=2 BF=1-0=10649151Tree A (AVL)Tree B (AVL)12/26/03AVL Trees - Lecture 835Node heights after insert 7210306498151height of node = hbalance factor = hleft-hrightempty height = -110206491510707balance factor 1-(-1) = 2-1Tree A (AVL)Tree B (not AVL)12/26/03AVL Trees - Lecture 836Insert and rotation in AVL treesInsert operation may cause balance factor to become 2 or 2 for some node only nodes on the path from insertion point to root node have possibly changed in heightSo after the Insert, go back up to the root node by node, updating heightsIf a new balance factor (the difference hleft-hright) is 2 or 2, adjust tree by rotation around the node12/26/03AVL Trees - Lecture 837Single Rotation in an AVL Tree210206498151070102064981510712/26/03AVL Trees - Lecture 838Let the node that needs rebalancing be .

There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of .The rebalancing is performed through four separate rotation algorithms.Insertions in AVL trees12/26/03AVL Trees - Lecture 839jkXYZConsider a validAVL subtreeAVL insertion: outside case hhh12/26/03AVL Trees - Lecture 840jkXYZInserting into Xdestroys the AVL property at node jAVL Insertion: Outside Case hh+1h12/26/03AVL Trees - Lecture 841jkXYZDo a right rotationAVL Insertion: Outside Case hh+1h12/26/03AVL Trees - Lecture 842jkXYZDo a right rotationSingle right rotationhh+1h12/26/03AVL Trees - Lecture 843jkXYZRight rotation done!(Left rotation is mirror symmetric)Outside Case CompletedAVL property has been restored!hh+1h12/26/03AVL Trees - Lecture 844jkXYZAVL Insertion: Inside Case Consider a validAVL subtreehhh 12/26/03AVL Trees - Lecture 845Inserting into Y destroys theAVL propertyat node j jkXYZAVL Insertion: Inside CaseDoes right rotationrestore balance?hh+1h12/26/03AVL Trees - Lecture 846jkXYZRight rotationdoes not restorebalance now k isout of balanceAVL Insertion: Inside Casehh+1h12/26/03AVL Trees - Lecture 847Consider the structureof subtree YjkXYZAVL Insertion: Inside Casehh+1h12/26/03AVL Trees - Lecture 848jkXVZWiY = node i andsubtrees V and WAVL Insertion: Inside Casehh+1hh or h-112/26/03AVL Trees - Lecture 849jkXVZWiAVL Insertion: Inside CaseWe will do a left-right double rotation . . .12/26/03AVL Trees - Lecture 850jkXVZWiDouble rotation : first rotationleft rotation complete12/26/03AVL Trees - Lecture 851jkXVZWiDouble rotation : second rotationNow do a right rotation12/26/03AVL Trees - Lecture 852jkXVZWiDouble rotation : second rotationright rotation completeBalance has been restoredhhh or h-112/26/03AVL Trees - Lecture 853Implementationbalance (1,0,-1)keyrightleftNo need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you dont perform rotationsOnce you have performed a rotation (single or double) you wont need to go back up the tree12/26/03AVL Trees - Lecture 854Single RotationRotateFromRight(n : reference node pointer) {p : node pointer;p := n.right;n.right := p.left;p.left := n;n := p}XYZnYou also need to modify the heights or balance factors of n and pInsert12/26/03AVL Trees - Lecture 855Double RotationImplement Double Rotation in two lines.DoubleRotateFromRight(n : reference node pointer) {????}XnVWZ12/26/03AVL Trees - Lecture 856Insertion in AVL TreesInsert at the leaf (as for all BST)only nodes on the path from insertion point to root node have possibly changed in heightSo after the Insert, go back up to the root node by node, updating heightsIf a new balance factor (the difference hleft-hright) is 2 or 2, adjust tree by rotation around the node12/26/03AVL Trees - Lecture 857Insert in BSTInsert(T : reference tree pointer, x : element) : integer {if T = null then T := new tree; T.data := x; return 1;//the links to //children are nullcase T.data = x : return 0; //Duplicate do nothing T.data > x : return Insert(T.left, x); T.data < x : return Insert(T.right, x);endcase}12/26/03AVL Trees - Lecture 858Insert in AVL treesInsert(T : reference tree pointer, x : element) : {if T = null then {T := new tree; T.data := x; height := 0; return;}case T.data = x : return ; //Duplicate do nothing T.data > x : Insert(T.left, x); if ((height(T.left)- height(T.right)) = 2){ if (T.left.data > x ) then //outside case T = RotatefromLeft (T); else //inside case T = DoubleRotatefromLeft (T);} T.data < x : Insert(T.right, x); code similar to the left caseEndcase T.height := max(height(T.left),height(T.right)) +1; return;}12/26/03AVL Trees - Lecture 859Example of Insertions in an AVL Tree102201030250350Insert 5, 4012/26/03AVL Trees - Lecture 860Example of Insertions in an AVL Tree1022010302513505020103025135540000123Now Insert 4512/26/03AVL Trees - Lecture 861Single rotation (outside case)203201030251352502010302514054000012345Imbalance3545001Now Insert 3412/26/03AVL Trees - Lecture 862Double rotation (inside case)30320103025140250201035301405450123Imbalance4501Insertion of 3435340012534012/26/03AVL Trees - Lecture 863AVL Tree DeletionSimilar but more complex than insertionRotations and double rotations needed to rebalanceImbalance may propagate upward so that many rotations may be needed.12/26/03AVL Trees - Lecture 864Arguments for AVL trees:

Search is O(log N) since AVL trees are always balanced.Insertion and deletions are also O(logn)The height balancing adds no more than a constant factor to the speed of insertion.

Arguments against using AVL trees:Difficult to program & debug; more space for balance factor.Asymptotically faster but rebalancing costs time.Most large searches are done in database systems on disk and use other structures (e.g. B-trees).May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees).Pros and Cons of AVL Trees12/26/03AVL Trees - Lecture 865Double Rotation SolutionDoubleRotateFromRight(n : reference node pointer) {RotateFromLeft(n.right);RotateFromRight(n);}XnVWZOutlineBalanced Search Trees2-3 Trees2-3-4 TreesRed-Black TreesWhy care about advanced implementations?

Same entries, different insertion sequence: Not good! Would like to keep tree balanced.2-3 Treeseach internal node has either 2 or 3 childrenall leaves are at the same level

Features2-3 Trees with Ordered Nodes

2-node3-nodeleaf node can be either a 2-node or a 3-nodeExample of 2-3 Tree

Traversing a 2-3 Treeinorder(in ttTree: TwoThreeTree)if(ttTrees root node r is a leaf)visit the data item(s)else if(r has two data items){inorder(left subtree of ttTrees root)visit the first data iteminorder(middle subtree of ttTrees root)visit the second data iteminorder(right subtree of ttTrees root)}else{inorder(left subtree of ttTrees root)visit the data iteminorder(right subtree of ttTrees root)}Searching a 2-3 treeretrieveItem(in ttTree: TwoThreeTree,in searchKey:KeyType,out treeItem:TreeItemType):booleanif(searchKey is in ttTrees root node r){treeItem = the data portion of rreturn true}else if(r is a leaf)return falseelse{return retrieveItem(appropriate subtree,searchKey, treeItem)}What did we gain?

What is the time efficiency of searching for an item? Gain: Ease of Keeping the Tree Balanced

Binary SearchTree2-3 Treeboth trees afterinserting items39, 38, ... 32Inserting ItemsInsert 39

Inserting ItemsInsert 38

insert in leafdivide leafand move middlevalue up to parentresultInserting ItemsInsert 37

Inserting ItemsInsert 36

insert in leafdivide leafand move middlevalue up to parentovercrowdednodeInserting Items... still inserting 36

divide overcrowded node,move middle value up to parent,attach children to smallest and largestresultInserting ItemsAfter Insertion of 35, 34, 33

Inserting so far

Inserting so far

Inserting ItemsHow do we insert 32?

Inserting Itemscreating a new root if necessarytree grows at the root

Inserting ItemsFinal Result

70Deleting ItemsDelete 7080

Deleting ItemsDeleting 70: swap 70 with inorder successor (80)Deleting ItemsDeleting 70: ... get rid of 70

Deleting ItemsResult

Deleting ItemsDelete 100

Deleting ItemsDeleting 100

Deleting ItemsResult

Deleting ItemsDelete 80

Deleting ItemsDeleting 80 ...

Deleting ItemsDeleting 80 ...

Deleting ItemsDeleting 80 ...

Deleting ItemsFinal Result

comparison withbinary search treeDeletion Algorithm ILocate node n, which contains item IIf node n is not a leaf swap I with inorder successordeletion always begins at a leafIf leaf node n contains another item, just delete item Ielsetry to redistribute nodes from siblings (see next slide)if not possible, merge node (see next slide)Deleting item I:Deletion Algorithm II

A sibling has 2 items:redistribute itembetween siblings andparent

No sibling has 2 items:merge nodemove item from parentto siblingRedistributionMerging

Deletion Algorithm IIIInternal node n has no item leftredistribute Redistribution not possible:merge nodemove item from parentto siblingadopt child of nIf n's parent ends up without item, apply process recursivelyRedistributionMergingDeletion Algorithm IVIf merging process reaches the root and root is without item delete root

Operations of 2-3 Treesall operations have time complexity of log n2-3-4 Treessimilar to 2-3 trees4-nodes can have 3 items and 4 children4-node

2-3-4 Tree example2-3-4 Tree: InsertionInsertion procedure:similar to insertion in 2-3 treesitems are inserted at the leafssince a 4-node cannot take another item,4-nodes are split up during insertion process

Strategyon the way from the root down to the leaf:split up all 4-nodes "on the way" insertion can be done in one pass(remember: in 2-3 trees, a reverse pass might be necessary)2-3-4 Tree: InsertionInserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 1002-3-4 Tree: Insertion

Inserting 60, 30, 10, 20 ...... 50, 40 ...2-3-4 Tree: InsertionInserting 50, 40 ...

... 70, ...2-3-4 Tree: InsertionInserting 70 ...

... 80, 15 ...2-3-4 Tree: InsertionInserting 80, 15 ...

... 90 ...2-3-4 Tree: InsertionInserting 90 ...

... 100 ...2-3-4 Tree: InsertionInserting 100 ...

2-3-4 Tree: Insertion Procedure Splitting 4-nodes during Insertion

2-3-4 Tree: Insertion Procedure Splitting a 4-node whose parent is a 2-node during insertion

2-3-4 Tree: Insertion Procedure Splitting a 4-node whose parent is a 3-node during insertion

2-3-4 Tree: DeletionDeletion procedure:similar to deletion in 2-3 treesitems are deleted at the leafs swap item of internal node with inorder successornote: a 2-node leaf creates a problem

Strategy (different strategies possible)on the way from the root down to the leaf:turn 2-nodes (except root) into 3-nodes deletion can be done in one pass(remember: in 2-3 trees, a reverse pass might be necessary)2-3-4 Tree: DeletionTurning a 2-node into a 3-node ...Case 1: an adjacent sibling has 2 or 3 items "steal" item from sibling by rotating items and moving subtree30 5010 20402520 501030 4025"rotation"2-3-4 Tree: DeletionTurning a 2-node into a 3-node ...Case 2: each adjacent sibling has only one item "steal" item from parent and merge node with sibling(note: parent has at least two items, unless it is the root) 30 501040255025merging10 30 4035352-3-4 Tree: Deletion PracticeDelete 32, 35, 40, 38, 39, 37, 60

Red-Black Treebinary-search-tree representation of 2-3-4 tree3- and 4-nodes are represented by equivalent binary treesred and black child pointers are used to distinguish betweenoriginal 2-nodes and 2-nodes that represent 3- and 4-nodesRed-Black Representation of 4-node

Red-Black Representation of 3-node

Red-Black Tree Example

Red-Black Tree Example

Red-Black Tree OperationsTraversals same as in binary search trees

Insertion and Deletion analog to 2-3-4 tree need to split 4-nodes need to merge 2-nodesSplitting a 4-node that is a root

Splitting a 4-node whose parent is a 2-node

Splitting a 4-node whose parent is a 3-node

Splitting a 4-node whose parent is a 3-node

Splitting a 4-node whose parent is a 3-node

Motivation for B-TreesSo far we have assumed that we can store an entire data structure in main memoryWhat if we have so much data that it wont fit?We will have to use disk storage but when this happens our time complexity failsThe problem is that Big-Oh analysis assumes that all operations take roughly equal timeThis is not the case when disk access is involved

Motivation (cont.)Assume that a disk spins at 3600 RPMIn 1 minute it makes 3600 revolutions, hence one revolution occurs in 1/60 of a second, or 16.7msOn average what we want is half way round this disk it will take 8msThis sounds good until you realize that we get 120 disk accesses a second the same time as 25 million instructionsIn other words, one disk access takes about the same time as 200,000 instructionsIt is worth executing lots of instructions to avoid a disk accessMotivation (cont.)Assume that we use an Binary tree to store all the details of people in Canada (about 32 million records)We still end up with a very deep tree with lots of different disk accesses; log2 20,000,000 is about 25, so this takes about 0.21 seconds (if there is only one user of the program)We know we cant improve on the log n for a binary treeBut, the solution is to use more branches and thus less height!As branching increases, depth decreases

Definition of a B-treeA B-tree of order m is an m-way tree (i.e., a tree where each node may have up to m children) in which:1.the number of keys in each non-leaf node is one less than the number of its children and these keys partition the keys in the children in the fashion of a search tree2.all leaves are on the same level3.all non-leaf nodes except the root have at least m / 2 children4.the root is either a leaf node, or it has from two to m children5.a leaf node contains no more than m 1 keysThe number m should always be oddAn example B-Tree5162426122655

60

70

64

90

45

124781315182527

29

46

48

53

A B-tree of order 5 containing 26 itemsNote that all the leaves are at the same levelSuppose we start with an empty B-tree and keys arrive in the following order:1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45We want to construct a B-tree of order 5The first four items go into the root:

To put the fifth item in the root would violate condition 5Therefore, when 25 arrives, pick the middle key to make a new root

Constructing a B-tree12812Constructing a B-treeAdd 25 to the tree1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 451281225Exceeds Order. Promote middle and split.Constructing a B-tree (contd.)6, 14, 28 get added to the leaf nodes:1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45128122512812256122814Constructing a B-tree (contd.)Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 451 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 4512822561228142817Constructing a B-tree (contd.)7, 52, 16, 48 get added to the leaf nodes1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45128256122814177521648Constructing a B-tree (contd.)Adding 68 causes us to split the right most leaf, promoting 48 to the root1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 4581776211614125248282568Constructing a B-tree (contd.)Adding 3 causes us to split the left most leaf1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 454817876211614122528526837Constructing a B-tree (contd.)1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45Add 26, 29, 53, 55 then go into the leaves48178312675268252816141226295355Constructing a B-tree (contd.)Add 45 increases the trees level1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 454817832928262568555352161412671245Exceeds Order. Promote middle and split.Exceeds Order. Promote middle and split.Inserting into a B-TreeAttempt to insert the new key into a leafIf this would result in that leaf becoming too big, split the leaf into two, promoting the middle key to the leafs parentIf this would result in the parent becoming too big, split the parent into two, promoting the middle keyThis strategy might have to be repeated all the way to the topIf necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higherExercise in Inserting a B-Tree Insert the following keys to a 5-way B-tree:3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56

Answer to Exercise

Java Applet SourceRemoval from a B-treeDuring insertion, the key always goes into a leaf. For deletion we wish to remove from a leaf. There are three possible ways we can do this:1 - If the key is already in a leaf node, and removing it doesnt cause that leaf node to have too few keys, then simply remove the key to be deleted.2 - If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case can we delete the key and promote the predecessor or successor key to the non-leaf deleted keys position.Removal from a B-tree (2)If (1) or (2) lead to a leaf node containing less than the minimum number of keys then we have to look at the siblings immediately adjacent to the leaf in question: 3: if one of them has more than the min number of keys then we can promote one of its keys to the parent and take the parent key into our lacking leaf 4: if neither of them has more than the min number of keys then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys; if this step leave the parent with too few keys then we repeat the process up to the root itself, if required

Type #1: Simple leaf deletion12295227915225669723143Delete 2: Since there are enoughkeys in the node, just delete itAssuming a 5-wayB-Tree, as before...Note when printed: this slide is animatedType #2: Simple non-leaf deletion1229527915225669723143Delete 52Borrow the predecessoror (in this case) successor56Note when printed: this slide is animatedType #4: Too few keys in node and its siblings12295679152269723143Delete 72Too few keys!

Join back togetherNote when printed: this slide is animatedType #4: Too few keys in node and its siblings122979152269563143Note when printed: this slide is animatedType #3: Enough siblings122979152269563143Delete 22Demote root key andpromote leaf keyNote when printed: this slide is animatedType #3: Enough siblings1229791531695643Note when printed: this slide is animatedExercise in Removal from a B-TreeGiven 5-way B-tree created by these data (last exercise):3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56Add these further keys: 2, 6,12Delete these keys: 4, 5, 7, 3, 14

Answer to Exercise

Java Applet SourceAnalysis of B-TreesThe maximum number of items in a B-tree of order m and height h:rootm 1level 1m(m 1)level 2m2(m 1). . .level hmh(m 1)So, the total number of items is(1 + m + m2 + m3 + + mh)(m 1) =[(mh+1 1)/ (m 1)] (m 1) = mh+1 1When m = 5 and h = 2 this gives 53 1 = 124Reasons for using B-TreesWhen searching tables held on disc, the cost of each disc transfer is high but doesn't depend much on the amount of data transferred, especially if consecutive items are transferredIf we use a B-tree of order 101, say, we can transfer each node in one disc read operationA B-tree of order 101 and height 3 can hold 1014 1 items (approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory)If we take m = 3, we get a 2-3 tree, in which non-leaf nodes have two or three children (i.e., one or two keys)B-Trees are always balanced (since the leaves are all at the same level), so 2-3 trees make a good type of balanced tree


Recommended