Search Algorithm
Lecture 6
Sequential Searching
If the elements are randomly distributed andsequentially stored in a data structure, we use the sequential search.
We assume inputs are stored in an array or in a linked list.
We search data one by one comparing the search key.•
worst case time complexity= O(logn)For each search (while loop), the size of records gets the half
Binary Search
Binary search is fast but if we want to insert or delete a key, we have to move n/2 records onaverage to keep the sorted structure, thus it’s not appropriate for applications, which need insertion or deletion.
Binary Search
Binary Search Tree
If the data records are dynamically inserted/deleted, we need a way to make insertion and deletion easy.
Binary search tree: Binary tree that each node as left and right link.
left side of node x is smaller than x and right side of node x is bigger than xBinary Search Tree
average time complexity= O(logn)proportion to the number of comparisons. Thus, proportion to the length of the paths.
average number of comparisons
= average length of tree paths/n
Binary Search Tree
average time complexity= O(logn)proportion to the number of comparisons. Thus, proportion to the length of the paths
insertion and deletion time depend on the search time. worst time complexity= O(n)
average time complexity= O(logn)
Balanced Tree
The worst cases using binary search tree three cases
inserted in increasing order A, B, C, D, E, F, G, H
Balanced Tree
The worst cases using binary search tree three cases
inserted in decreasing order A, B, C, D, E, F, G, H
Z, Y, X, W, V, U, T, S
…
Balanced Tree
The worst cases using binary search tree
three cases
big and small key insert in turn A, B, C, D, E, F, G, H
Z, Y, X, W, V, U, T, S A, Z, B, Y, C, X, D, W
Balanced Tree
avoid skewed tree
balance the height of left and the right
2-3-4 tree
red black tree2-3-4 tree
insertion – insert G
find the insertion location for G, fail at HIJ, the node already contains 3 elements (4-node), thus, we cannot insert into the node. Create a new node to insert the
element by increasing the height of the tree. Better solution?
splitting nodes only occur through the search
In other words, splitting occurs only on the search path
data structure is complicated
Implementation is not easy
For some cases, it is slower then the binary search tree