cs 225
play

CS 225 Data Structures Feb. 23 BST Rem emove Wad ade Fag - PowerPoint PPT Presentation

CS 225 Data Structures Feb. 23 BST Rem emove Wad ade Fag agen-Ulm lmschneid ider template<typename K, typename V> 1 2 3 void BST::_insert(TreeNode *& root, K & key, V & value) { 4 TreeNode *t = _find(root, key);


  1. CS 225 Data Structures Feb. 23 – BST Rem emove Wad ade Fag agen-Ulm lmschneid ider

  2. template<typename K, typename V> 1 2 3 void BST::_insert(TreeNode *& root, K & key, V & value) { 4 TreeNode *t = _find(root, key); 5 t = new TreeNode(key, value); 6 } root_ 38 13 51 10 25 40 84 12 37 89 66 95

  3. template<typename K, typename V> 1 2 3 void BST::_insert(TreeNode *& root, K & key, V & value) { 4 TreeNode *t = _find(root, key); 5 t = new TreeNode(key, value); 6 } root_ 38 13 51 10 25 40 84 12 37 89 66 95

  4. 38 13 51 10 25 40 84 12 37 66 89 95

  5. template<typename K, typename V> 1 2 ________________________ _remove(TreeNode *& root, const K & key) { 3 4 5 6 7 8 9 10 root 11 12 38 13 14 15 13 51 16 17 18 10 25 40 84 19 20 21 12 37 89 66 22 23 24 95 25 26 }

  6. 38 13 51 10 25 40 84 12 37 66 89 95 remove(40);

  7. 38 13 51 10 25 40 84 12 37 66 89 95 remove(25);

  8. 38 13 51 10 25 40 84 12 37 66 89 95 remove(10);

  9. 38 13 51 10 25 40 84 12 37 66 89 95 remove(13);

  10. BST Analysis – Running Time BST Worst Case Operation find insert delete traverse

  11. BST Analysis Every operation that we have studied on a BST depends on the height of the tree: O(h) . …what is this in terms of n , the amount of data? We need a relationship between h and n : f(n) ≤ h ≤ g(n)

  12. BST Analysis Q: What is the maximum number of nodes in a tree of height h ? 22 S X A 2 2 5

  13. BST Analysis Q: What is the minimum number of nodes in a tree of height h ? 22 S X What is the maximum height for a tree of n nodes? A 2 2 5

  14. BST Analysis Therefore, for all BST: Lower bound: U pper bound:

  15. BST Analysis The height of a BST depends on the order in which the data is inserted into it. ex: 1 3 2 4 5 7 6 vs. 4 2 3 6 7 1 5 Q: How many different ways are there to insert keys into a BST? Q: What is the average height of all the arrangements?

  16. BST Analysis Q: How many different ways are there to insert keys into a BST? Q: What is the average height of all the arrangements?

  17. BST Analysis – Running Time BST BST Sorted array Sorted List Average case Worst case Operation find insert delete traverse

  18. Height-Balanced Tree What tree makes you happier? 5 7 7 5 9 9 Height balance: b = height(T L ) - height(T R ) A tree is height balanced if:

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend