Searching
description
Transcript of Searching
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
NULL
NULL
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
11
11
11
17
17
17
17
21
21
21
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
23
57
89
11
17
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
23
57
89
11
17
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
NULL
23
57
89
11
17
21
27
53
1711
9
8
21
instead of :
we may get :
NULL
9
Skip Lists
Example:
if maxLevel == 4, then the list has at most 24-1 = 15 elementsOf these,
8 are level 14 are level 22 are level 31 is level 4
Come up with a function that generates1 with probability 1/22 with probability 1/43 with probability 1/84 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.
27
53
1711
9
8
21
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.
27
53
1711
9
8
21
NULL
13
ComparisonSorted 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 TreesRoot
: leaves : internal nodes
branches
H
B I
A F
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 TreesRootH
B I
A F
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
Binary Search Trees
Used for storing and retrieving information Typical operations:
– insert– delete – search
Data structures that support these three operations are called dictionaries.