CS 225
Data Structures
Fe February 24 – Tr Traversal
G G Carl Evans
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
Data Structures
Fe February 24 – Tr Traversal
G G Carl Evans
A X S 2 C 2 5 Y
C S X A 2 2 5 Y
Ø Ø Ø Ø Ø Ø Ø Ø Ø
Theorem: If there are n data items in our representation of a binary tree, then there are ___________ nullptrs.
Base Cases: NULLS(0): NULLS(1): NULLS(2):
Base Cases: NULLS(3):
Induction Hypothesis:
Consider an arbitrary tree T containing k nodes:
*
+ / c d e a
*
+ / c d e
template<class T> void BinaryTree<T>::__Order(TreeNode * cur) { } 49 50 51 52 53 54 55 56 57 58
a
*
+ / c d e
template<class T> void BinaryTree<T>::___Order(TreeNode * cur) { if (cur != NULL) { ______________________; ___Order(cur->left); ______________________; ___Order(cur->right); ______________________; } } 49 50 51 52 53 54 55 56 57 58
a
*
+ / c d e
template<class T> void BinaryTree<T>::___Order(TreeNode * cur) { if (cur != NULL) { ______________________; ___Order(cur->left); ______________________; ___Order(cur->right); ______________________; } } 49 50 51 52 53 54 55 56 57 58
a
*
+ / c d e a
template<class T> void BinaryTree<T>::levelOrder(TreeNode * root) { } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
*
+ / c d e a
Traversal Search
Strategy: Breadth First Search (BFS) Strategy: Depth First Search (DFS)
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 …
#pragma once class Dictionary { public: private: // ... };
Dictionary.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
U O M C W A T E S I N
A BST is a binary tree T such that:
13 10 25 12 37 38 51 40 84 89 66 95
#pragma once template <class K, class V> class BST { public: BST(); void insert(const K key, V value); V remove(const K & key); V find(const K & key) const; TreeIterator traverse() const; private: };
BST.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
template<class K, class V> ________________________ _find(TreeNode *& root, const K & key) const { }
13 10 25 12 37 38 51 40 84 89 66 95
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
root
13 10 25 12 37 38 51 40 84 89 66 95
template<class K, class V> ________________________ _insert(TreeNode *& root, const K & key) { }
13 10 25 12 37 38 51 40 84 89 66 95
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
root
13 10 25 12 37 38 51 40 84 89 66 95
template<class K, class V> ________________________ _remove(TreeNode *& root, const K & key) { }
13 10 25 12 37 38 51 40 84 89 66 95
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
root
13 10 25 12 37 38 51 40 84 89 66 95
remove(40);
13 10 25 12 37 38 51 40 84 89 66 95
remove(25);
13 10 25 12 37 38 51 40 84 89 66 95
remove(10);
13 10 25 12 37 38 51 40 84 89 66 95
remove(13);