cs 225

CS 225 Data Structures Fe February 24 Tr Traversal G G Carl - PowerPoint PPT Presentation

CS 225 Data Structures Fe February 24 Tr Traversal G G Carl Evans Tr Trees arent new: C C S X S X A 2 2 5 A 2 2 5 Y Y Ho How many nul nullptrs? Theorem: If there are n data items


  1. CS 225 Data Structures Fe February 24 – Tr Traversal G G Carl Evans

  2. Tr Trees aren’t new: C C S X S X A 2 2 5 A 2 2 5 Ø Ø Ø Ø Ø Ø Ø Y Y Ø Ø

  3. Ho How many nul nullptrs? Theorem: If there are n data items in our representation of a binary tree, then there are ___________ nullptr s.

  4. Ho How many nul nullptrs? Base Cases: NULLS(0): NULLS(1): NULLS(2):

  5. Ho How many nul nullptrs? Base Cases: NULLS(3):

  6. Ho How many nul nullptrs? Induction Hypothesis:

  7. Ho How many nul nullptrs? Consider an arbitrary tree T containing k nodes:

  8. Tr Traversals + - * a d e / b c

  9. Traversals Tr 49 template<class T> 50 void BinaryTree<T>::__Order(TreeNode * cur) + 51 { 52 53 54 - * 55 56 d e / a 57 58 } b c

  10. Tr Traversals 49 template<class T> 50 void BinaryTree<T>::___Order(TreeNode * cur) { + 51 if (cur != NULL) { 52 ______________________; 53 ___Order(cur->left); 54 ______________________; - * 55 ___Order(cur->right); 56 ______________________; d e / a 57 } 58 } b c

  11. Tr Traversals 49 template<class T> 50 void BinaryTree<T>::___Order(TreeNode * cur) { + 51 if (cur != NULL) { 52 ______________________; 53 ___Order(cur->left); 54 ______________________; - * 55 ___Order(cur->right); 56 ______________________; d e / a 57 } 58 } b c

  12. A D A Different T Type o of T Traversal + - * d e / a b c

  13. A Different T A D Type o of T Traversal 1 template<class T> 2 void BinaryTree<T>::levelOrder(TreeNode * root) { + 3 4 5 6 7 8 9 - * 10 11 12 13 d e / a 14 15 16 17 } b c

  14. Tr Traversal vs. Search Traversal Search

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

  16. Dic Dictio tionar ary 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 …

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

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

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

  20. BST.h 1 #pragma once 2 3 template <class K, class 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 TreeIterator traverse() const; 11 12 private: 13 14 15 16 17 18 19 20 21 22 };

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

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

  23. 1 template<class K, class 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 }

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

  25. 1 template<class K, class 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 }

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

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

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

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

Recommend


More recommend