CS 225 Data Structures Feb. 21 Binary Search Tre ree Wad ade Fag - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Feb. 21 Binary Search Tre ree Wad ade Fag - - PowerPoint PPT Presentation

CS 225 Data Structures Feb. 21 Binary Search Tre ree Wad ade Fag agen-Ulm lmschneid ider Traversal vs. . Search Traversal vs. Search: Traversal visits every node in the tree exactly once. Search finds one element in the tree.


slide-1
SLIDE 1

CS 225

Data Structures

  • Feb. 21 – Binary Search Tre

ree

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2
slide-3
SLIDE 3

Traversal vs. . Search

Traversal vs. Search:

  • Traversal visits every node in the tree exactly once.
  • Search finds one element in the tree.
slide-4
SLIDE 4

Search: Breadth First vs. . Depth First

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

slide-5
SLIDE 5

Running Times on a Binary ry Tree

U O M C W A T E S I N

slide-6
SLIDE 6

Dictionary ry 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-7
SLIDE 7

#ifndef DICTIONARY_H #define DICTIONARY_H class Dictionary { public: private: }; #endif

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-8
SLIDE 8

Binary ry Tree as a Search Structure

U O M C W A T E S I N

slide-9
SLIDE 9

Binary ry ___________ Tree (B (BST)

A BST is a binary tree T such that:

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

slide-10
SLIDE 10

#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: struct TreeNode { TreeNode *left, *right; K & key; V & value; TreeNode(K & k, V & v) : key(k), value(v), left(NULL), right(NULL) { } }; }; #endif

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-11
SLIDE 11

template<typename K, typename 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-12
SLIDE 12

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

slide-13
SLIDE 13

template<typename K, typename 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-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

template<typename K, typename 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-17
SLIDE 17

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

remove(40);

slide-18
SLIDE 18

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

remove(25);

slide-19
SLIDE 19

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

remove(10);

slide-20
SLIDE 20

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

remove(13);