chapter 8 multiway trees this chapter studies multiway
play

Chapter 8 Multiway Trees This chapter studies multiway trees. These - PowerPoint PPT Presentation

CS 2412 Data Structures Chapter 8 Multiway Trees This chapter studies multiway trees. These trees can be used for external search. When searched data is big, it is not suitable to load all the data to memory. Search in high-speed memory is


  1. CS 2412 Data Structures Chapter 8 Multiway Trees

  2. This chapter studies multiway trees. These trees can be used for external search. When searched data is big, it is not suitable to load all the data to memory. Search in high-speed memory is much faster than search in external devices (hard discs, CD etc.) The idea of external search: each time read in a block of information to the memory and decide what is the next block we should search on. Data Structure 2015 R. Wei 2

  3. Definition An m -way tree has the following properties. • Each node has 0 to m subtrees. • A node with k < m subtrees contains k subtrees and k − 1 data entries. • The keys of the data entries are ordered: key 1 ≤ key 2 ≤ · · · ≤ key k − 1 . • The key values in the first subtree (0th subtree) are all less than key 1 ; the key values in the i th subtrees are all greater than or equal to key i but less than key i +1 . • All subtrees are themselves multiway trees. A 2-way tree is a BST. Data Structure 2015 R. Wei 3

  4. Data Structure 2015 R. Wei 4

  5. Data Structure 2015 R. Wei 5

  6. We also want the multisay tree to be balance. Definition A B-tree is an m -way tree with the following additional properties: • The root is either a leaf or it has 2 to m subtrees. • All internal nodes have at least ⌈ m/ 2 ⌉ nonnull subtrees. • All leaf nodes are at the same level. • A leaf node has at least ⌈ m/ 2 ⌉ − 1 entries. Data Structure 2015 R. Wei 6

  7. Data Structure 2015 R. Wei 7

  8. Data structure of B-tree. The structure of m -way tree: an entry of a node contains data and a pointer to its right subtree. A node contains the first pointer to the subtree with entries less than the key of the first entry, a count of the number of entries currently in the node, and an array of entries. The array can be of size m . Main operations for B-trees are: insert, delete, traverse and search. Data Structure 2015 R. Wei 8

  9. Data Structure 2015 R. Wei 9

  10. B-tree insertion: B-tree insertion takes place at a leaf node. • Locate the leaf node where the data can be inserted. • If the node is not full (has less than m − 1 entries), insert the data to this node. • If the node is full (called overflow condition), split the node into two node. A B-tree grows from the bottom up. Data Structure 2015 R. Wei 10

  11. Example Insert 11 , 21 , 14 , 78 , 97 to a B-tree of order 5: Data Structure 2015 R. Wei 11

  12. The algorithm of B-tree insert: • If the B-tree is empty, then create the root and insert the first entry. • If the B-tree is not empty, call the insert node algorithm which finds the location, insert it and do necessary update (if overflow, then split and install the median entry to the parent etc.). • If the root needs to split, then create a new root. Data Structure 2015 R. Wei 12

  13. Algorithm BTreeInsert( tree, data) if (tree empty) create new node set left subtree of node to null move data to first entry in new node set subtree of first entry to null set tree root to address of new node set number of entries to 1 else insertNode(tree, data, upEntry) end if if (tree higher) create new node move upEntry to first entry in new node set left subtree of the new node to tree set tree root to new node set number of entries to 1 end if Data Structure 2015 R. Wei 13

  14. Data Structure 2015 R. Wei 14

  15. Data Structure 2015 R. Wei 15

  16. Algorithm searchNode(nodePtr, target) if (target < key in first entry) return 0 end if set walker to number of entries -1 loop (targer < entry key[walker]) decrement walker end loop return walker This function returns the index to entry with key ≤ target, or 0 if the key < first entry in node. Data Structure 2015 R. Wei 16

  17. Algorithm splitNode (node, entryNdx, newEntryLow, upEntry) create new node move high entries to new node if (entryNdx < minimum entries) inset upEntry in new node end if move median data to upEntry make new node first Ptr the right subtree of median data make new node the right subtree of upEntry Data Structure 2015 R. Wei 17

  18. Data Structure 2015 R. Wei 18

  19. Data Structure 2015 R. Wei 19

  20. Data Structure 2015 R. Wei 20

  21. Data Structure 2015 R. Wei 21

  22. B-tree deletion: B-tree deletion is a little more complicated than insertion. • Search for the data to be deleted. If can’t find, then print an error message and quit. • If the data is found, then delete the data. Two cases need to consider: the data at leaf node or non-leaf node. • If an underflow (a leaf node has less than ⌈ m/ 2 ⌉ − 1 entries or an internal node has less than ⌈ m/ 2 ⌉ nonull subtrees) occurred after the data deletion, then adjustment must be done. Data Structure 2015 R. Wei 22

  23. The following algorithm delete an entry. Some situations are considered: empty tree, the root is empty after deletion. Leave the details about how to treat underflow to algorithm delete . Algorithm BTreeDelete (tree, dltKey) if (tree empty) return false end if delete (tree, dltKey, success) if (success) if(tree number of entries zero) set tree to left subtree end if end if return success Data Structure 2015 R. Wei 23

  24. Data Structure 2015 R. Wei 24

  25. 6 end if 6 end if 7 return underflow end delete Data Structure 2015 R. Wei 25

  26. The following algorithm deletes the entry from a leaf node and returns the value of underflow. Algorithm deleteEntry (node, entryNdx) delete entry at entryNdx from node shift entries after delete to left if (number of entries less minimum, entries) return true else retrun false end if Data Structure 2015 R. Wei 26

  27. When deleting an entry in an internal node, we must find substitute data. We use the immediate predecessor, which is the largest node on the left subtree of the entry to be deleted. In the subtree, the largest node is the rightmost subtree. Algorithm deleteMid (node, entryNdx, subtree) if (no rightmost subtree) //predecessor in a leaf node move predecessor’s data to deleted entry set underflow if node entries less minimum else set underflow to deleteMid(node, entryNdx, right subtree) if (underflow) set underflow to reFlow(root, entryNdx) end if end if return underflow Data Structure 2015 R. Wei 27

  28. When a node is underflow, we need to do some adjustment which we call reflow. Suppose one of the subtree contains unerflow node, two situations need to consider: • If the other subtree has more entries than the minimum number, than we just move some entry to the underflow node, which we call it balance . • If the other subtree only has minimum number of entries, then we need to combine two node to one node together with the root entry. This is called combine . Data Structure 2015 R. Wei 28

  29. Algorithm reflow (root, entryNdx) if (rightTree entries greater minimum entries) borrowRight (root, entryNdx, leftTree, rightTree) set underflow to false else if (leftTree entries greater minimum entries) borrowLeft (root, entryNdx, leftTree, rightTree) set underflow to false else combine (root, entryNdx, leftTree, rightTree) if (root numEntries less minimum entries) set underflow to true else set underflow to false end if end if return underflow Data Structure 2015 R. Wei 29

  30. Data Structure 2015 R. Wei 30

  31. Algorithm borrowLeft(root, entryNdx, left, right) shift all elements one to the right move root data to first entry in right move right first pointer to right subtree of first entry move left last right pointer to right first pointer move left last entry data to root at entryNdx In above algorithm, when an entry is moved the according pointers are also adjusted. (To see that, consider the underflow node is not a leaf node). The algorithm of borrowRight is similar. Data Structure 2015 R. Wei 31

  32. Data Structure 2015 R. Wei 32

  33. Algorithm combine (root, entryNdx, left, right) move parent entry to first open entry in left subtree move right subtree first subtree to moved parent left subtree move entries from right subtree to end of left subtree shift root data to left Data Structure 2015 R. Wei 33

  34. Example Data Structure 2015 R. Wei 34

  35. Data Structure 2015 R. Wei 35

  36. Similar to BST, the traversal of a B-tree uses inorder. The difference is that except of leaf nodes, the data in a node is not processed at the same time. Data Structure 2015 R. Wei 36

  37. Algorithm BTreeTraversal (root) set scanCount to 0 set nextSubTree to root left subtree loop (scanCount <= number of entries) if (nextSubTree not null) BTreeTraversal (nextSubTree) end if if (ScanCount < number of entries) process (entry[scanCount]) set nextSubTree to current entry right subtree end if increment scanount end loop Data Structure 2015 R. Wei 37

  38. The B-tree search algorithm follow the similar idea of search a binary tree. But we need find the node and then find the entry in that node. In this case, we need to return both the node and the location of the entry in that node. Recursive method are used for finding the node. At the node found, compare from the last entry to the first entry. Data Structure 2015 R. Wei 38

  39. Data Structure 2015 R. Wei 39

  40. Data Structure 2015 R. Wei 40

Recommend


More recommend