27
CS 225 Data Structures Feb. 19 – Traversal Wade Fagen-Ulmschneider

PowerPoint Presentation · PDF file11 12 13 14 15 16 17 a. Traversals - * b + / c d e ... 26 root. 13 10 25 12 37 38 51 40 84 ... PowerPoint Presentation Author: Wade Fagen Created

Embed Size (px)

Citation preview

CS 225Data Structures

Feb. 19 – TraversalWade Fagen-Ulmschneider

Traversals

*-

b

+

/

c

d ea

Traversals

*-

b

+

/

c

d e

template<class T>

void BinaryTree<T>::__Order(TreeNode * root)

{

}

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

a

Traversals

*-

b

+

/

c

d e

template<class T>

void BinaryTree<T>::__Order(TreeNode * root)

{

if (root != NULL) {

______________________;

___Order(root->left);

______________________;

___Order(root->right);

______________________;

}

}

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

a

Traversals

*-

b

+

/

c

d e

template<class T>

void BinaryTree<T>::__Order(TreeNode * root)

{

if (root != NULL) {

______________________;

___Order(root->left);

______________________;

___Order(root->right);

______________________;

}

}

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

a

Traversals

*-

b

+

/

c

d e

template<class T>

void BinaryTree<T>::__Order(TreeNode * root)

{

if (root != NULL) {

______________________;

___Order(root->left);

______________________;

___Order(root->right);

______________________;

}

}

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

a

template<class T>

TreeNode * BinaryTree<T>::_copy

(TreeNode * root) {

if (root != NULL) {

}

}

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

A Broader View

*-

b

+

/

c

d ea

template<class T>

void BinaryTree<T>::_clear(TreeNode * root) {

if (root != NULL) {

}

}

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

A Broader View

*-

b

+

/

c

d ea

A Different Type of Traversal

*-

b

+

/

c

d ea

template<class T>

void BinaryTree<T>::levelOrder(TreeNode * root) {

}

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

A Different Type of Traversal

*-

b

+

/

c

d ea

Traversal vs. SearchTraversal

Search

Search: Breadth First vs. Depth FirstStrategy: Breadth First Search (BFS)

Strategy: Depth First Search (DFS)

Dictionary ADTData is often organized into key/value pairs:

UIN Advising RecordCourse Number Lecture/Lab ScheduleNode Incident EdgesFlight Number Arrival InformationURL HTML Page…

#ifndef DICTIONARY_H

#define DICTIONARY_H

class Dictionary {

public:

private:

};

#endif

Dictionary.h1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Binary Tree as a Search Structure

U

O M

C W

A

T

E S

IN

Binary ___________ Tree (BST)A BST is a binary tree T such that:

13

10 25

12 37

38

51

40 84

8966

95

#ifndef DICTIONARY_H

#define DICTIONARY_H

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:

};

#endif

BST.h1

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

8966

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

8966

95

template<class K, class V>

________________________ _insert(TreeNode *& root, const K & key) {

}

13

10 25

12 37

38

51

40 84

8966

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

8966

95

template<class K, class V>

________________________ _remove(TreeNode *& root, const K & key) {

}

13

10 25

12 37

38

51

40 84

8966

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

8966

95

remove(40);

13

10 25

12 37

38

51

40 84

8966

95

remove(25);

13

10 25

12 37

38

51

40 84

8966

95

remove(10);

13

10 25

12 37

38

51

40 84

8966

95

remove(13);