Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.

Similar presentations


Presentation on theme: "COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I."— Presentation transcript:

1 COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I

2 2 Topics Introduction & terminology 2-3 trees Traversal Search Insertion Deletion

3 3 Height of Binary Search Tree Tom Jane Bob Alan Wendy Nancy Ellen Unbalanced BST Tom Nancy Alan Bob Ellen Jane Wend y Skewed BST Jane Nancy Bob Ellen Tom Balanced BST Jane Tom Bob Alan Ellen Wendy Nancy Full & Complete BST

4 4 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

5 5 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 A 2-3 Tree with 2 Subtrees root A 2-3 Tree with 3 Subtrees root TLTL TRTR TLTL TMTM TRTR

6 6 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 (T L and T R ) of height (h-1) Key in r > each key in T L Key in r < each search key in T R Or T is of the form of a root r containing 2 data items and & three 2-3 trees (T L, T M and T R ) of height (h-1). Smaller key in r > each key in T L Smaller key in r < each key in T M Larger key in r > each key in T M Larger key in r < each key in T R

7 7 2-3 Trees Example 50 90 30 40 100 110130 140 120 150 70 160 806010 20

8 8 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) 10 2060 8040 90 30 50 100

9 9 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 10 2060 8040 90 30 50 100

10 10 ADT 2-3 Trees Implementation Main Operations Traverse Search (Retrieve) Insert Delete 10 2060 8040 90 30 50 100

11 11 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

12 12 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 10 2060 8040 90 30 50 100

13 13 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

14 14 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

15 15 ADT 2-3 Trees Implementation Insertion example

16 16 ADT 2-3 Trees Implementation Insertion has three special cases …..

17 17 ADT 2-3 Trees Implementation Splitting a leaf node P L S P S M L M

18 18 ADT 2-3 Trees Implementation Splitting an internal node S M L A BC D P A BC D P M P’s parent S L

19 19 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 S M L A BC D root A BC D M New root S L

20 20 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 )

21 21 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) elseLet 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)

22 22 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

23 23 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 B A B C C A I I

24 24 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 A B A B I I I

25 25 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 A B A B I I I

26 26 ADT 2-3 Trees Implementation Deletion If the parent has a sibling with two values, redistribute the value B A B C C A W X Y Z W X Y z

27 27 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 A B A B X Y Z X Y Z

28 28 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 X Y Z A B X Y Z

29 29 ADT 2-3 Trees Implementation Deletion example: Delete 70 --10 206040 80 90 30 50 100 Delete 10 2060 8040 90 30 50 100 Merge 8010 206040 70 90 30 50 1007010 206040 80 90 30 50 100 Replace with successor

30 30 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

31 31 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

32 32 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

33 33 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

34 34 Review All the nodes in a binary tree are ______. single nodes 1-nodes 2-nodes double nodes

35 35 Review A node that contains one data item and has two children is called a ______. 1-node 2-node single node double node

36 36 Review A node that contains two data items and has three children is called a ______. 2-node 3-node double node triple node

37 37 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

38 38 Review A 2-3 tree of height h always has at least ______ nodes. h 2 h 2 – 2 2 h 2 h – 1

39 39 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

40 40 Review In a 2-3 tree, a leaf may contain ______ data item(s). one one or two two two or three three

41 41 Review Searching a 2-3 tree is ______. O(n) O(log 2 n) O(log 2 n * n) O(n 2 )

42 42 Review A 2-3 implementation of a table is ______ for all table operations. O(n) O(log 2 n) O(log 2 n * n) O(n 2 )


Download ppt "COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I."

Similar presentations


Ads by Google