Download presentation
Presentation is loading. Please wait.
Published byEmmeline Barnett Modified over 8 years ago
1
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
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
3 NULL 2 2 2 2 3 3 3 3 5 5 5 5 7 7 7 7 8 8 8 8 119 9 9 9 17 21
4
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 2 3 5 7 8 9 11 17 21
5
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 2 12.5% nodes of level 3 etc. NULL 2 3 5 7 8 9 11 17 21
6
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
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
8 Skip Lists NULL 2 3 5 7 8 9 11 17 21 2 7 5 3 17 11 9 8 21 instead of : we may get : NULL
9
9 Skip Lists Example: if maxLevel == 4, then the list has at most 2 4 -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
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
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. 2 7 5 3 17 11 9 8 21 NULL
12
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. 2 7 5 3 17 11 9 8 21 NULL
13
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
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
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
16 Binary Search Trees RootH BI AF D C G E -- 0 -- -- 1 -- -- 2 -- -- 3 -- -- 4 -- height : 4 a path from the root to a leaf height = length of longest path from the root to a leaf
17
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.