Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.

Similar presentations


Presentation on theme: "Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions."— Presentation transcript:

1 Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions

2 Motivation Primary memory (RAM) : very fast, but costly Secondary storage (disk) : very cheap, but slow Problem: a large D.B. must reside partially on disk. But disk operations are very slow. Solution: take advantage of important disk property -Basic read/write unit is a page (2-4 Kb) - can’t read/write less. Thus when analyzing D.B. performance, we consider two different measures: CPU time and number of times we need to access the disk. Besides, B-trees are an interesting type of balanced trees...

3 B-Trees B-Tree: a balanced search tree whose nodes can have many children: A node x contains n[x] keys, and has n[x]+1 children (c 1 [x], c 2 [x], …, c n[x]+1 [x]). The keys in each node are ordered, and relate to their left and right sub-trees like regular search trees: if k i is any key stored in the sub-tree rooted at c i [x], then: All leaves have the same depth h (the tree’s height) There is a fixed integer t (the minimum degree) : –Every node (besides the root) has at least t-1 keys (i.e. t children) –Every node can contain at most 2t-1 keys (2t children).

4 Example 50 2510898365733934827086856154939022201712 t=3

5 B-Trees and disk access (last time...) Each node contains as many keys as possible without being larger than a single page on disk. Whenever we need to access a node – load it from the disk (one read operation), after changing a node – rewrite it to the disk. For example, say each node contains 1000 keys – then the root has 1001 children, each of which also has 1001 children. Thus with just 2 disk accesses we are able to access ~1000 3 records. Operations are designed to work in one pass from the root to the leaves – we do not need to backtrack our steps. This further reduces the number of disk accesses we make.

6 The height of a B-Tree Theorem: If n  1, then for any B-tree of height h with n keys and minimum degree t  2: h  log t ( (n+1) / 2 ) Proof : Each child of the root has at least t children, each of them also has at least t children, and so on. Thus in every sub-tree of the root there are at least nodes. Each of them contains at least t-1 keys. The root contains at least one key and has at least two children, so we have:

7 B-Tree Search Search is done in the regular way: In each node, we find the sub-tree in which our value might be, and recursively find it there. Performance : O(th) = O(tlog t n) - total run-time, out of which: O(h) = O(log t n) - disk access operations

8 B-Tree Split Used for insertion. This operation verifies that a node will have less than 2t-1 keys. What we do is split the node into two nodes, each with t-1 keys. The extra key goes into the node’s parent (We assume the parent is not full) To split a node x (look at the next slide for illustration), take key t [x] (notice it is the median key). All smaller keys (exactly t-1 of them) form one new (legal) node, the same with all larger keys. key t [x] goes into x’s parent. If the node we split is the root, then a new root is created. This new root contains only one key.

9 B-tree split xy m t-1 keys...... xym t-1 keys...... (parent) (full node) Notice that the parent has many other sub-trees that don’t change.

10 Example 50 2510 8983659596 89502510 83659596 A full node (t=3)

11 B-Tree Insert We insert a key only to a leaf. We start from the root and go down to the appropriate leaf. On the way, before going down to the next node, we check if it is full. If so, we split it (its father is non-full because we checked this before going down to the father). When we reach the right leaf, we know that the leaf is not full, so we can simply insert the new value to the leaf. Notice that we may need to split the root, if it is full. In this case, the tree’s height increases (but the tree remains completely balanced!). That’s why we say that a B-tree grows from the root, in contrast to most of the trees, who grow from the leaves...

12 Example 10733439 (I) Inserting 3,7,34,10,39 10 73253934 (II) Inserting 25 splits the root 10 732539342040 (III) Inserting 40 and 20 3410734039 252017 (IV) Inserting 17 splits the right leaf We start with an empty tree (t=3)

13 B-Tree Insert (cont.) Performance: –Split: three disk accesses (to write the 2 new nodes, and the parent) O(t) - total run time –Insert: O(h) - disk accesses O(tlog t n) - total run time Requires O(1) pages in main memory.

14 Problem Set 1 Solutions 2.b, Solution –This recurrence is defined for power of 2, –We use here the general Master theorem which deals with –In this case –We will claim that (next next slide)

15 Problem Set 1 Solutions (cont) –The first case of Master theorem applies: for some, because –Using the Master theorem we have

16 Problem Set 1 Solutions (cont) –Claim : Recall that This implies that for any constant substitute, and get Now set and we get that If we multiply by n we get

17 Problem Set 1 Solutions (cont) 3. Finding the missing integer –All the integers from 0 to n except one, are stored in an array A[1…n]. We want to find the missing integer. –In this problem we cannot access an entire integer in A with a single operation. –The elements in A are represented as binary strings. –The only operation to access the integers is to ask for the ith bit of A[j]. –Each such query takes constant time. –Give an algorithm that finds the missing integer using queries.

18 Problem Set 1 Solutions (cont) Assumptions: –All integers are presented with the same number of bits. 0’s are put to the left of smaller numbers. – for some integer k. Let x be the missing integer.

19 Problem Set 1 Solutions (cont) The principle: –A binary search –Run on the first bit of all the integers in A (takes n queries). –Count the number of 0’s you see on the way. –If x<N/2 then one zero is missing (N/2-1 0’s). –If x>=N/2 then no zero is missing (N/2 0’s). –After one pass of A we know: The first bit of x Whether x<N/2 or not We have only half the problem to search. (only elements with the same bit as x) –One pass of A is one step in the binary search. –Proceed to query the next bit.

20 Problem Set 1 Solutions (cont) The problem: –Is to keep track of all the elements of A that are still relevant for the search. Solution: –Use a linked list L –Each element y in L contains two fields (except for the usual pointers in a linked list ). y.pA – a pointer to an element in A y.bit – one bit of that element in A. –Run through A by running over the pointers in the linked list. –Updates the bits in the elements of L as you run (in every pass). –When the number of 0’s counted at the end of the pass, delete from the list the elements pointing to non relevant elements of A.

21 Problem Set 1 Solutions (cont) The run time of the algorithm: –Each run goes a constant number of times through the liked list Performs a constant number of operations for each element. O(n) steps. –Each step the list is shortened by half. – for some constant d. –Solution with Master theorem rule number 3: (a=1, b=2)

22 Problem Set 1 Solutions (cont) 2.d Solution –This recurrence is defined for power of 2, –Les us try to see how it behaves. Opening it up, we have: –We see that the answers are power of 2.

23 Problem Set 1 Solutions (cont) –We now have to observe that if we present n as a power of 2 as well, we get a simpler rule. –We thus can guess: –The recurrence presented in term of power of 2, is: –Let us prove by induction on k that is indeed the solution.

24 Problem Set 1 Solutions (cont) –Base case: For k=0,1 it is true because of the initialization of the recurrence. –Reduction assumption: –Reduction proof:


Download ppt "Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions."

Similar presentations


Ads by Google