• Tidak ada hasil yang ditemukan

Sequential Searching

N/A
N/A
Protected

Academic year: 2024

Membagikan "Sequential Searching"

Copied!
13
0
0

Teks penuh

(1)

Search Algorithm

Lecture 6

(2)

Sequential Searching

If the elements are randomly distributed and

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

(3)

worst case time complexity= O(logn)

For each search (while loop), the size of records gets the half

Binary Search

(4)

Binary search is fast but if we want to insert or delete a key, we have to move n/2 records on

average to keep the sorted structure, thus it’s not appropriate for applications, which need insertion or deletion.

Binary Search

(5)

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 x

(6)

Binary 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

(7)

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)

(8)

Balanced Tree

The worst cases using binary search tree

three cases

inserted in increasing order A, B, C, D, E, F, G, H

(9)

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

(10)

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

(11)

Balanced Tree

avoid skewed tree

balance the height of left and the right

2-3-4 tree

red black tree

(12)

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

(13)

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

2-3-4 tree

Referensi

Dokumen terkait

Also, we implement the binary tree algorithm witch is the simple and fast algorithm [24-26] to divide the original line into the equal line segment which is based on the number of