Download presentation
Presentation is loading. Please wait.
Published byCatherine Bridges Modified over 8 years ago
1
FALL 2005CENG 213 Data Structures1 Review
2
FALL 2005CENG 213 Data Structures2 Collection of items Problems that must manage data by value. Some important operations are: –Inserting a data item containing the value x. –Delete a data item containing the value x. –Retrieve a data item containing the value x. Various implementations are possible. We have to analyze the possible implementations so that we can make an intelligent choice. – Some operations are performed more efficiently in certain implementations.
3
FALL 2005CENG 213 Data Structures3 An ordinary table of cities
4
FALL 2005CENG 213 Data Structures4 ADT Table Operations Various sets of table operations are possible. Some of them are: –Create an empty table. –Destroy a table. –Determine whether a table is empty. –Determine the number of items in the table. –Insert a new item into a table. –Delete the item with a given search key. –Retrieve the item with a given search key. –Traverse the table. The client of the ADT table may need a subset of these operations, or require more operations on the table. Are keys in the table unique? –We will assume that keys in our table are unique. –But, some other tables allow duplicate keys.
5
FALL 2005CENG 213 Data Structures5 Selecting an Implementation Since an array or a linked list represents items one after another, these implementations are called linear. There are four categories of linear implementations: –Unsorted, array based (an unsorted array) –Unsorted, pointer based (a simple linked list) –Sorted (by search key), array based (a sorted array) –Sorted (by search key), pointer based (a sorted linked list). We have also nonlinear implementations such as binary search tree. –Binary search tree implementation offers several advantages over linear implementations.
6
FALL 2005CENG 213 Data Structures6 Sorted Linear Implementations a)Array-Based b)Pointer-Based
7
FALL 2005CENG 213 Data Structures7 Binary Search Tree Implementation
8
FALL 2005CENG 213 Data Structures8 Which Implementation should be used? Which implementation is appropriate depends on our application. Before we select an implementation, we should answer following questions about our application: 1.What operations are needed? –Our application may not need all operations. –Some operations are done more efficiently in an implementation, and others are done more efficiently in another implementation. 2.How often is each operation is required? –Some applications may require many occurrences of an operation, but other applications may not. –For example, some applications may perform many retrievals, but not so many insertions and deletions. On the other hand, other applications may perform many insertions and deletions.
9
FALL 2005CENG 213 Data Structures9 How to Select an Implementation – Scenario A Let us assume that we have an application: – inserts data items into a table. –after all data items are inserted traverse this table in no particular order. –does not perform any retrieval and deletion operations. Which implementation is appropriate for this application? –Keeping the items in a sorted order does not provide any advantage for this application. In fact, it will be more costly for this application. Unsorted implementation will be more appropriate. Which unsorted implementation (array-based, pointer-based)? Do we know the maximum size of the table? If we know the expected size is close to the maximum size of the table an array-based will be more appropriate (because pointer-based use extra space for pointers) Otherwise, a pointer-based will be more appropriate (because too many entries will be empty in the array-based implementation)
10
FALL 2005CENG 213 Data Structures10 Insertion for unsorted linear implementations (a)array based; (b)pointer based Runtime complexity of insertion in an unsorted list: O(1)
11
FALL 2005CENG 213 Data Structures11 How to Select an Implementation – Scenario B Let us assume that we have an application: – performs many retrievals, but no insertions and deletions (or so few insertions or deletions, so that we ignore their costs). –for example, a thesaurus (to look up synonyms of a word) For this application a sorted implementation is more appropriate. –If we use a sorted array, we can use binary search to access data. –Since binary search is not practical with linked lists, sorted linked list implementation will not be appropriate. –We can also use a binary search tree (in fact, we can use a balanced binary search tree). If we know the table’s maximum size, a sorted array-based implementation is more appropriate for frequent retrievals. –Otherwise, the binary search tree implementation will be more appropriate for frequent retrievals.
12
FALL 2005CENG 213 Data Structures12 How to Select an Implementation – Scenario C Let us assume that we have an application: – performs many retrievals, insertions and deletions. ? Sorted Array Implementation: –Retrieval is efficient. –But insertion and deletion are not efficient. We have to shift data items. sorted-array implementation is not appropriate. ? Sorted Linked List Implementation: –Retrieval, insertion, and deletion are not efficient (although we do not shift data items) sorted linked list implementation is not appropriate. ? Binary Search Tree Implementation: –Retrieval, insertion, and deletion are efficient. binary search tree implementation is appropriate.
13
FALL 2005CENG 213 Data Structures13 Insertion for sorted linear implementations (a)array based; (b) pointer based
14
FALL 2005CENG 213 Data Structures14 Which Implementation? Despite difficulties, linear implementations of a table can be appropriate. –Linear implementations are easy to understand, easy to implement. –Linear implementations can be appropriate for small tables. –For large tables, if there are few deletions and retrievals, linear implementations may be appropriate. In general, a binary search tree implementation is a better choice. –Worst case: O(n) for most table operations –Average case: O(log 2 n) for most table operations Balanced binary search tree increases the efficiency of the ADT table operations.
15
FALL 2005CENG 213 Data Structures15 The average-case order of the operations for various implementations
16
FALL 2005CENG 213 Data Structures16 The ADT Priority Queue Each data item in a priority queue has a priority value. We insert an item with a priority value into its proper position in the priority queue. The delete operation deletes the item with the highest priority. Using a priority queue we prioritize a list of tasks: –Job scheduling
17
FALL 2005CENG 213 Data Structures17 Some implementations of the ADT priority queue (a) array based; (b) pointer based; (c) binary search tree
18
FALL 2005CENG 213 Data Structures18 Implementations – Analysis None of these implementations of the priority queue is not efficient enough. Array-Based – –Insertion will be O(n) Pointer-Based – –Insertion will be O(n) BST Implementation – –Insertion is O(log 2 n) in average, but O(n) in the worst case. We need a balanced BST so that we can get better performance ( O(logn) in the worst case) Heap
19
FALL 2005CENG 213 Data Structures19 Difference between Heap and BST A heap is NOT a binary search tree. Differences between heap and BST: 1.While we can see a binary search tree as sorted, but a heap is ordered in much weaker sense. The order of the heap (although it is not sorted) is sufficient for the efficient performance of the priority queue operations. 2.While binary search trees come in many different shapes, heaps are always complete binary trees.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.