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

cs 225
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS 225

Data Structures

Fe February 24 – Tr Traversal

G G Carl Evans

slide-2
SLIDE 2

Tr Trees aren’t new:

A X S 2 C 2 5 Y

C S X A 2 2 5 Y

Ø Ø Ø Ø Ø Ø Ø Ø Ø

slide-3
SLIDE 3

Ho How many nul nullptrs?

Theorem: If there are n data items in our representation of a binary tree, then there are ___________ nullptrs.

slide-4
SLIDE 4

Ho How many nul nullptrs?

Base Cases: NULLS(0): NULLS(1): NULLS(2):

slide-5
SLIDE 5

Ho How many nul nullptrs?

Base Cases: NULLS(3):

slide-6
SLIDE 6

Ho How many nul nullptrs?

Induction Hypothesis:

slide-7
SLIDE 7

Ho How many nul nullptrs?

Consider an arbitrary tree T containing k nodes:

slide-8
SLIDE 8

Tr Traversals

*

  • b

+ / c d e a

slide-9
SLIDE 9

Tr Traversals

*

  • b

+ / c d e

template<class T> void BinaryTree<T>::__Order(TreeNode * cur) { } 49 50 51 52 53 54 55 56 57 58

a

slide-10
SLIDE 10

Tr Traversals

*

  • b

+ / 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

slide-11
SLIDE 11

Tr Traversals

*

  • b

+ / 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

slide-12
SLIDE 12

A D A Different T Type o

  • f T

Traversal

*

  • b

+ / c d e a

slide-13
SLIDE 13

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

A D A Different T Type o

  • f T

Traversal

*

  • b

+ / c d e a

slide-14
SLIDE 14

Tr Traversal vs. Search

Traversal Search

slide-15
SLIDE 15

Se Search ch: Br : Breadth F First v

  • vs. D

Depth F First

Strategy: Breadth First Search (BFS) Strategy: Depth First Search (DFS)

slide-16
SLIDE 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 …

slide-17
SLIDE 17

#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

slide-18
SLIDE 18

Bi Binary T Tree a as a a Se Search St Stru ructure

U O M C W A T E S I N

slide-19
SLIDE 19

Bi Binary _ ___________ T Tree ( (BS BST)

A BST is a binary tree T such that:

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

slide-20
SLIDE 20

#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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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

remove(40);

slide-27
SLIDE 27

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

remove(25);

slide-28
SLIDE 28

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

remove(10);

slide-29
SLIDE 29

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

remove(13);