420 likes | 621 Views
COSC 2007 Data Structures II. Chapter 13 Advanced Implementation of Tables I. Topics. Introduction & terminology 2-3 trees Traversal Search Insertion Deletion. Jane. Tom. Balanced BST. Unbalanced BST. Alan. Bob. Nancy. Jane. Wendy. Bob. Ellen. Ellen. Tom. Bob. Nancy.
E N D
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I
Topics • Introduction & terminology • 2-3 trees • Traversal • Search • Insertion • Deletion
Jane Tom Balanced BST Unbalanced BST Alan Bob Nancy Jane Wendy Bob Ellen Ellen Tom Bob Nancy Ellen Alan Ellen Skewed BST Jane Full & Complete BST Jane Nancy Bob Tom Tom Alan Ellen Nancy Wendy Wendy Height of Binary Search Tree
Introduction & Terminology • The height of a BST is sensitive to the order of insertion & deletion of its nodes • BSTs can loose their balance and approach a linear shape • Variations of the basic BST are used to absorb insertion & deletion while preserving their balance • Searching in these trees is almost as efficient as with the minimum-height BST • BST Variations • 2-3 Trees • 2-3-4 Trees (Self study) • Red-Black Trees (Self study) • AVL Trees
root root TL TR TL TM TR A 2-3 Tree with 3 Subtrees A 2-3 Tree with 2 Subtrees 2-3 Trees • A 2-3 tree: • is a tree in which each internal node has either 2 or 3 children, and all leaves are at the same level • 2-node: • A node with two children • 3-node: • A node with three children
2-3 Trees • A 2-3 tree T of height h (Recursive definition) • Either T is empty (h = 0) • Or T is of the form of a root r containing 1 data item and two 2-3 trees (TL and TR) of height (h-1) • Key in r > each key in TL • Key in r < each search key in TR • Or T is of the form of a root r containing 2 data items and & three 2-3 trees (TL , TM and TR) of height (h-1). • Smaller key in r > each key in TL • Smaller key in r < each key in TM • Larger key in r > each key in TM • Larger key in r < each key in TR
50 90 120 150 20 70 100 110 130 140 160 10 30 40 60 80 2-3 Trees • Example
50 90 30 10 20 40 60 80 100 2-3 Trees • Observations • Binary tree? Balanced? • A 2-3 tree of height h has at least many nodes as a full binary tree of height h • Height is less than a BST having the same number of elements (n nodes)
50 90 30 10 20 40 60 80 100 ADT 2-3 Trees Implementation • Node Implementation class Tree23Node { private KeyedItem smallItem; private KeyedItem largeItem; private Tree23Node leftChild, private Tree23Node midChild; private Tree23Node rightChild; // constructor and method } // end Tree23Node • When a node contains only one item • Place it in SmallItem • Use leftChild & midChild to point to the node's children • Assign null to rightChild
50 90 30 10 20 40 60 80 100 ADT 2-3 Trees Implementation • Main Operations • Traverse • Search (Retrieve) • Insert • Delete
ADT 2-3 Trees Implementation • Traverse • In sorted search-key • Analog to inorder traversal inorder(ttTree) // Traverse nonempty 2-3 tree T in sorted search-key order if (ttTree’s root node r is a leaf) visit data item(s) else if (r has two data items) { inorder (left subtree of ttTree’s root) Visit the first data item inorder middle subtree of ttTree’s root) Visit the second data item inorder (right subtree of ttTree’s root) } else // r has one data item { inorder(left subtree of ttTree’s root) Visit the data item inorder (middle subtree of ttTree’s root) } // end if
50 90 30 10 20 40 60 80 100 ADT 2-3 Trees Implementation • Retrieve • Analog to retrieval operations in BSTs • Searching is more efficient than in BST, why? • More comparisons are needed than in BST
ADT 2-3 Trees Implementation • Retrieve retrieveItem(ttTree, searchKey) { if (SsarchKey is in ttTree’s root node r) // Item found treeItem = the data portion of r else if (r is a leaf) // failure treeItem = null; else if (r has 2 data items){ // Search appropriate subtree if (searchKey < smaller search key of r) treeItem=retrieveItem( r’s left subtree, searchKey) else if (SearchKey < larger search key of R) treeItem= retieveItem( r’s middle subtree, searchKey) else treeItem=retieveItem( r’s right subtree, searchKey) } else // r has one data item { if (searckKey < r’s search key) treeItem= retrieveItem(r’s left subtree, searchKey) else treeItem= retrieveItem(r’s middle subtree, searchKey) } //end if-else
ADT 2-3 Trees Implementation • Insertion • Doesn't degenerate the 2-3 tree as in BST • Idea: • When a leaf contains 3 items, split it into 2 nodes • When an internal node contains 3 items, split it into 2 nodes and accommodate its children • Splitting & moving items up to parent continues recursively until a node is reached that had only 1 item before the insertion • Insertion doesn’t increase the height of the tree, as long as there is at least one node containing only one item in the path from the root to the leaf into which the new item is inserted • If root contains 3 items, split it into 2 nodes & create a new root node • Height will increase
ADT 2-3 Trees Implementation • Insertion example
ADT 2-3 Trees Implementation • Insertion has three special cases …..
P P M S M L L S ADT 2-3 Trees Implementation • Splitting a leaf node
ADT 2-3 Trees Implementation • Splitting an internal node P’s parent M P P S L S M L A B C D A B C D
ADT 2-3 Trees Implementation • Splitting the root node • When the root contains 3 items • Split it into 2 nodes • Create a new root node New root root M S M L S L A B C D A B C D
ADT 2-3 Trees Implementation • Insertion insertItem(ttTree, newItem) // Let sKey be the search key of newItem Locate the leaf leafNode in which sKey belongs Add newItem to leafNode if (leafNode now has 3 items) Split (leafNode )
ADT 2-3 Trees Implementation • Insertion split (Tree23Node n) // Splits node n, which contains 3 items. if(n is the root) Create a new node p (refine later to set success) else Let p be the parent of n Replace n with 2 nodes, n1 & n2 so that p is their parent Give n1 the item in n with smallest search-key value Give n2 the item in n with largest search-key value if(n isn’t a leaf) { n1 becomes parent of n’s two leftmost children n2 becomes parent of n’s two rightmost children } Move item in n that has the middle search-key value up to p if (p now has three items) split (p)
ADT 2-3 Trees Implementation • Deletion • Strategy is the inverse of the insertion • Merge nodes when they become empty • Swap the value deleted with its inorder successor • Deletion will always then delete from a leaf • Steps • To delete x from a 2-3 tree, first locate n, the node containing x • If n is an interior node, find x’s in-order successor and swap it with x • As a result, deletion always begins at a leaf node I • If I contains a value in addition to x, delete x from I and we are done
ADT 2-3 Trees Implementation • Deletion • If deleting x from I leaves I empty, move work must be done • Check sibling of now empty leaf • If sibling has two values, redistribute the value C B A B A C I I
ADT 2-3 Trees Implementation • If deleting x from I leaves I empty, move work must be done • Check sibling of now empty leaf • If no sibling of I has two values, merge I with an adjacent sibling and bring down a value from I’s parent • The merging of I may cause the parent to be left without a value and only one child B A A B I I I
ADT 2-3 Trees Implementation • If deleting x from I leaves I empty, move work must be done • Check sibling of now empty leaf • The merging of I may cause the parent to be left without a value and only one child • If so, recursively apply deletion procedure to the parent B A A B I I I
ADT 2-3 Trees Implementation • Deletion • If the parent has a sibling with two values, redistribute the value C B A B A C Z W X Y z W X Y
ADT 2-3 Trees Implementation • Deletion • If the parent has no sibling with two values, merge the parent with a sibling, and let the sibling adopt the parent’s child B A A B Z X Y Z X Y
ADT 2-3 Trees Implementation • Deletion • If the merging continues so that the root of the tree is without a value (and has only one child), delete the root. Height will now be h-1 A B A B X Y Z X Y Z
50 50 50 Replace with successor 30 30 30 80 90 80 90 70 90 10 20 10 20 10 20 40 40 40 60 60 60 70 -- 80 100 100 100 50 Merge Delete 90 30 10 20 40 60 80 100 ADT 2-3 Trees Implementation • Deletion example: • Delete 70
ADT 2-3 Trees Implementation • Implementation of deletion deleteItem( searchKey) Attempt to locate item I with Searck Key equals searchKey if (theItem is present){ if (theItem is not a leaf) Swap item theItem with its inorder successor in leaf leafNode Delete Item theItem from leaf leafNode if (leafNode now has no items) fix(leafNode) } return true } Else return false
ADT 2-3 Trees Implementation • Deletion fix(Tree23Node n) if (n is the root) Remove the root else { Let p be the parent of n if (some sibling of n has two items) { Distribute items appropriately among n, sibling, & p if (n is internal) Move appropriate child from sibling to n } else // merge the node { Choose an adjacent sibling s of n Bring the appropriate item down from p into s if (n is internal) Move n’s child to s Remove node n if (p is now empty) fix(p) } // end if } // end if
Review • A(n) ______ is a tree in which each internal node has either two or three children, and all leaves are at the same level. • red-black tree • 2-3 tree • 2-3-4 tree • AVL tree
Review • In a 2-3 tree, ______. • all internal nodes have 2 children • all internal nodes have 3 children • all leaves are at the same level • all nodes have 2 data items
Review • All the nodes in a binary tree are ______. • single nodes • 1-nodes • 2-nodes • double nodes
Review • A node that contains one data item and has two children is called a ______. • 1-node • 2-node • single node • double node
Review • A node that contains two data items and has three children is called a ______. • 2-node • 3-node • double node • triple node
Review • If a particular 2-3 tree does NOT contain 3-nodes, it is like a ______. • general tree • binary search tree • full binary tree • complete binary tree
Review • A 2-3 tree of height h always has at least ______ nodes. • h2 • h2 – 2 • 2h • 2h – 1
Review • In a 3-node, ______. • the left child has the largest search key • the middle child has smallest search key • the middle child has the largest search key • the right child has the largest search key
Review • In a 2-3 tree, a leaf may contain ______ data item(s). • one • one or two • two • two or three • three
Review • Searching a 2-3 tree is ______. • O(n) • O(log2n) • O(log2n * n) • O(n2)
Review • A 2-3 implementation of a table is ______ for all table operations. • O(n) • O(log2n) • O(log2n * n) • O(n2)