Upcoming SlideShare
×

# MELJUN CORTES Jedi slides data st-chapter08-tables

386 views

Published on

MELJUN CORTES Jedi slides data st-chapter08-tables

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
386
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
23
0
Likes
1
Embeds 0
No embeds

No notes for slide

### MELJUN CORTES Jedi slides data st-chapter08-tables

1. 1. 8 TablesData Structures – Tables 1
2. 2. ObjectivesAt the end of the lesson, the student should be able to:● Discuss the basic concepts and definitions on tables: keys, operations and implementation● Explain table organization – unordered and ordered● Perform searching on a table using sequential, indexed sequential, binary and Fibonacci search Data Structures – Tables 2
3. 3. Definition and Related Concepts● Table – Most common structure used to store data to search – Group of elements, each of which is called a record which has a unique key. The notation used for a record is (Ki, Xi) – Also referred to as a file class Table{ int key[]; int data[]; int size; Table(){} Table(int s){ size = s; key = new int[size]; data = new int[size]; } } Data Structures – Tables 3
4. 4. Definition and Related Concepts● Types of Keys – internal or embedded key - key contained within the record at a specific offset relative to the start of the record – external key - key contained in a separate table with pointers to the associated data● Operations – Searching for the record where Ki = K, where K is given by the user – Insertion – Deletion – Search the record with smallest (largest) key – Given key Ki, find the record with the next larger (smaller) key – And so on… Data Structures – Tables 4
5. 5. Definition and Related Concepts● Implementation – Can be represented using sequential allocation, linked allocation or a combination of both – Factors to consider: ● Size of the key space Uk, i.e., the number of possible keys ● Nature of the table: dynamic or static ● Type and mix of operations performed on the table – Direct-address table – sequential allocation where key is the same as the index in the array Data Structures – Tables 5
6. 6. Searching● Table Organization – ordered table - elements are sorted based on their keys – unordered table - no relation presumed to exist among the records and their associated keys● Sequential search in an unordered table – linear search; O(n) time int sequentialSearch(int k, int key[]){ for (int i=0; i<key.length; i++) if (k == key[i]) return i; /* successful search */ return -1; /* unsuccessful search */ } Data Structures – Tables 6
7. 7. Searching● Searching in an Ordered Table – Indexed Sequential Search - an auxiliary table called index is added to the sorted table ● Each element in the index consists of a key and a pointer to the record in the file that corresponds to Kindex. ● Elements in the index must be sorted based on the key. ● The actual data file may or may not be ordered. ID No Pointer ID No Name Birthdate Course 1 12345 45678 Andres Agor 01/23/87 BSCS 2 23456 56789 Juan Ramos 10/14/85 BSIE 3 34567 23456 Maria dela Cruz 12/07/86 BSCS 4 45678 78901 Mayumi Antonio 09/18/85 BSCE 5 56789 34567 Jose Santiago 06/17/86 BS Biology 6 67890 12345 Bituin Abad 04/21/84 BSBA 7 78901 67890 Frisco Aquino 08/22/87 BSME Data Structures – Tables 7
8. 8. Searching● Searching in an Ordered Table – Binary Search – O(log2n) time /* Returns the index of key k if found, otherwise -1 */ int binarySearch(int k, Table t){ int lower = 0; int upper = t.size – 1; int middle; while (lower < upper){ middle = (int) Math.floor((lower + upper) / 2); /* successful search */ if (k == t.key[middle]) return middle; /* discard lower half */ else if (k > t.key[middle]) lower = middle + 1; /* discard upper half */ else upper = upper - 1; } return(-1); /* unsuccessful search */ } Data Structures – Tables 8
9. 9. Searching● Searching in an Ordered Table – Binary Search ● Example, search for the key k=34567: 0 1 2 3 4 5 6 12345 23456 34567 45678 56789 67890 78901 lower = 0, upper = 6, middle = 3: k < kmiddle (45678) lower = 0, upper = 2, middle = 1: k > kmiddle (23456) lower = 2, upper = 2, middle = 2: k = kmiddle (34567) ==> successful search Data Structures – Tables 9
10. 10. Searching● Searching in an Ordered Table – Multiplicative Binary Search - similar to the binary search but avoids the division needed to find the middle key. – Must rearrange the records in the table: ● Assign keys K1 < K2 < K3 < …< Kn to the nodes of a complete binary tree in inorder sequence ● Arrange the records in the table according to the corresponding level- order sequence in the binary tree. /*Accepts set of keys represented as complete binary tree */ int multiplicativeBinarySearch(int k, int key[]){ int i = 0; while (i < key.length){ if (k == key[i]) return i; /* successful search */ else if (k < key[i]) i = 2 * i + 1; /* go left */ else i = 2 * i + 2; /* go right */ } return -1; /* unsuccessful search */ ● } Data Structures – Tables 10
11. 11. Searching● Searching in an Ordered Table – Fibonacci Search F0 = 0 F1 = 1 Fj = Fi-2 + Fi-1 , i ≥ 2 ● Two auxiliary variables: p = Fi-1 and q = Fi-2 ● Assumption: table size n=Fi+1-1 Data Structures – Tables 11
12. 12. Searching● Searching in an ordered table – Fibonacci Search ● Three states of comparison are possible. Kj is initially chosen such that j=Fi, where Fi is the largest Fibonacci number <= table size n: – If K = Kj, stop: successful search – If K < Kj 1) Discard all the keys with indexes greater than j. 2) Set j =j – q 3) Shift p and q one place to the left of the number sequence – If K > Kj, 1) Discard all the keys with indexes less than j 2) Set j =j + q 3) Shift p and q two places to the left of the number sequence – K < Kj and q=0 or K > Kj and p=1: search unsuccessful Data Structures – Tables 12
13. 13. Searching● Searching in an ordered table – Fibonacci Search ● Example: search for key k = 34567: 0 1 2 3 4 5 6 12345 23456 34567 45678 56789 67890 78901 0 1 1 2 3 5 8 13 F 01234567 i i = 5; Fi = 5; (Assumption) table size = Fi+1 - 1 = 7 j = 5, p = 3, q = 2: k < key[j] j = 3, p = 2, q = 1: k < key[j] j = 2, p = 1, q = 1: k = key[j] Successful Data Structures – Tables 13
14. 14. Summary● A table is a group of elements, each of which is called a record● The types of keys are internal or embedded key, and external key● Tables may be represented using sequential allocation, link allocation or both● Searching is eliminated in direct-address table. Also, insertion and deletion are relatively straightforward● A table may be ordered or unordered. Linear search is used if the table is unordered, while indexed sequential search, binary search or Fibonacci search is used if the table is ordered Data Structures – Tables 14