cs 225
play

CS 225 Data Structures Oc October 16 AV AVL Applications G G - PowerPoint PPT Presentation

CS 225 Data Structures Oc October 16 AV AVL Applications G G Carl Evans AV AVL Tree Analysis We know: insert, remove and find runs in: __________. We will argue that: h is _________. AV AVL Tree Analysis n, number of nodes h, height


  1. CS 225 Data Structures Oc October 16 – AV AVL Applications G G Carl Evans

  2. AV AVL Tree Analysis We know: insert, remove and find runs in: __________. We will argue that: h is _________.

  3. AV AVL Tree Analysis n, number of nodes h, height n, number of nodes h, height • The number of nodes in the tree, f -1 (h) , will always be greater than c × g -1 (h) for all values where n > k .

  4. Pl Plan an of Acti tion Since our goal is to find the lower bound on n given h , we can begin by defining a function given h which describes the smallest number of nodes in an AVL tree of height h :

  5. Si Simplify t the R Recu curr rrence ce N(h) = 1 + N(h - 1) + N(h - 2)

  6. St State a a T Theor orem Theorem: An AVL tree of height h has at least __________. Proof: I. Consider an AVL tree and let h denote its height. II. Case: ______________ An AVL tree of height ____ has at least ____ nodes.

  7. Pr Prove ve a Theorem III. Case: ______________ An AVL tree of height ____ has at least ____ nodes.

  8. Pr Prove ve a Theorem IV. Case: ______________ By an Inductive Hypothesis (IH): We will show that: An AVL tree of height ____ has at least ____ nodes.

  9. Pr Prove ve a Theorem V. Using a proof by induction, we have shown that: …and inverting:

  10. AV 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) ) : N(h) := Minimum # of nodes in an AVL tree of height h N(h) = 1 + N(h-1) + N(h-2) > 1 + 2 h-1/2 + 2 h-2/2 > 2 × 2 h-2/2 = 2 h-2/2+1 = 2 h/2 Theorem #1: Every AVL tree of height h has at least 2 h/2 nodes.

  11. AV 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) ) : # of nodes (n) ≥ N(h) > 2 h/2 n > 2 h/2 lg(n) > h/2 2 × lg(n) > h h < 2 × lg(n) , for h ≥ 1 Proved: The maximum number of nodes in an AVL tree of height h is less than 2 × lg(n).

  12. Su Summary of of Ba Balance ced BS BST AVL Trees - Max height: 1.44 * lg(n) - Rotations:

  13. Su Summary of of Ba Balance ced BS 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 (max 2), remove (max 3).

  14. Wh Why Balanced BST?

  15. Su Summary of of Ba Balance ced BS BST Pros: - Running Time: - Improvement Over: - Great for specific applications:

  16. Su Summary of of Ba Balance ced BS BST Cons: - Running Time: - In-memory Requirement:

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

  18. Re Red-Bl Black T Trees i in C+ C++ V & std::map<K, V>::operator[]( const K & )

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

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

  21. CS 225 CS 225 -- -- Cou Course U Update Your grades can now be viewed on moodle (https://learn.illinois.edu/) We will discuss the grades for the course as a whole (ex: average, etc) in lecture on Wednesday. 385

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

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

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

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

  26. Ra Range-ba base sed d Searche hes Q: Consider points in 1D: p = {p 1 , p 2 , …, p n }. …what points fall in [11, 42]? Tree construction:

  27. Ra Range-ba base sed d Searche hes 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: 6 11 41 44 3 33 55

  28. Ra Range-ba base sed d Searche hes Q: Consider points in 1D: p = {p 1 , p 2 , …, p n }. …what points fall in [11, 42]? Ex: 6 11 41 44 3 33 55

  29. Ra Range-ba base sed d Searche hes Q: Consider points in 1D: p = {p 1 , p 2 , …, p n }. …what points fall in [11, 42]? Tree construction:

  30. Ra Range-ba base sed d Searche hes

  31. Ra Range-ba base sed d Searche hes 33 6 44 3 11 41 55 3 6 11 33 41 44

  32. Ra Range-ba base sed d Searche hes 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 3 6 11 33 41 44

  33. Ra Range-ba base sed d Searche hes 33 6 44 3 11 41 55 3 6 11 33 41 44

  34. Ru Running T Time 33 6 44 3 11 41 55 3 6 11 33 41 44

  35. Ra Range-ba base sed d Searche hes Q: Consider points in 1D: p = {p 1 , p 2 , …, p n }. …what points fall in [11, 42]? Ex: 6 11 41 44 3 33 55

  36. Ra Range-ba base sed d Searche hes 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

  37. Ra Range-ba base sed d Searche hes 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

  38. Ra Range-ba base sed d Searche hes p 2 p 5 p 6 p 1 p 7 p 3 p 1 p 2 p 3 p 4 p 5 p 6 p 4 p 7

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

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

Recommend


More recommend