. . Januray 27th, 2011 Biostatistics 615/815 - Lecture 7 Hyun Min Kang Januray 27th, 2011 Hyun Min Kang Data Structures Biostatistics 615/815 Lecture 7: . . . . . . Summary . . Tree List SortedArray Introduction . . . . . . . . . 1 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . Homework #2 is finally announced Due is on Feb 7th, but better start earlier . 815 projects . . . . . . . . Instructor will send out E-mails to individually later today. Hyun Min Kang Biostatistics 615/815 - Lecture 7 Januray 27th, 2011 . . . . . . . . . . . . . Introduction SortedArray List Tree . Summary Announcements . Another good and bad news . . 2 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 815 projects . . . . . . . . Instructor will send out E-mails to individually later today. Hyun Min Kang Biostatistics 615/815 - Lecture 7 Januray 27th, 2011 . . . . . . . . . . . . . Introduction SortedArray List 2 / 33 Tree . Summary Announcements . Another good and bad news . . . . . . . . . . . . . . . . . . . . . . . . . . . • Homework #2 is finally announced • Due is on Feb 7th, but better start earlier
. . . . . . . 815 projects . . . . . . . . Hyun Min Kang Biostatistics 615/815 - Lecture 7 Januray 27th, 2011 . . . . . . . . . . . . . Introduction SortedArray List Tree . Summary Announcements . Another good and bad news 2 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . . • Homework #2 is finally announced • Due is on Feb 7th, but better start earlier • Instructor will send out E-mails to individually later today.
. . Januray 27th, 2011 Biostatistics 615/815 - Lecture 7 Hyun Min Kang using a linear sorting algorithm such as CountingSort . . . . . . . . Key idea . Recap : Radix sort Summary . . . . . . . . . Introduction SortedArray List Tree . 3 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . . • Sort the input sequence from the last digit to the first repeatedly • Applicable to integers within a finite range
. Recap : Elementary data structures Januray 27th, 2011 Biostatistics 615/815 - Lecture 7 Hyun Min Kang Hash Tree List SortedArray . Array Delete Insert Search 4 / 33 Summary . Tree List SortedArray . . . Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Θ( n ) Θ(1) Θ( n ) Θ( log n ) Θ( n ) Θ( n ) Θ( n ) Θ(1) Θ( n ) Θ( log n ) Θ( log n ) Θ( log n ) Θ(1) Θ(1) Θ(1) • Array or list is simple and fast enough for small-sized data • Tree is easier to scale up to moderate to large-sized data • Hash is the most robust for very large datasets
. Tree Januray 27th, 2011 Biostatistics 615/815 - Lecture 7 Hyun Min Kang . can be tricky Recap : Memory management with user-defined data type Summary . 5 / 33 List SortedArray Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . int main(int argc, char** argv) { myArray<int> A; // creating an instance of myArray A.insert(10); A.insert(20); myArray<int> B = A; // copy the instance B.remove(10); if ( A.search(10) < 0 ) { std::cout << "Cannot find 10" << std::endl; // what would happen? } return 0; // would to program terminate without errors? }
. . . . . . . Focus . . . . . . . . Hyun Min Kang Biostatistics 615/815 - Lecture 7 Januray 27th, 2011 . . . Tree . . . . . . . . . Introduction SortedArray . List Summary . Today . More data structures 6 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . . • Sorted array • Linked list • Binary search tree • Hash table • Key concepts • How to implement the concepts to working examples • But not too much detail of advanced C++
Remove Same as the unsorted version of Array . . . . Algorithms . . . . . . . . Insert Insert the element at the end, and swap with the previous element if larger Same as a single iteration of InsertionSort Search Use the binary search algorithm Hyun Min Kang Biostatistics 615/815 - Lecture 7 Januray 27th, 2011 . . . Summary . . . . . . . . . Introduction SortedArray List Tree . . Sorted Array . Key Idea . . . 7 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . . • Same structure with Array • Ensure that elements are sorted when inserting and deleting an object • Insertion takes longer, but search will be much faster • Θ( n ) for insert • Θ( log n ) for search
Remove Same as the unsorted version of Array . . . . Algorithms . . . . . . . . Insert Insert the element at the end, and swap with the previous element if larger Same as a single iteration of InsertionSort Search Use the binary search algorithm Hyun Min Kang Biostatistics 615/815 - Lecture 7 Januray 27th, 2011 . . . Summary . . . . . . . . . Introduction SortedArray List Tree . . Sorted Array . Key Idea . . . 7 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . . • Same structure with Array • Ensure that elements are sorted when inserting and deleting an object • Insertion takes longer, but search will be much faster • Θ( n ) for insert • Θ( log n ) for search
Remove Same as the unsorted version of Array . . . . Algorithms . . . . . . . . Insert Insert the element at the end, and swap with the previous element if larger Same as a single iteration of InsertionSort Search Use the binary search algorithm Hyun Min Kang Biostatistics 615/815 - Lecture 7 Januray 27th, 2011 . . . Summary . . . . . . . . . Introduction SortedArray List Tree . . Sorted Array . Key Idea . . . 7 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . . • Same structure with Array • Ensure that elements are sorted when inserting and deleting an object • Insertion takes longer, but search will be much faster • Θ( n ) for insert • Θ( log n ) for search
Remove Same as the unsorted version of Array . . . . . Algorithms . . . . . . . . Insert Insert the element at the end, and swap with the previous element if larger Search Use the binary search algorithm Hyun Min Kang Biostatistics 615/815 - Lecture 7 Januray 27th, 2011 . . . . . . . . . . . . . Introduction SortedArray List . Tree Summary Sorted Array . Key Idea . . 7 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . . • Same structure with Array • Ensure that elements are sorted when inserting and deleting an object • Insertion takes longer, but search will be much faster • Θ( n ) for insert • Θ( log n ) for search • Same as a single iteration of InsertionSort
Remove Same as the unsorted version of Array . . . . . Algorithms . . . . . . . . Insert Insert the element at the end, and swap with the previous element if larger Search Use the binary search algorithm Hyun Min Kang Biostatistics 615/815 - Lecture 7 Januray 27th, 2011 . . . . . . . . . . . . . Introduction SortedArray List . Tree Summary Sorted Array . Key Idea . . 7 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . . • Same structure with Array • Ensure that elements are sorted when inserting and deleting an object • Insertion takes longer, but search will be much faster • Θ( n ) for insert • Θ( log n ) for search • Same as a single iteration of InsertionSort
. . . . . Algorithms . . . . . . . . Insert Insert the element at the end, and swap with the previous element if larger Search Use the binary search algorithm Hyun Min Kang Biostatistics 615/815 - Lecture 7 Januray 27th, 2011 . . . . . . . . . . . . . Introduction SortedArray List . Tree Summary Sorted Array . . Key Idea 7 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . . . • Same structure with Array • Ensure that elements are sorted when inserting and deleting an object • Insertion takes longer, but search will be much faster • Θ( n ) for insert • Θ( log n ) for search • Same as a single iteration of InsertionSort Remove Same as the unsorted version of Array
. List Januray 27th, 2011 Biostatistics 615/815 - Lecture 7 Hyun Min Kang . Summary . Tree 8 / 33 . SortedArray . Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implementation : mySortedArray.h // Exactly the same as myArray.h #include <iostream> #define DEFAULT_ALLOC 1024 template <class T> // template supporting a generic type class mySortedArray { protected: // member variables hidden from outside T *data; // array of the genetic type int size; // number of elements in the container int nalloc; // # of objects allocated in the memory mySortedArray(mySortedArray& a) {}; // for disabling object copy int search(const T& x, int begin, int end); // search with ranges public: // abstract interface visible to outside mySortedArray(); // default constructor ˜ mySortedArray(); // destructor void insert(const T& x); // insert an element x int search(const T& x); // search for an element x and return its location bool remove(const T& x); // delete a particular element };
Recommend
More recommend