Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

An Introduction to Programming

though C++
Abhiram G. Ranade
Ch. 24: Structural Recursion
(Part 1)
B tree: generalization of 2-3 tree
• Each node v holds r value v1,..,vr, in all nodes except the
root and d ≤ r ≤ 2d.
• Each node also has r+1 pointers p0,...,pr.
• It is useful to think of a node being described by the a
descriptor p0,v1,...vr,pr.
• Note that the vi is larger than all values under pi-1 and
smaller than all values under pi.

2-3 tree: d=1.


Searching for a key x
• Examine keys stored in a node, if x is present, return.
• If x < v1 search for x in p0
• Else if x < v2 search for x in p1
• ...
• Else search in pr.

Time
• at most 2d comparisons at each node
• Path length at most logdn
• So O(d logdn)
• Number of comparisons at each node can be made O(log d) by using
binary search.
Inserting x into a B-tree
1. If tree is empty, create a new node with signature NULL,x,NULL. Return.
2. Find v = leaf reached by search.
3. place x in the sorted order of the values in v.
4. Create the new signature in v. (Basically add a pointer)
5. while v has more than 2d values{ //v=p0v1p1...p2dv2d+1p2d+1
6. Let w be the parent of v, with p in w the pointer to v.
7. If v is the root, create w and make it contain just one pointer p to v.
8. split v into v', v'' with v’ = p0,v1,..vd,pd and
9. v’’ = pd+1,vd+2,...,v2d+1,p2d+1 into v’’
10. replace p in the signature of w by &v’, vd+1, &v’’
11. v = w
12. }
Analysis
• For finding, O(d) time will be spent at each
level of the tree.
• In the loop, we will move up, and spend O(d)
at each level.
• We can only move up all the way to the root.
• So total time = O(d*height) = O(height).
• Note that height < logd n, where n= number of
keys.
Deletion
• Can also be done in O(log n) time.
• Lots of cases.
• Omitted from this course.
Trees stored on disk
• May be needed if tree is very large
• Disk can also be considered to consist of memory elements with addresses
0, 1, ..., S-1.
• Fetching m consecutive words requires moving the disk head to the correct
position on the disk (mechanical movement), and then reading is fast.
– Time = A + O(m), where A is very large.
• Time to fetch single tree node: A + O(d)
• Time for a find operation:
– Disk fetch time: logdn (A + O(d))
– Processing time: O(dlogdn), or O(log d logdn) with binary search
– Total O(logdn(A+d))
– Minimum when d=O(A). The precise value will depend on all the constants.
• B trees with large d are indeed used for storing search trees on disks.

You might also like