1 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 –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 NULL
4 Skip Lists The list is kept in sorted order Every 2 i th node has a pointer 2 i 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
5 Skip Lists Every 2 i th node has a pointer 2 i nodes ahead –levels of nodes are distributed as follows: 50% nodes of level 1 25% nodes of level % nodes of level 3 etc. NULL
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 NULL instead of : we may get : NULL
9 Skip Lists Example: if maxLevel == 4, then the list has at most = 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
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
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 Root : leaves : internal nodes branches H BI AF D C G E subtree rooted at D B is the parent of F A, F are children of B D is a descendant of B B is an ancestor of G
16 Binary Search Trees RootH BI AF D C G E height : 4 a path from the root to a leaf 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.