1 / 17

Searching

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

hafwen
Download Presentation

Searching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

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

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

  10. 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)

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

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

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

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

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

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

  17. Binary Search Trees • Used for storing and retrieving information • Typical operations: • insert • delete • search • Data structures that support these three operations are called dictionaries.

More Related