B+ Tree and Hashing
B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers
B+ Tree Properties Balanced Tree Same height for paths from root to leaf Given a search-key K, nearly same access time for different K values B+ Tree is constructed by parameter n Each Node (except root) has n/ to n pointers Each Node (except root) has n/- to n- search-key values Case for n=3 K K P P P 3 B+ Tree Properties General case for n P K K K n- P P n- P n
B+ Tree Properties Tutorial 8. Search keys are sorted in order K < K < <K n- Non-leaf Node Each key-search values in subtree S i pointed by P i < K i, >=K i- Key values in S < K K <= Key values in S < K Leaf Node P i points record or bucket with search key value K i P n points to the neighbor leaf node P K K P P P 3 S S S 3 K K P Record of K Record of K Record of K P 3
B+ Tree Searching Tutorial 8. Given a search-value k Start from the root, look for the largest searchkey value (K l ) in the node <= k Follow pointer P l+ to next level, until reach a leaf node K l <=k<k l+ K K K l K l+ K n- P P P 3 P n- P n P l+ If k is found to be equal to K l in the leaf, follow P l to search the record or bucket K l K l+ Record of K l Record of K l P l k = K l
B+ Tree Insertion Tutorial 8.3 Overflow When number of search-key values exceed n- 7 9 3 5 Insert 8 Leaf Node Split into two nodes: st node contains (n-)/ values nd node contains remaining values Copy the smallest search-key value of the nd node to parent node 9 7 8 9 3 5
B+ Tree Insertion Tutorial 8.3 Overflow When number of search-key values exceed n- 7 9 3 5 Insert 8 Non-Leaf Node Split into two nodes: st node contains n/ - values Move the smallest of the remaining values, together with pointer, to the parent nd node contains the remaining values 9 7 8 3 5
B+ Tree Insertion Tutorial 8.3 Example : Construct a B + tree for (, 4, 7, 0, 7,, 3, 5, 9, 0, 8, 4) with n=4. 7 4 7 4 7 0 7 7 4 7 0 7
B+ Tree Insertion, 4, 7, 0, 7,, 3, 5, 9, 0, 8, 4 7 7 5 4 7 0 7 5 3 7 7 0 5 4 7 0 7 9 0 5 3
B+ Tree Insertion Tutorial 8.3, 4, 7, 0, 7,, 3, 5, 9, 0, 8, 4 7 7 0 5 3 4 7 9 3 4 7 0 0 5 8
B+ Tree Insertion Tutorial 8.3 Example : n=3, insert 4 into the following B+Tree 9 0 7 8 Subtree C Subtree D 5 Leaf A Leaf B 9 7 0 4 8 C D 4 5 A B
B+ Tree Deletion Tutorial 8.4 Underflow When number of search-key values < n/- Leaf Node Redistribute to sibling Right node not less than left node Replace the between-value in parent by their smallest value of the right node Merge (contain too few entries) Move all values, pointers to left node Remove the between-value in parent 9 0 Delete 0 3 8 9 0 3 4 4 8 9 3 4 6 6 3 8 8 9 0 3 4 9 3 4
B+ Tree Deletion Tutorial 8.4 Non-Leaf Node Redistribute to sibling Through parent Right node not less than left node Merge (contain too few entries) Bring down parent Move all values, pointers to left node Delete the right node, and pointers in parent 9 0 Delete 0 3 8 9 0 4 5 4 8 9 3 5 6 6 3 8 8 9 0 4 6 9 3 4 6
B+ Tree Deletion Tutorial 8.4 Example 3: n=3, delete 3 5 0 3 8 Subtree A 3 0 5 8 Subtree A
B+ Tree Deletion Tutorial 8.4 Example 4: Delete 8, 3,, 5, 9 0 7 7 5 3 50 4 7 0 7 9 0 5 8 3 4 0 7 7 5 50 4 7 0 7 9 0 5 3 4
B+ Tree Deletion Tutorial 8.4 Example 4: Delete 8, 3,, 5, 9 7 7 0 50 4 7 0 7 9 0 5 4 7 7 50 4 7 0 7 0 4
Static Hashing Tutorial 8.5 A hash function h maps a search-key value K to an address of a bucket Commonly used hash function hash value mod n B where n B is the no. of buckets E.g. h(brighton) = (+8+9+7+8+0+5+4) mod 0 = 93 mod 0 = 3 No. of buckets = 0 Hash function h Brighton Round Hill A-7 A-305.. 750 350..
Extendable Hashing Tutorial 8.6 Hash prefix i i Length of common hash prefix bucket Data bucket i bucket Bucket address table i3 bucket3 Hash function returns b bits Only the prefix i bits are used to hash the item There are i entries in the bucket address table Let i j be the length of the common hash prefix for data bucket j, there is (i-i j ) entries in bucket address table points to j
Extendable Hashing Tutorial 8.6 Splitting (Case i j =i) Only one entry in bucket address table points to data bucket j i++; split data bucket j to j, z; i j =i z =i; rehash all items previously in j; 3 3 3 00 0 0 000 00 00 0 00 0 0
Extendable Hashing Tutorial 8.6 Splitting (Case i j < i) More than one entry in bucket address table point to data bucket j split data bucket j to j, z; i j = i z = i j +; Adjust the pointers previously point to j to j and z; rehash all items previously in j; 000 00 00 0 00 0 0 3 000 00 00 0 00 0 0 3
Extendable Hashing Tutorial 8.6 Example 5: Suppose the hash function is h(x) = x mod 8 and each bucket can hold at most two records. Show the extendable hash structure after inserting, 4, 5, 7, 8,, 0. 4 5 7 8 0 00 00 0 000 00 00 0 0 0 4 4 5 00 0 0 8 4 5 7
Extendable Hashing Tutorial 8.6 00 0 0 inserting, 4, 5, 7, 8,, 0 8 4 5 4 5 7 8 0 00 00 0 000 00 00 000 00 00 0 00 0 0 3 8 3 4 0 3 5 7 7
96-97 Final Q9. Tutorial 8.7 00 0 0 8 4 5 7 Suppose the hash function h(x) =x mod 8, each bucket can hold at most records. Show the structure after inserting 0
96-97 Final Q9. Tutorial 8.7 000 00 00 0 00 0 0 3 8 3 4 0 3 5 7