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

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 1 The ADT Binary Search Tree Definition The ADT Binary Search Tree is a.
CSC 205 – Java Programming II Lecture 35 April 17, 2002.
Binary Search Trees CSE 331 Section 2 James Daly.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
A balanced life is a prefect life.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Data Structures Topic #9. Today’s Agenda Continue Discussing Trees Examine the algorithm to insert Examine the algorithm to remove Begin discussing efficiency.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
1 Balanced Search Trees (Walls & Mirrors - Chapter 12 & end of Chapter 10)
Marc Smith and Jim Ten Eyck
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Advanced Implementation of Tables.
2-3 Trees Professor Sin-Min Lee. Contents n Introduction n The 2-3 Trees Rules n The Advantage of 2-3 Trees n Searching For an Item in a 2-3 Tree n Inserting.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Data Structures CSCI 2720 Spring 2007 Balanced Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
2-3 Trees, Trees Red-Black Trees
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Data Structures Balanced Trees CSCI 2720 Spring 2007.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
BST Trees
Binary Search Tree (BST)
Binary Search Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Data Structures and Algorithms
Data Structures Balanced Trees CSCI
Chapter 21: Binary Trees.
CS202 - Fundamental Structures of Computer Science II
Binary Trees, Binary Search Trees
Ch Balanced Search Trees
Mark Redekopp David Kempe
Trees.
CSC 205 – Java Programming II
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
COSC2007 Data Structures II
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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

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

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 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 Trees (Self study) Red-Black Trees (Self study) AVL Trees

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 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 2-3 Trees Example

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)

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 ADT 2-3 Trees Implementation Main Operations Traverse Search (Retrieve) Insert Delete

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

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 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 ADT 2-3 Trees Implementation Insertion example

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

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

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 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 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 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 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 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 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 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 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 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 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 ADT 2-3 Trees Implementation Deletion example: Delete Delete Merge Replace with successor

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 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 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 tree AVL tree

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 Review All the nodes in a binary tree are ______. single nodes 1-nodes 2-nodes double nodes

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 Review A node that contains two data items and has three children is called a ______. 2-node 3-node double node triple node

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 Review A 2-3 tree of height h always has at least ______ nodes. h 2 h 2 – 2 2 h 2 h – 1

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 Review In a 2-3 tree, a leaf may contain ______ data item(s). one one or two two two or three three

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

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 )