300 likes | 412 Views
Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees. Data Structures. Haim Kaplan and Uri Zwick November 2012. Dictionaries/Dynamic sets. Maintain a set of items . Each item has key and info fields.
E N D
Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees Data Structures Haim Kaplan and Uri ZwickNovember 2012
Dictionaries/Dynamic sets • Maintain a set of items. • Each item has key and info fields. • Keys belong to a totally ordered universe, and can be compared with each other • Support the following operations: Insert, Delete, Search, Min, Max, … Extremely useful data structure!
Abstract Data Type: Dictionaries Dic-Item(k,i) – Create a dictionary item containing keyk andinfoi, Key(I), Info(I) – The keyand info contained in Dic-ItemI. • Dictionary() – Create an emptydictionary • Insert(D,I) – Insert Iinto D • Delete(D,I) – Delete Ifrom D (assuming Iis in D) • Search(D,k) – Find a dic-item with key k in D, if any. • Min(D) – Return the dic-item with the minimum key in D. • Max(D) – Return the dic-item with the maximum key in D. • Successor(D,I) – Return the successor of Iin D. • Predecessor(D,I) – Return the predecessor of Iin D. Assume that dic-items have distinct keys
Implementing dictionaries using lists • Store the dic-items in a list (in no particular order). • Insert a new dic-item to an arbitrary position of the list, e.g., the first or last position. • Delete a dic-item by either using a supplied pointer to it, or by first locating it in the list. • Search, and other operations, are implemented by scanning the list.
Implementing dictionaries using doubly linked lists (ver. 1) • Store the dic-items in a list, in no particular order. • Insert a new dic-item to an arbitrary position of the list, e.g., the first or last position. • Delete a dic-item using a supplied pointer to it. • Search, and other operations, are implemented by scanning the list. Insert, Delete – O(1) time Other operations – O(n) time
Implementing dictionaries using doubly linked lists (ver. 2) • Store the dic-items in a list, in increasing order of keys. • Insert a new dic-item to the appropriate position • Delete a dic-item by using a supplied pointer to it. • Search is implemented by scanning the list, stopping when the key of the current item is larger than the key sought. Insert,Search – O(n) time (or O(n/2) “on average”) Delete – O(1) time Min, Max, Successor, Predecessor – O(1) time
Implementing dictionaries using (circular) arrays • Store the dic-items in a list, in increasing order of keys. • Insert a new dic-item to the appropriate position • Delete a dic-item by using a supplied pointer to it. • Search implemented using binary search. Insert, Delete– O(n) time (or O(n/2) ) Min, Max, Successor, Predecessor – O(1) time Search – O(log n)
Binary search Successful search:Search(38) high mid low 10 25 38 47 56 67 73 84 95 0 1 2 3 4 5 6 7 8
Binary search Unsuccessful search:Search(39) high mid low 10 25 38 47 56 67 73 84 95 0 1 2 3 4 5 6 7 8
Binary search Key k was foundin position mid Key(Retrieve(L,mid)) Key k should be insertedin position mid or mid+1
Can we implement alloperations in O(log n) time? Yes! Using Binary Search Trees
7 2 8 1 5 10 Binary search trees A binary tree in which each node contains a dic-item. Satisfies the binary-search-tree property:If y is in the left subtree of x, then y.key < x.key. If y is in the right subtree of x, then y.key > x.key. x parent key info right left
7 2 8 1 5 10 Binary search trees D.root x Dic-Item ≡ Tree-Node parent key info right left left, right, parent are initially null
1 7 2 2 8 8 1 5 10 7 10 5 A set can be represented using several different trees height=4 4 height=2 2 3 1 1 2 0 0 0 1 0 Height – length of a longest path to a leaf 0
7 2 8 1 10 5 Tree-Search(x,k) – Look for k in the subtree of x x x x Tree-Search(x,5) We usually start the search at the root of the tree: Search(D,k) Tree-Search(D.root,k)
7 2 8 1 10 5 Tree-Position(x,k) – Look for k in the subtree of xReturn the last node encountered x x x y y y Tree-Position(x,6) Returns the node containing 5 Tree-Position(x,k) is used to find insertion points
7 2 8 1 5 10 Printing the elements of a BST in sorted order(In-order walk) 3 1 4 0 2 5 Printing, of course, is just an example…
7 2 8 1 5 10 Finding the minimum“keep going left”
Successor(x) If x has a right child, the successor of xis the minimal element in x.right. x “Go right once, and then left all the way” What if x.right=null ?
Successor(x) If x.right=null, the successor of x is the lowestancestory of x such that x is in its right subtree y “Go up from x untilthe first turn right’’ x
Successor(x) y If x has the largest key, then Successor(x)=null. x Predecessor is symmetric
7 2 8 1 5 10 Insertions 6 9 Insert(6) Insert(9)
Binary Search Tree Animations http://webdiis.unizar.es/asignaturas/EDA/AVLTree/avltree.html http://webdiis.unizar.es/asignaturas/EDA/AVLTree/avltree.html For the time being, turn all buttons on the right off Warning: There are some differences with what we learn
Deletion: easy cases first 7 2 8 1 5 10 6 9 Delete(6) – 6 is a leaf; simply remove it. Delete(8) – 8 has only one child; bypass it. Delete(10) – 10 has only one child; bypass it. Delete(2) – more complicated…
Deletion of a binary node If z has two children,let y be the successor of z z y has no left child Remove y from the tree Replace z by y y Binary-search-tree property preserved! Is it enough to let z.keyy.key? And maybe also z.infoy.info?
Analysis Each operation takes O(h+1)time, where h is the height of the tree In general h may be as large as n Want to keep the tree with small h
Balanced trees A full tree of height h contains n=2h+1 − 1 nodes h = log2(n+1)−1 How do we keep the tree more or less balanced?
Randomly built BSTs Maybe balancing will take care of itself? Not if we insert the elements in sorted order.We get a path of length n Things are usually ok if weinsert the elements in random order Theorem: If n distinct keys are inserted into a BST in random order, then the expected height of the tree is O(log n). We want worst-case results…
Rotations x y Right rotate y x A C A C B B Left rotate