cs 225
play

CS 225 Data Structures March 5 5 AVL Applications Wad ade Fag - PowerPoint PPT Presentation

CS 225 Data Structures March 5 5 AVL Applications Wad ade Fag agen-Ulm lmschneid ider AVL Runtime Proof On Friday, we proved an upper-bound on the height of an AVL tree is 2*lg(n) or O( lg(n) ) . Summary ry of f Balanced BST AVL


  1. CS 225 Data Structures March 5 5 – AVL Applications Wad ade Fag agen-Ulm lmschneid ider

  2. AVL Runtime Proof On Friday, we proved an upper-bound on the height of an AVL tree is 2*lg(n) or O( lg(n) ) .

  3. Summary ry of f Balanced BST AVL Trees - Max height: 1.44 * lg(n) - Rotations:

  4. Summary ry of f Balanced BST AVL Trees - Max height: 1.44 * lg(n) - Rotations: Zero rotations on find One rotation on insert O( h ) == O( lg(n) ) rotations on remove Red-Black Trees - Max height: 2 * lg(n) - Constant number of rotations on insert, remove, and find

  5. Why AVL?

  6. Summary ry of f Balanced BST Pros: - Running Time: - Improvement Over: - Great for specific applications:

  7. Summary ry of f Balanced BST Cons: - Running Time: - In-memory Requirement:

  8. Red-Black Trees in C++ ++ C++ provides us a balanced BST as part of the standard library: std::map<K, V> map;

  9. Red-Black Trees in C++ ++ V & std::map<K, V>::operator[]( const K & )

  10. Red-Black Trees in C++ ++ V & std::map<K, V>::operator[]( const K & ) std::map<K, V>::erase( const K & )

  11. Red-Black Trees in C++ ++ iterator std::map<K, V>::lower_bound( const K & ); iterator std::map<K, V>::upper_bound( const K & );

  12. CS 225 -- -- Course Update This weekend, the following grades were updated: • mp1 • mp2* • mp3* • lab_inheritance • lab_quacks • lab_trees

  13. It Iterators Why do we care? DFS dfs(...); 1 2 for ( ImageTraversal::Iterator it = dfs.begin(); it != dfs.end(); ++it ) { 3 std::cout << (*it) << std::endl; 4 }

  14. It Iterators Why do we care? DFS dfs(...); 1 2 for ( ImageTraversal::Iterator it = dfs.begin(); it != dfs.end(); ++it ) { 3 std::cout << (*it) << std::endl; 4 } DFS dfs(...); 1 2 for ( const Point & p : dfs ) { 3 std::cout << p << std::endl; 4 }

  15. It Iterators Why do we care? DFS dfs(...); 1 2 for ( ImageTraversal::Iterator it = dfs.begin(); it != dfs.end(); ++it ) { 3 std::cout << (*it) << std::endl; 4 } DFS dfs(...); 1 2 for ( const Point & p : dfs ) { 3 std::cout << p << std::endl; 4 } ImageTraversal & traversal = /* ... */; 1 2 for ( const Point & p : traversal ) { 3 std::cout << p << std::endl; 4 }

  16. It Iterators ImageTraversal *traversal = /* ... */; 1 2 for ( const Point & p : traversal ) { 3 std::cout << p << std::endl; 4 }

  17. Every ry Data Structure So Far Unsorted Sorted Unsorted Sorted Binary Tree BST AVL Array Array List List Find Insert Remove Traverse

  18. Range-based Searches Q: Consider points in 1D: p = {p 1 , p 2 , …, p n }. …what points fall in [11, 42]? Tree construction:

  19. Range-based Searches Balanced BSTs are useful structures for range-based and nearest-neighbor searches. Q: Consider points in 1D: p = {p 1 , p 2 , …, p n }. …what points fall in [11, 42]? Ex: 3 6 11 33 41 44 55

  20. Range-based Searches Q: Consider points in 1D: p = {p 1 , p 2 , …, p n }. …what points fall in [11, 42]? Ex: 3 6 11 33 41 44 55

  21. Range-based Searches Q: Consider points in 1D: p = {p 1 , p 2 , …, p n }. …what points fall in [11, 42]? Tree construction:

  22. Range-based Searches

  23. Range-based Searches 33 6 44 3 11 41 55 3 6 11 33 41 44

  24. Range-based Searches Q: Consider points in 1D: p = {p 1 , p 2 , …, p n }. …what points fall in [11, 42]? 33 6 44 3 11 41 55 6 11 33 41 44 3

  25. Range-based Searches 33 6 44 3 11 41 55 6 11 33 41 44 3

  26. Running Time 33 6 44 3 11 41 55 6 11 33 41 44 3

  27. Range-based Searches Q: Consider points in 1D: p = {p 1 , p 2 , …, p n }. …what points fall in [11, 42]? Ex: 3 6 11 33 41 44 55

  28. Range-based Searches Consider points in 2D: p = {p 1 , p 2 , …, p n }. Q: What points are in the rectangle: p 2 [ (x 1 , y 1 ), (x 2 , y 2 ) ]? p 5 p 6 p 1 Q: What is the nearest point to (x 1 , y 1 ) ? p 3 p 4 p 7

  29. Range-based Searches Consider points in 2D: p = {p 1 , p 2 , …, p n }. Tree construction: p 2 p 5 p 6 p 1 p 3 p 4 p 7

  30. Range-based Searches p 2 p 5 p 6 p 1 p 7 p 3 p 2 p 3 p 4 p 5 p 6 p 1 p 4 p 7

  31. kD kD-Trees p 2 p 5 p 6 p 1 p 7 p 3 p 2 p 3 p 4 p 5 p 6 p 1 p 4 p 7

  32. kD kD-Trees p 2 p 5 p 6 p 1 p 7 p 3 p 2 p 3 p 4 p 5 p 6 p 1 p 4 p 7

Recommend


More recommend