13_Binary_Search_Tree_Class_2016_Spring.pdf

# 13_Binary_Search_Tree_Class_2016_Spring.pdf - Running times...

• Notes
• 56

This preview shows page 1 - 7 out of 56 pages.

Running times? •printPreOrder( ) •printInOrder( ) •printPostOrder( ) •size( ) •height( ) •duplicate( ) 1 O(n) O(n) O(n) O(n) O(n) O(n)

Subscribe to view the full document.

CS2134 Alternative Design Can achieve more flexibility in what is done on a traversal,without the full generality of iterators, by using a functor to specify what is done when a node is visited, e.g.: template <class Object> template< class Visitor> void BinaryTreeNode<Object>::preorder (Visitor visit ) { visit (this); //instead of cout << element if (left != NULL) left->preorder( visit ) if (right!= NULL) right->preorder( visit ); ... }
CS2134 Relationship between height and size Some algorithms we’ll study have running time O( h ). How does this relate to the size n ? Worst case (biggest height for a given size): Each node has a single child: h=n-1 Best case: (smallest height for a given size): – Each internal node has two children n = 2 (h+1) – 1 , so h=O(log n) Average case: h = O(log n) – Bigger constant than best case

Subscribe to view the full document.

CS2134 Lecture 16 Binary Search Trees
#include <iostream> #include <string> using namespace std; int main () { map<string,string> mymap; mymap[ "Bakery" ]= "Barbara" ; // new element inserted mymap[ "Seafood" ]= "Lisa" ; // new element inserted mymap[ "Produce" ]= "John" ; // new element inserted string name = mymap[ "Bakery" ]; // existing element accessed (read) mymap[ "Seafood" ] = name; // existing element accessed (written) mymap[ "Bakery" ] = mymap[ "Produce" ]; // existing elements accessed (read/written) name = mymap[ "Deli" ]; // non-existing element: new element "Deli" inserted! mymap[ "Produce" ] = mymap[ "Gifts" ]; // new element "Gifts" inserted, "Produce" written } Modified from []/ map example <string,string> <Produce, John> <Gifts, ””> <Bakery, Barbara> <Seafood, Lisa> <Deli, ””> John ”” Barbara> #include <unordered_map>

Subscribe to view the full document.

#include <iostream> #include <string> using namespace std; int main () { mymap[ "Bakery" ]= "Barbara" ; // new element inserted mymap[ "Seafood" ]= "Lisa" ; // new element inserted mymap[ "Produce" ]= "John" ; // new element inserted string name = mymap[ "Bakery" ]; // existing element accessed (read) mymap[ "Seafood" ] = name; // existing element accessed (written) mymap[ "Bakery" ] = mymap[ "Produce" ]; // existing elements accessed (read/written) name = mymap[ "Deli" ]; // non-existing element: new element "Deli" inserted! mymap[ "Produce" ] = mymap[ "Gifts" ]; // new element "Gifts" inserted, "Produce" written } Modified from []/ map example <string,string> <Produce, John> <Gifts, ””> <Bakery, Barbara> <Seafood, Lisa> <Deli, ””> John ”” Barbara> unordered_map example #include <unordered_map> unordered_map<string,string> mymap;
CS2134 // Example from SGI STL documentation struct ltstr { bool operator()(const char* s1,const char* s2)const { return strcmp(s1, s2) < 0; } }; int main() { map<const char*, int, ltstr > months; months["january"] = 31; months["february"] = 28; . . . map<const char*, int, ltstr >::iterator cur; cur = months. find ("june"); map<const char*, int, ltstr >::iterator prev = cur; map<const char*, int, ltstr >::iterator next = cur; ++next; --prev; cout << "Previous (in alphabetical order) is " << (*prev).first << endl; cout << "Next (in alphabetical order) is " << (*next).first << endl; } <string,int> <january,31> <february, 28> <march, 31> <april, 30> <may, 31> <june, 30> <july, 31> <august, 31> <september, 30> <october, 31> <december, 31> <november, 30> 7
You've reached the end of this preview.
• Fall '07
• Hellerstein

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern