180 likes | 306 Views
Searching. Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the worst and average case. Can we get logarithmic search time in a linked memory structure? Idea #1 : Skip List
E N D
Searching • Searching in a sorted linked list takes linear time in the worst and average case. • Searching in a sorted array takes logarithmic timein the worst and average case. • Can we get logarithmic search time in a linked memory structure? • Idea #1 : Skip List • Idea #2 : Binary Search Tree
Skip Lists • Linked lists have linear search time. • Goal: improve the search time • Idea: Modify the list structure in a way that will allow the application of binary search. • Add a pointer to the middle element • Add a pointer to the middle of each half • Add a pointer to the middle of each quarter • etc. • The result is a skip list
2 3 5 7 8 9 11 17 21 NULL NULL 3 7 9 17 2 5 8 11 21 7 17 NULL 3 9 2 5 8 11 21 NULL 17 7 3 9 2 5 8 11 21
Skip Lists • The list is kept in sorted order • Every 2ith node has a pointer 2i nodes ahead. • This list is called a perfect skip list. • A node with k pointers is called a level k node • Level of list = maximum node level. NULL 17 7 3 9 2 5 8 11 21
Skip Lists • Every 2ith node has a pointer 2i nodes ahead • levels of nodes are distributed as follows: • 50% nodes of level 1 • 25% nodes of level 2 • 12.5% nodes of level 3 • etc. NULL 17 7 3 9 2 5 8 11 21
Skip Lists • Extra pointers : O(n) • Search time : O(lgn) • Insert/Delete • Problem : • The list will need extensive restructuring after a delete or insert operation • Solution? • Keep some advantages of skip list • Avoid restructuring
Skip Lists • Idea : • Drop requirement about the position of the nodes at each level • Instead, make sure we have the same number of nodes at each level, regardless of how they are arranged • In other words: • Choose node level randomly but in the same proportions: 50% level 1, 25% level 2, etc.
Skip Lists instead of : NULL 17 7 3 9 2 5 8 11 21 we may get : NULL 8 3 7 11 2 5 9 17 21
Skip Lists • Example: if maxLevel == 4, then the list has at most 24-1= 15 elements Of these, 8 are level 1 4 are level 2 2 are level 3 1 is level 4 Come up with a function that generates 1 with probability 1/2 2 with probability 1/4 3 with probability 1/8 4 with probability 1/16
Skip Lists • SEARCH(target) • Start at the highest chain • As long as the target is greater than the next key, • move forward along the chain. • When the target is less than the next key, • move down one level. • Repeat this process until the target is found, or it is determined (at level 1) that it is not in the list. • Time • best/average case : logarithmic • worst case : linear (the skip list has become a regular list)
Skip Lists • INSERT (key) • Do a search to find the insert location • keep track of potential predecessor • Select level of new node • Insert new node and, if necessary, increase maxLevel. NULL 8 3 7 11 2 5 9 17 21
Skip Lists • DELETE (key) • Do a search to find the node to be deleted • keep track of predecessor • Delete node and, if necessary, decrease maxLevel. NULL 8 3 7 11 2 5 9 17 21
Comparison • Sorted Linked List • Very easy to implement, but linear search/insert/delete • Skip list • More space but insert/delete are much simpler to implement. Worst-case search/insert/delete is linear but in practice it is almost always logarithmic.
Binary Search Trees • A binary search tree • Is a recursively defined structure: • It contains no nodes, or • it is comprised of three disjoint sets of nodes: • a root • a binary search tree called the left subtree of the root • a binary search tree called the right subtree of the root • Satisfies the binary search property: • The key stored in the root is larger than any key in the left subtree and smaller than any key in the right subtree.
Binary Search Trees H Root branches B I B is the parent of F A, F are children of B D is a descendant of B B is an ancestor of G A F subtree rooted at D D G C E : leaves : internal nodes
Binary Search Trees H Root -- 0 -- a path from the root to a leaf B I -- 1 -- A F -- 2 -- height : 4 D G -- 3 -- -- 4 -- C E height = length of longest path from the root to a leaf
Binary Search Trees • Used for storing and retrieving information • Typical operations: • insert • delete • search • Data structures that support these three operations are called dictionaries.