cs 225
play

CS 225 Data Structures Feb. 21 Binary Search Tre ree Wad ade Fag - PowerPoint PPT Presentation

CS 225 Data Structures Feb. 21 Binary Search Tre ree Wad ade Fag agen-Ulm lmschneid ider Traversal vs. . Search Traversal vs. Search: Traversal visits every node in the tree exactly once. Search finds one element in the tree.


  1. CS 225 Data Structures Feb. 21 – Binary Search Tre ree Wad ade Fag agen-Ulm lmschneid ider

  2. Traversal vs. . Search Traversal vs. Search: • Traversal visits every node in the tree exactly once. • Search finds one element in the tree.

  3. Search: Breadth First vs. . Depth First Strategy: Breadth First Search (BFS) / Traversal Strategy: Depth First Search (DFS) / Traversal

  4. Running Times on a Binary ry Tree A U T O M E S C W N I

  5. Dictionary ry ADT Data is often organized into key/value pairs: UIN  Advising Record Course Number  Lecture/Lab Schedule Node  Incident Edges Flight Number  Arrival Information URL  HTML Page …

  6. Dictionary.h 1 #ifndef DICTIONARY_H 2 #define DICTIONARY_H 3 4 5 class Dictionary { 6 public: 7 8 9 10 11 12 13 14 15 16 private: 17 18 19 20 }; 21 22 #endif

  7. Binary ry Tree as a Search Structure A U T O M E S C W N I

  8. Binary ry ___________ Tree (B (BST) A BST is a binary tree T such that: 38 13 51 10 25 40 84 12 37 89 66 95

  9. BST.h 1 #ifndef DICTIONARY_H 2 #define DICTIONARY_H 3 4 template <class K, class V> 5 class BST { 6 public: 7 BST(); 8 void insert(const K key, V value); 9 V remove(const K & key); 10 V find(const K & key) const; 11 TreeIterator traverse() const; 12 private: 13 struct TreeNode { 14 TreeNode *left, *right; 15 K & key; 16 V & value; 17 TreeNode(K & k, V & v) : key(k), value(v), left(NULL), 18 right(NULL) { } 19 }; 20 }; 21 22 #endif

  10. template<typename K, typename V> 1 2 ________________________ _find(TreeNode *& root, const K & key) const { 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 }

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

  12. template<typename K, typename V> 1 2 ________________________ _insert(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 }

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

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

  15. 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 }

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

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

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

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

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