代做Assignment 3: B-Tree Layer代做数据库编程

Assignment 3: B-Tree Layer

Introduction

In this assignment, you must implement the missing pieces of the B-Tree layer including the logic for insert, delete, and balance operations in a B-tree data structure. This assignment tests your knowledge of the B-Tree data structures and algorithms, level up your system design skills, and get a feel for how things work under the hood in a database. When you conclude this assignment, you will better understand how the B-Tree layer allows its upper layer (VDBE) to execute SQL operations by:

● Creating tables and indexes

● Inserting, searching, and deleting key-data pairs in O(log n) time

● Traversing through key-data pairs in the order of the keys

● Starting transactions

Task 1: Complete B-Tree Balance Function

This task aims to help you understand how a B-Tree-inspired data storage engine works under the hood. B-Trees are designed to keep their height low, even as they grow. A balancing function ensures that the tree remains balanced after inserts and deletes, so search, insert, and delete operations run in O(log n) time. In this part, you have to finish the balance functions in the B-Tree layer. These functions are responsible for balancing the file structure. Without balance, the tree could become skewed (like a linked list), slowing operations and defeating the purpose of using a B-Tree.

What is a B-Tree?

A. B-Tree Definition

B-Tree is a self-balancing search tree in which each node can have more than one element and more than two children. It is a generalized form. of a binary search tree. A B-Tree keeps data sorted and allows searches, insertions, and deletion in logarithmic time:  O(log n).

In a traditional binary search tree, we only have 1 element stored inside a node. This means that each node has at most 2 child pointers (right and left pointers), as shown in Figure 1.

Figure 1

On the other hand, a B-Tree allows more than one element to be inserted inside a single node. This results in nodes having more than two child pointers.

The Figure 2 below shows the structure of a B-Tree. Each internal node with k elements has k+1 child pointers. The elements inside each node are also sorted.

 

Figure 2: Example of B-tree (source)

As mentioned before, the time complexity of searches, insertions, and deletions on a B-Tree is O(log n), where n is the number of elements in the tree. This time is not affected by the maximum number of elements we are allowed to insert in each node, normally denoted as m. This is the same complexity as the operations on a self-balancing binary search tree.

B. Tree in File Structure Design

At first glance, B-Trees do not provide much benefit compared to a binary search tree. However, creating a search tree with multiple elements in 1 node becomes relevant when designing memory-efficient file structures.

In CapybaraDB, all the data in a database is stored within one file. The file is split into multiple sections of equal sizes in bytes called pages. A majority of the pages are linked into search-trees, such that each node in the tree is represented as a page, named node page.

The major difference between B-Trees in definitions and B-Trees in file format is the size limiting factor. In most definitions of the B-Tree, a constant value m denotes the order of a B-Tree. In other words, itt determines how many elements can be placed in a node and when to split the node for balancing purposes. In file format, we are limited by a constant page size (e.g., 1 KB). We determine when to split based on how much free space there is on a page.

TODOs

The codebase (file btree_balance.cc) contains several "TODO" comments to guide you. These markers indicate the locations where you need to insert the implementation logic to balance the B-Tree. Pay close attention to these "TODOs" as they directly correspond to the following key functions:

● B-Tree::Balance():

○ This method is responsible for rebalancing the B-tree after insertions or deletions. The "TODO" within this function will require you to complete some logic for redistributing cells across pages when a node becomes overfull. This may include identifying sibling pages, splitting or merging nodes, updating parent pointers, and ensuring the cursor remains correctly positioned after the rebalancing process. 

● B-Tree::BalanceHelperHandleRoot():

○ This helper method is responsible for handling balance operations specifically on the root page of the B-tree, which requires special treatment. The "TODO" within this function will require you to implement logic that preserves the root’s identity while restructuring its contents. This may include copying data to a child node, clearing the root page, adjusting parent pointers, and determining whether the balancing operation can return early. 

● B-Tree::BalanceHelperFindChildIdx():

○ This helper method is responsible for checking which cell in the parent node points at the current page node. The "TODO" within this function will require you to implement logic that finds the cell index in the node page that points to the current node page.

Task 2: Complete B-Tree Insertion and Deletion Functions

In this task, your goal is to implement the logic behind insertion and deletion which are the key operations that B-Tree needs to support. These functions rely heavily on cursor navigation, page manipulation, overflow handling, and the balance function implemented in Task 1. Your implementation must ensure correctness even in edge cases — such as replacing an existing key or deleting from internal or leaf nodes. This task will deepen your understanding of how B-Trees maintain structure and correctness as data is added and removed.

TODOs

The codebase  (file btree_bt_cursor_public.cc)  contains several "TODO" comments to guide you. These markers indicate the locations where you need to insert the implementation logic. Pay close attention to these "TODOs" as they directly correspond to the following key functions:

● B-Tree::B-TreeInsert():

○ This method is responsible for inserting a key-data pair into the B-tree. The "TODO" within this function will require you to complete logic for locating the correct position for insertion, handling overflow pages when keys or data are too large, replacing existing entries with the same key, and calling the balance function to maintain the tree structure. You will also need to ensure that the cursor correctly reflects the new insertion point.

● B-Tree::B-TreeDelete():

○ This method is responsible for deleting the key-data pair that the cursor is currently pointing to. The "TODO" within this function will require you to implement logic to remove the entry from the page, handle overflow cleanup, and distinguish between deletion in internal vs. leaf nodes. You will also need to invoke the balancing function afterward to ensure the B-tree maintains its structural properties following the deletion.

Submission

For this assignment, you are required to submit your work via Autolab. Please submit a .tar file that contains only the following two files:

● btree_balance.cc

● btree_bt_cursor_public.cc




热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图