lecture15

lecture15 - We sday May 24 dne s Table s Hash Table vie...

This preview shows pages 1–9. Sign up to view the full content.

Wednesday, May 24 th Tables Hash Tables Review Challenge

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Big-OH Craziness Consider a binary search tree that holds N student records, all indexed by their name . Each student record contains a linked-list of the L classes that they have taken while at UCLA. What is the big-oh to determine if a student has taken a class? bool HasTakenClass( BTree & b , string & class ) Name: Rick Classes: Left Right Class: CS31 Next: Name: Linda Classes: Left Right Class: CS31 Next: Class: EE10 Next: Name: Sal Classes: Left Right Class: Math31 Next: Class: Math31 Next: NULL Class: EE100 Next: NULL NULL NULL NULL NULL NULL
Tables A table is a data structure that allows us to store a number of records and efficiently lookup those records using one or more key fields . Examples: 1. UCLA: Store student records and look them up by the student’s last name . 1. DMV: vehicle records and look them up by the Vehicle ID Number (VIN) 1. Phone company: customer records customer’s phone # or last name .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Tables Definition : A record is basically a struct/class variable with a bunch of information. struct CarRecord { string make; string model; int year; int horsepower; int VehicleIDNum; }; key field is a specially designated field in the struct that the user of the table can search by. bool Search(CarRecord table[ ], int idnum , CarRecord &out) { for (int i=0;i<table.size(); i++) if ( == table[i]. VehicleIDNum ) { out = table[i]; return(true); } return(false); }
Can anyone think of any data structures that allow us to search quickly? Tables This is a perfectly valid table! However, often times we want to make it more efficient to search our table. In the previous example, we used an array to hold our table and a linear search to find the proper car by its ID #. Right! We can use a Binary Search Tree to organize our records by the key field.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Tables using Binary Trees name: Carey GPA: 3.62 ID: 400655312 name: Linda GPA: 3.99 ID: 000317312 name: Alex GPA: 2.05 ID: 712424512 null Consider this table of students. Here we use the student’s name as the key field . name: Abe GPA: 4.00 ID: 987654321 name: Zelda GPA: 3.43 ID: 641632128 name: Jason GPA: 1.55 ID: 105405118 What if we wanted to use the student ID # as the key field ? We order our BST by the student’s name so we can quickly search it to find a student.
Tables using Binary Trees name: Carey GPA: 3.62 ID: 400655312 name: Zelda GPA: 3.43 ID: 641632128 null name: Linda GPA: 3.99 ID: 000317312 name: Alex GPA: 2.05 ID: 712424512 name: Jason GPA: 1.55 ID: 105405118 name: Abe GPA: 4.00 ID: 987654321 With this BST, you can quickly find any student’s record by their ID #! Question : What if we want to have two key fields, so the user can quickly search by name OR ID #? We can efficiently search a table by its key field . But if you want to search by another field, it slow ! Challenge : Search for “Zelda” in O(log n) time!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Complex Tables If we want to have more than one key field, we can use two separate binary search trees AND an array name: Linda GPA: 3.99 ID: 000317313 name: Alex GPA: 2.05 ID: 712424512 name: Jason GPA: 1.55 ID: 105405118 null name: Abe GPA: 4.00 ID: 987654321 name: Zelda GPA: 3.43 ID: 641632128 name: Carey GPA: 3.62 ID: 400655312 Student Array 0 1 2 3 4 5 Each node in the trees will contain JUST the key value
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/20/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 44

lecture15 - We sday May 24 dne s Table s Hash Table vie...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online