cs 225
play

CS 225 Data Structures Fe February 26 Bi Binary Sea earch Tree - PowerPoint PPT Presentation

CS 225 Data Structures Fe February 26 Bi Binary Sea earch Tree ee (BS BST) G G Carl Evans Tr Traversal vs. Search Traversal vs. Search: Traversal visits every node in the tree exactly once. Search finds one element in the


  1. CS 225 Data Structures Fe February 26 – Bi Binary Sea earch Tree ee (BS BST) G G Carl Evans

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

  3. Se Search ch: Br : Breadth F First v vs. D Depth F First Strategy: Depth First Search (DFS) / Traversal Strategy: Breadth First Search (BFS) / Traversal

  4. Search Se ch Ru Running T Times on on a a Bi Binary T Tree A U T O M E S C W N I

  5. Dic Dictio tionar ary ADT Data is often organized into key/value pairs: Word è Definition Course Number è Lecture/Lab Schedule Node è Incident Edges Flight Number è Arrival Information URL è HTML Page …

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

  7. Bi Binary T Tree a as a a Se Search St Stru ructure A U T O M E S C W N I

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

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

  10. 1 template<typename K, typename V> 2 ________________________ find(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 66 89 22 23 24 95 25 26 }

  11. 1 template<typename K, typename V> 2 ________________________ _find(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 66 89 22 23 24 95 25 26 }

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

  13. 1 template<typename K, typename V> 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 66 89 22 23 24 95 25 26 }

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

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

  16. 1 template<typename K, typename V> 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 66 89 22 23 24 95 25 26 }

  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(40);

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

  20. 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