CS 225 Data Structures Fe February 26 Bi Binary Sea earch Tree - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Fe February 26 Bi Binary Sea earch Tree - - PowerPoint PPT Presentation

CS 225 Data Structures Fe February 26 Bi Binary Sea earch Tree ee (BS BST) G G Carl Evans Tr Traversal vs. Search Traversal vs. Search: Traversal visits every node in the tree exactly once. Search finds one element in the


slide-1
SLIDE 1

CS 225

Data Structures

Fe February 26 – Bi Binary Sea earch Tree ee (BS BST)

G G Carl Evans

slide-2
SLIDE 2

Tr Traversal vs. Search

Traversal vs. Search:

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

Se Search ch: Br : Breadth F First v

  • vs. D

Depth F First

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

slide-4
SLIDE 4

Se Search ch Ru Running T Times on

  • n a

a Bi Binary T Tree

U O M C W A T E S I N

slide-5
SLIDE 5

Dic Dictio tionar ary ADT

Data is often organized into key/value pairs: Word è Definition Course Number è Lecture/Lab Schedule Node è Incident Edges Flight Number è Arrival Information URL è HTML Page …

slide-6
SLIDE 6

#pragma once 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-7
SLIDE 7

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

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

#pragma once template <typename K, typename V> class BST { public: BST(); void insert(const & K key, V value); V remove(const K & key); V find(const K & key) const; private: struct TreeNode { TreeNode *left, *right; K key; V value; TreeNode(const K & k, const V & v) : key(k), value(v), left(NULL), right(NULL) { } }; TreeNode *head_; };

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

template<typename K, typename V> ________________________ find(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-11
SLIDE 11

template<typename K, typename V> ________________________ _find(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-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(25);

slide-18
SLIDE 18

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

remove(40);

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);