CS 225 Data Structures Fe February 24 – Tr Traversal G G Carl Evans
Tr Trees aren’t 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 in our representation of a binary tree, then there are ___________ nullptr s.
Ho How many nul nullptrs? Base Cases: NULLS(0): NULLS(1): NULLS(2):
Ho How many nul nullptrs? Base Cases: NULLS(3):
Ho How many nul nullptrs? Induction Hypothesis:
Ho How many nul nullptrs? Consider an arbitrary tree T containing k nodes:
Tr Traversals + - * a d e / b c
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
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
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
A D A Different T Type o of T Traversal + - * d e / a b c
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
Tr Traversal vs. Search Traversal Search
Se Search ch: Br : Breadth F First v vs. D Depth F First Strategy: Breadth First Search (BFS) Strategy: Depth First Search (DFS)
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 …
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 };
Bi Binary T Tree a as a a Se Search St Stru ructure A U T O M E S C W N I
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
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 };
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 }
38 13 51 10 25 40 84 12 37 66 89 95
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 }
38 13 51 10 25 40 84 12 37 66 89 95
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 }
38 13 51 10 25 40 84 12 37 66 89 95 remove(40);
38 13 51 10 25 40 84 12 37 66 89 95 remove(25);
38 13 51 10 25 40 84 12 37 66 89 95 remove(10);
38 13 51 10 25 40 84 12 37 66 89 95 remove(13);
Recommend
More recommend