90 likes | 199 Views
CSE 3358 Note Set 15. Data Structures and Algorithms. Multiway Search Tree of Order m. AKA: m-way search tree Properties Each node has m children and m – 1 keys Keys in each node are in ascending order Keys in first i children are smaller than the i-th key
E N D
CSE 3358 Note Set 15 Data Structures and Algorithms
Multiway Search Tree of Order m • AKA: m-way search tree • Properties • Each node has m children and m – 1 keys • Keys in each node are in ascending order • Keys in first i children are smaller than the i-th key • The keys in the last m – i children are larger than the i-th key
Disk Access • Not all data needed is stored in main memory during execution of a program • Think Database Systems for example • Any access to secondary storage is much (much, much, much) slower than access to main memory • Makes big-oh useless to compare one algorithms
Disk Access • Disk Access – based on block i/o • On request • position has to be located on disk • Read/write head has to be moved to that location • Data has to be read or written
B-Tree • Used with algorithms that access secondary storage • Each node • can be any size, but makes sense to make it the same size as a block on the HD • Contains a certain number of keys • Number of keys depends on the size of a key and size of a block (do the math)
B-Tree of order m • B-Tree of order M is a m-way search tree with the following properties: • The root has at least two subtrees unless it is a leaf • Each nonroot and each nonleaf node holds k – 1 keys and k pointers to subtrees where ceil(m/2) <= k <= m • Each leaf node holds k – 1 keys where cei(m/2) <= k <= m. • All leaves are on the same level. 5 19 27 45 63 m = 6
Basic Node Idea template <class T, int M> class BTreeNode { public: BTreeNode(); //Other constructors as needed private: bool leaf; intkeyTally; //# of keys currently stored in node T keys[M-1]; BTreeNode* pointers[M]; };
Inserting into a B-Tree • The possibilities • A key is placed in a leaf that still has some room • The leaf in which a key should be placed is full. • New leaf is created. • ½ keys stay in original leaf, ½ go to new leaf • Middle key is moved to the parent • (Same thing happens for non-leaf nodes) • Root is full • old root is split • middle key to new root • other keys split between old root and its new sibling