BalancedTrees2 - 2 ­3 Trees Defini'on: A 2...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2 ­3 Trees Defini'on: A 2 ­3 tree is a tree in which each internal node has either two or three children, and all leaves are at the same level. •  2 ­node: a node with two children •  3 ­node: a node with three children An example of a 2-3 tree → A 2 ­3 tree is not a binary tree → A 2 ­3 tree is never taller than a minimum ­height binary tree → A 2 ­3 tree with N nodes never has height greater than Ⱥ log2(N+1) Ⱥ → A 2 ­3 tree of height h always has at least 2h ­1 nodes. 4/4/11 CS202 - Fundamental Structures of Computer Science II 1 2 ­3 Trees T is a 2 ­3 tree of height h if 1.  T is empty (a 2 ­3 tree of height 0), or 2 ­node 2.  T is of the form r TL TR where r is a node that contains one data item and TL and TR are both 2 ­3 trees, each of height h ­1, or 3 ­node 3.  T is of the form r TL TM TR where r is a node that contains two data items and TL , TM and TR are 2 ­3 trees, each of height h ­1. 4/4/11 CS202 - Fundamental Structures of Computer Science II 2 2 ­3 Trees  ­ ­ Example 4/4/11 CS202 - Fundamental Structures of Computer Science II 3 C++ Class for a 2 ­3 Tree Node class TreeNode {! private:! ! !TreeItemType smallItem, largeItem;! ! !TreeNode *leftChildPtr, *midChildPtr, *rightChildPtr;! ! ! };! !// friend class-can access private class members! !friend class TwoThreeTree;! •  When a node is a 2 ­node (contains only one item) –  Place it in smallItem –  Use leftChildPtr and midChildPtr to point to the node s children –  Place NULL in rightChildPtr 4/4/11 CS202 - Fundamental Structures of Computer Science II 4 Traversing a 2 ­3 Tree •  Inorder traversal visits the nodes in a sorted search ­key order –  Leaf node: •  Visit the data item(s) –  2 ­node: •  Visit its leO subtree •  Visit the data item •  Visit its right subtree 4/4/11 –  3 ­node: •  •  •  •  •  Visit its leO subtree Visit the smaller data item Visit its middle subtree Visit the larger data item Visit its right subtree CS202 - Fundamental Structures of Computer Science II 5 Searching a 2 ­3 Tree •  Searching a 2 ­3 tree is similar to searching a binary search tree –  For a 3 ­node, compare the searched key with the two values of the 3 ­node and select one of its three subtrees according to these comparisons •  Searching a 2 ­3 tree is O(log N) –  Searching a 2 ­3 tree and the shortest BST has approximately the same efficiency. •  A binary search tree with N nodes cannot be shorter than Ⱥ log2(N+1) Ⱥ •  A 2 ­3 tree with N nodes cannot be taller than Ⱥ log2(N+1) Ⱥ 4/4/11 A 2 ­3 tree with the same elements A balanced binary search tree 6 InserCng into a 2 ­3 Tree Insert [ 39 38 37 36 35 34 33 32 ] into the trees given in the previous slide –  While we insert items into a 2 ­3 tree, its shape is maintained 4/4/11 CS202 - Fundamental Structures of Computer Science II 7 InserCng into a 2 ­3 Tree  ­ ­ Example Star_ng from the following tree, insert [ 39 38 37 36 35 34 33 32 ] Insert 39 •  Find the node into which you can put 39 4/4/11 CS202 - Fundamental Structures of Computer Science II InserCon into a 2 ­node leaf is simple 8 InserCng into a 2 ­3 Tree  ­ ­ Example Insert 38 •  Find the node into which you can put 38 InserCon into a 3 ­node causes it to divide 4/4/11 CS202 - Fundamental Structures of Computer Science II 9 InserCng into a 2 ­3 Tree  ­ ­ Example Insert 37 •  Find the node into which you can put 37 InserCon into a 2 ­node leaf is simple 4/4/11 CS202 - Fundamental Structures of Computer Science II 10 InserCng into a 2 ­3 Tree  ­ ­ Example Insert 36 •  Find the node into which you can put 36 4/4/11 InserCng into a 2 ­3 Tree  ­ ­ Example Insert 35, 34, 33 4/4/11 CS202 - Fundamental Structures of Computer Science II 2 ­3 Trees  ­ ­ InserCon Algorithm SpliLng a leaf in a 2 ­3 tree 4/4/11 CS202 - Fundamental Structures of Computer Science II 13 2 ­3 Trees  ­ ­ InserCon Algorithm SpliLng an internal node in a 2 ­3 tree 4/4/11 CS202 - Fundamental Structures of Computer Science II 14 2 ­3 Trees  ­ ­ InserCon Algorithm SpliLng the root of a 2 ­3 tree 4/4/11 CS202 - Fundamental Structures of Computer Science II 15 DeleCng from a 2 ­3 Tree •  Dele_on strategy is the inverse of inser_on strategy. •  Dele_on starts like normal BST dele_on (swap with inorder successor) •  Then, we merge the nodes that have become underloaded. 50 Delete [ 70 100 80 ] from this 2 ­3 tree 70 90 30 10 20 4/4/11 40 60 CS202 - Fundamental Structures of Computer Science II 80 100 16 DeleCng from a 2 ­3 Tree  ­ ­ Example 50 50 10 20 40 60 80 80 90 30 70 90 30 100 10 20 40 60 70 100 50 Delete 70 •  Swap with inorder successor •  Delete value from leaf 10 20 4/4/11 80 90 30 CS202 - Fundamental Structures of Computer Science II 40 60 – 100 17 DeleCng from a 2 ­3 Tree  ­ ­ Example (cont.) 50 50 80 90 30 10 20 40 60 90 30 100 10 20 40 60 80 100 50 Delete 70 •  •  •  •  Swap with inorder successor Delete value from leaf Delete the empty leaf Shrink the parent (no more mid ­pointer) 30 10 20 4/4/11 CS202 - Fundamental Structures of Computer Science II 90 40 60 80 100 18 DeleCng from a 2 ­3 Tree  ­ ­ Example 50 50 Doesn’t work 30 10 20 90 40 60 80 30 – 10 20 90 40 60 80 50 Delete 100 •  Delete value from leaf •  Distribute the children Doesn’t work •  Redistribute the parent and the children 30 10 20 4/4/11 CS202 - Fundamental Structures of Computer Science II 80 40 60 90 19 DeleCng from a 2 ­3 Tree  ­ ­ Example 50 50 30 10 20 30 90 40 60 80 10 20 90 40 60 – 50 Delete 80 Node becomes empty •  Swap with inorder successor •  Delete value from leaf •  Merge by moving 90 down and removing the empty leaf 30 10 20 CS202 - Fundamental Structures of Computer Science II – 40 60 90 20 DeleCng from a 2 ­3 Tree  ­ ­ Example (cont.) – Root becomes empty 30 50 30 50 10 20 10 20 40 40 60 90 60 90 Delete 80 Swap with inorder successor Delete value from leaf Merge by moving 90 down and removing the empty leaf Merge by moving 50 down, adop_ng empty node’s child and removing the empty node •  Remove empty root •  •  •  •  CS202 - Fundamental Structures of Computer Science II 21 2 ­3 Trees  ­ ­ DeleCon Algorithm •  To delete an item X from a 2 ­3 tree: –  –  –  –  First, we locate the node n containing X. If n is not a leaf, we find X's inorder successor and swap it with X. AOer the swap, the dele_on always begins at the leaf. If the leaf contains another item in addi_on to X, we simply delete X from that leaf, and we are done. –  If the leaf contains only X, dele_ng X would leave the leaf without a data item. In this case, we must perform some addi_onal work to complete the dele_on. •  Depending on the empty node and its siblings, we perform certain opera_ons: –  Delete empty root –  Merge nodes –  Redistribute values •  These opera_ons can be repeated all the way upto the root if necessary. 4/4/11 CS202 - Fundamental Structures of Computer Science II 22 2 ­3 Trees  ­ ­ DeleCon OperaCons DeleCng the root 4/4/11 CS202 - Fundamental Structures of Computer Science II 23 2 ­3 Trees  ­ ­ DeleCon OperaCons RedistribuCng values (and children) For a leaf For an internal node 4/4/11 CS202 - Fundamental Structures of Computer Science II 24 2 ­3 Trees  ­ ­ DeleCon OperaCons Merging For a leaf For an internal node 4/4/11 CS202 - Fundamental Structures of Computer Science II 25 2 ­3 Trees  ­ ­ Analysis •  •  •  •  4/4/11 We can use a 2 ­3 tree in the implementa_on of tables. A 2 ­3 tree has the advantage of always being balanced. Thus, inser_on and dele_on opera_ons are O(log N) Retrieval based on key is also guaranteed to O(log N) CS202 - Fundamental Structures of Computer Science II 26 2 ­3 ­4 Trees •  A 2 ­3 ­4 tree is like a 2 ­3 tree, but it allows 4 ­nodes, which are nodes that have four children and three data items. •  There is a close rela_on between 2 ­3 ­4 trees and red ­black trees. –  We will look at those a bit later •  2 ­3 ­4 trees are also known as 2 ­4 trees in other books. –  A specializa_on of M ­way tree (M=4) –  Some_mes also called 4th order B ­trees –  Variants of B ­trees are very useful in databases and file systems •  MySQL, Oracle, MS SQL all use B+ trees for indexing •  Many file systems (NTFS, Ext2FS etc.) use B+ trees for indexing metadata (file size, date etc.) •  Although a 2 ­3 ­4 tree has more efficient inser_on and dele_on opera_ons than a 2 ­3 tree, a 2 ­3 ­4 tree has greater storage requirements. 4/4/11 CS202 - Fundamental Structures of Computer Science II 27 2 ­3 ­4 Trees  ­ ­ Example 4/4/11 CS202 - Fundamental Structures of Computer Science II 28 2 ­3 ­4 Trees T is a 2 ­3 ­4 tree of height h if 1.  T is empty (a 2 ­3 ­4 tree of height 0), or 2 ­node r 2.  T is of the form TL TR where r is a node containing one data item and TL and TR are both 2 ­3 ­4 trees, each of height h ­1, or 3 ­node r 3.  T is of the form TL TM TR where r is a node containing two data items and TL , TM and TR are 2 ­3 ­4 trees, each of height h ­1, or 4.  T is of the form TL 4 ­node r TML TMR TR where r is a node containing three data items and TL , TML , TMR , and TR are 2 ­3 ­4 trees, each of height h ­1. 4/4/11 29 C++ Class for a 2 ­3 ­4 Tree Node class TreeNode {! private:! !TreeItemType smallItem, middleItem, largeItem;! !TreeNode *leftChildPtr, *lMidChildPtr;! !TreeNode *rMidChildPtr, *rightChildPtr;! !friend class TwoThreeFourTree; !! };! •  When a node is a 3 ­node (contains only two items) •  Place the items in smallItem and middleItem •  Use leftChildPtr, lMidChildPtr, rMidChildPtr to point to the node’s children •  Place NULL in rightChildPtr •  When a node is a 2 ­node (contains only one item) •  Place the item in smallItem •  Use leftChildPtr, lMidChildPtr to point to the node’s children •  Place NULL in rMidChildPtr and rightChildPtr 4/4/11 CS202 - Fundamental Structures of Computer Science II 30 2 ­3 ­4 Trees  ­ ­ OperaCons •  Searching and traversal algorithms for a 2 ­3 ­4 tree are similar to the 2 ­3 algorithms. •  For a 2 ­3 ­4 tree, inser_on and dele_on algorithms that are used for 2 ­3 trees, can similarly be used. •  But, we can also use a slightly different inser_on and dele_on algorithms for 2 ­3 ­4 trees to gain some efficiency. 4/4/11 CS202 - Fundamental Structures of Computer Science II 31 InserCng into a 2 ­3 ­4 Tree •  Splits a 4 ­nodes by moving one of its items up to its parent node. •  For a 2 ­3 tree, the inser_on algorithm traces a path from the root to a leaf and then backs up from the leaf as it splits nodes. •  To avoid this return path a=er reaching a leaf, the inser_on algorithm for a 2 ­3 ­4 tree splits 4 ­nodes as soon as it encounters them on the way down the tree from the root to a leaf. –  As a result, when a 4 ­node is split and an item is moved up to node’s parent, the parent cannot possibly be a 4 ­node and so can accommodate another item. Insert [ 20 50 40 70 80 15 90 100 ] to this 2 ­3 ­4 tree 4/4/11 CS202 - Fundamental Structures of Computer Science II 10 30 60 32 InserCng into a 2 ­3 ­4 Tree  ­ ­ Example 10 30 60 30 30 10 60 10 20 60 Insert 20 •  Root is a 4 ­node Split 4 ­nodes as they are encountered •  So, we split it before inser_on •  And, then add 20 4/4/11 CS202 - Fundamental Structures of Computer Science II 33 InserCng into a 2 ­3 ­4 Tree  ­ ­ Example 30 30 10 20 60 10 20 50 60 30 Insert 50 and 40 •  No 4 ­nodes have been encountered during their inser_on No split operaCon 4/4/11 10 20 CS202 - Fundamental Structures of Computer Science II 40 50 60 34 InserCng into a 2 ­3 ­4 Tree  ­ ­ Example 30 50 30 10 20 40 50 60 10 20 40 60 30 50 Insert 70 •  A 4 ­node is encountered 10 20 40 60 70 •  So, we split it before inser_on •  And, then add 70 4/4/11 CS202 - Fundamental Structures of Computer Science II 35 InserCng into a 2 ­3 ­4 Tree  ­ ­ Example 30 50 30 50 10 20 40 60 70 10 20 40 60 70 80 30 50 Insert 80 and 15 •  No 4 ­nodes have been encountered during their inser_on 10 15 20 40 60 70 80 No split operaCon 4/4/11 CS202 - Fundamental Structures of Computer Science II 36 InserCng into a 2 ­3 ­4 Tree  ­ ­ Example 30 50 10 15 20 40 30 50 70 60 70 80 10 15 20 40 60 80 30 50 70 Insert 90 •  A 4 ­node is encountered 10 15 20 40 60 80 90 •  So, we split it before inser_on •  And, then add 90 4/4/11 CS202 - Fundamental Structures of Computer Science II 37 InserCng into a 2 ­3 ­4 Tree  ­ ­ Example 30 50 70 10 15 20 40 60 50 80 90 30 10 15 20 70 40 Insert 100 60 80 90 50 •  A 4 ­node is encountered •  So, we split it before inser_on 30 70 •  And, then add 100 10 15 20 4/4/11 CS202 - Fundamental Structures of Computer Science II 40 60 80 90 100 38 SpliLng 4 ­nodes during inserCon •  We split each 4 ­node as soon as we encounter it during our search from the root to a leaf that will accommodate the new item to be inserted. •  The 4 ­node which will be split can: –  be the root, or –  have a 2 ­node parent, or –  have a 3 ­node parent. 4/4/11 CS202 - Fundamental Structures of Computer Science II 39 SpliLng 4 ­nodes during inserCon SpliLng a 4 ­node root 4/4/11 CS202 - Fundamental Structures of Computer Science II 40 SpliLng 4 ­nodes during inserCon SpliLng a 4 ­node whose parent is a 2 ­node 4/4/11 CS202 - Fundamental Structures of Computer Science II 41 SpliLng 4 ­nodes during inserCon SpliLng a 4 ­node whose parent is a 3 ­node DeleCng from a 2 ­3 ­4 tree •  For a 2 ­3 tree, the dele_on algorithm traces a path from the root to a leaf and then backs up from the leaf, fixing empty nodes on the path back up to root. •  To avoid this return path a=er reaching a leaf, the dele_on algorithm for a 2 ­3 ­4 tree transforms each 2 ­node into either 3 ­node or 4 ­node as soon as it encounters them on the way down the tree from the root to a leaf. –  If an adjacent sibling is a 3 ­node or 4 ­node, transfer an item from that sibling to our 2 ­node. –  If adjacent sibling is a 2 ­node, merge them. 4/4/11 CS202 - Fundamental Structures of Computer Science II 43 Red ­Black Trees •  A 2 ­3 ­4 tree requires more storage than a binary search tree. •  A special binary search tree, the red ­black ­tree, can be used to represent a 2 ­3 ­4 tree, so that we can retain advantages of a 2 ­3 ­4 tree without a storage overhead. –  3 ­node and 4 ­nodes in a 2 ­3 ­4 tree are represented by a binary tree. –  To dis_nguish the original 2 ­nodes from 2 ­nodes that are generated from 3 ­ nodes and 4 ­nodes, we use red and black pointers. –  All original pointers in a 2 ­3 ­4 tree are black pointers, red pointers are used for child pointers to link 2 ­nodes that result from the split of 3 ­nodes and 4 ­nodes. 4/4/11 CS202 - Fundamental Structures of Computer Science II 44 Red ­Black Trees Red ­black tree representaCon For a 4 ­node For a 3 ­node 4/4/11 CS202 - Fundamental Structures of Computer Science II 45 Red ­Black Trees  ­ ­ ProperCes •  Root is always a black node. •  The children of a red node (pointed by a red pointer) are always black nodes (pointed by a black pointer) •  All external nodes (leaves and nodes with a single child) should have the same number of black pointers on the path from the root to that external node. 4/4/11 CS202 - Fundamental Structures of Computer Science II 46 A 2 ­3 ­4 Tree and Its Corresponding Red ­Black Tree 37 50 30 35 10 20 70 90 39 32 33 34 36 38 40 60 80 100 37 50 30 20 10 32 4/4/11 36 33 34 90 39 35 38 40 100 70 60 80 47 C++ Class for a Red ­Black Tree Node enum Color {RED, BLACK};! class TreeNode {! private:! ! !TreeItemType Item;! ! !TreeNode *leftChildPtr, *rightChildPtr;! ! !Color leftColor, rightColor;! ! !friend class RedBlackTree;! };! 4/4/11 CS202 - Fundamental Structures of Computer Science II 48 SpliLng in a Red ­Black Tree RepresentaCon For a 4 ­node that is the root 4/4/11 CS202 - Fundamental Structures of Computer Science II 49 SpliLng in a Red ­Black Tree RepresentaCon For a 4 ­node whose parent is a 2 ­node 4/4/11 CS202 - Fundamental Structures of Computer Science II 50 SpliLng in a Red ­Black Tree RepresentaCon For a 4 ­node whose parent is a 3 ­node 4/4/11 51 SpliLng in a Red ­Black Tree RepresentaCon For a 4 ­node whose parent is a 3 ­node 4/4/11 SpliLng in a Red ­Black Tree RepresentaCon For a 4 ­node whose parent is a 3 ­node 4/4/11 53 ...
View Full Document

Ask a homework question - tutors are online