CS 225 Data Structures Fe Feb. 18 It Iterators G G Carl Evans - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Fe Feb. 18 It Iterators G G Carl Evans - - PowerPoint PPT Presentation

CS 225 Data Structures Fe Feb. 18 It Iterators G G Carl Evans It Iter erators Suppose we want to look through every element in our data structure: 8 2 5 Iterators encapsulated access to our data: Cur. Location Cur. Data


slide-1
SLIDE 1

CS 225

Data Structures

Fe

  • Feb. 18 – It

Iterators

G G Carl Evans

slide-2
SLIDE 2

It Iter erators

Suppose we want to look through every element in our data structure:

8 2 5

Ø

slide-3
SLIDE 3

Iterators encapsulated access to our data:

8 2 5

Ø

  • Cur. Location
  • Cur. Data

Next ListNode * index (x, y, z)

slide-4
SLIDE 4

It Iter erators

Every class that implements an iterator has two pieces:

  • 1. [Implementing Class]:
slide-5
SLIDE 5

It Iter erators

Every class that implements an iterator has two pieces:

  • 2. [Implementing Class’ Iterator]:
  • Must have the base class: std::iterator
  • std::iterator requires us to minimally implement:
slide-6
SLIDE 6

Iterators encapsulated access to our data:

8 2 5

Ø

::begin ::end

slide-7
SLIDE 7

#include <list> #include <string> #include <iostream> struct Animal { std::string name, food; bool big; Animal(std::string name = "blob", std::string food = "you", bool big = true) : name(name), food(food), big(big) { /* nothing */ } }; int main() { Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); std::vector<Animal> zoo; zoo.push_back(g); zoo.push_back(p); // std::vector’s insertAtEnd zoo.push_back(b); for ( std::vector<Animal>::iterator it = zoo.begin(); it != zoo.end(); it++ ) { std::cout << (*it).name << " " << (*it).food << std::endl; } return 0; }

stlList.cpp

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

slide-8
SLIDE 8

#include <list> #include <string> #include <iostream> struct Animal { std::string name, food; bool big; Animal(std::string name = "blob", std::string food = "you", bool big = true) : name(name), food(food), big(big) { /* nothing */ } }; int main() { Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); std::vector<Animal> zoo; zoo.push_back(g); zoo.push_back(p); // std::vector’s insertAtEnd zoo.push_back(b); for ( auto it = zoo.begin(); it != zoo.end(); it++ ) { std::cout << (*it).name << " " << (*it).food << std::endl; } return 0; }

stlList.cpp

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

slide-9
SLIDE 9

#include <list> #include <string> #include <iostream> struct Animal { std::string name, food; bool big; Animal(std::string name = "blob", std::string food = "you", bool big = true) : name(name), food(food), big(big) { /* none */ } }; int main() { Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); std::vector<Animal> zoo; zoo.push_back(g); zoo.push_back(p); // std::vector’s insertAtEnd zoo.push_back(b); for ( const Animal & animal : zoo ) { std::cout << animal.name << " " << animal.food << std::endl; } return 0; }

stlList.cpp

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

slide-10
SLIDE 10

Fo For Each and Iterators

for ( const TYPE & variable : collection ) { // ... } std::vector<Animal> zoo; … for ( const Animal & animal : zoo ) { std::cout << animal.name << " " << animal.food << std::endl; } 14 … 20 21 22

slide-11
SLIDE 11

Fo For Each and Iterators

for ( const TYPE & variable : collection ) { // ... } std::vector<Animal> zoo; … for ( const Animal & animal : zoo ) { std::cout << animal.name << " " << animal.food << std::endl; } 14 … 20 21 22 std::multimap<std::string, Animal> zoo; … for ( const Animal & animal : zoo ) { std::cout << animal.name << " " << animal.food << std::endl; } … … 20 21 22

slide-12
SLIDE 12

Tr Trees

“The most important non-linear data structure in computer science.”

  • David Knuth, The Art of Programming, Vol. 1

A tree is:

slide-13
SLIDE 13

Mo More e Specif ecific ic Trees ees

We’ll focus on binary trees:

  • A binary tree is rooted – every node can be reached via

a path from the root

a b d g h j c e i f

slide-14
SLIDE 14

Mo More e Specif ecific ic Trees ees

We’ll focus on binary trees:

  • A binary tree is acyclic – there are no cycles within the

graph

a b d g h j c e i f

slide-15
SLIDE 15

Mo More e Specif ecific ic Trees ees

We’ll focus on binary trees:

  • A binary tree contains two or fewer children – where
  • ne is the “left child” and
  • ne is the “right child”:

a b d g h j c e i f

slide-16
SLIDE 16

Tr Tree Terminology

  • What’s the longest English word you can make using the

vertex labels in the tree (repeats allowed)?

a b d g h j c e i f

slide-17
SLIDE 17

Tr Tree Terminology

  • Find an edge that is not on the longest path in the tree. Give that edge a

reasonable name.

  • One of the vertices is called the root of the tree. Which one?
  • Make an “word” containing the names of the vertices that

have a parent but no sibling.

  • How many parents does each vertex have?
  • Which vertex has the fewest children?
  • Which vertex has the most ancestors?
  • Which vertex has the most descendants?
  • List all the vertices is b’s left subtree.
  • List all the leaves in the tree.

b d g h j c e i f a

slide-18
SLIDE 18

Tr Tree Terminology

  • Find an edge that is not on the longest path in the tree. Give that edge a

reasonable name.

  • One of the vertices is called the root of the tree. Which one?
  • Make an “word” containing the names of the vertices that

have a parent but no sibling.

  • How many parents does each vertex have?
  • Which vertex has the fewest children?
  • Which vertex has the most ancestors?
  • Which vertex has the most descendants?
  • List all the vertices is b’s left subtree.
  • List all the leaves in the tree.

b d g h j c e i f a

slide-19
SLIDE 19

Tr Tree Terminology

  • Find an edge that is not on the longest path in the tree. Give that edge a

reasonable name.

  • One of the vertices is called the root of the tree. Which one?
  • Make an “word” containing the names of the vertices that

have a parent but no sibling.

  • How many parents does each vertex have?
  • Which vertex has the fewest children?
  • Which vertex has the most ancestors?
  • Which vertex has the most descendants?
  • List all the vertices is b’s left subtree.
  • List all the leaves in the tree.

b d g h j c e i f a

slide-20
SLIDE 20

Tr Tree Terminology

  • Find an edge that is not on the longest path in the tree. Give that edge a

reasonable name.

  • One of the vertices is called the root of the tree. Which one?
  • Make an “word” containing the names of the vertices that

have a parent but no sibling.

  • How many parents does each vertex have?
  • Which vertex has the fewest children?
  • Which vertex has the most ancestors?
  • Which vertex has the most descendants?
  • List all the vertices is b’s left subtree.
  • List all the leaves in the tree.

b d g h j c e i f a

slide-21
SLIDE 21

Tr Tree Terminology

  • Find an edge that is not on the longest path in the tree. Give that edge a

reasonable name.

  • One of the vertices is called the root of the tree. Which one?
  • Make an “word” containing the names of the vertices that

have a parent but no sibling.

  • How many parents does each vertex have?
  • Which vertex has the fewest children?
  • Which vertex has the most ancestors?
  • Which vertex has the most descendants?
  • List all the vertices is b’s left subtree.
  • List all the leaves in the tree.

b d g h j c e i f a

slide-22
SLIDE 22

Bi Binary T Tree – De Defin ined ed

A binary tree T is either:

  • OR
  • A

X S 2 C 2 5

slide-23
SLIDE 23

Tr Tree Property: height

height(T): length of the longest path from the root to a leaf Given a binary tree T: height(T) =

A X S 2 C 2 5

slide-24
SLIDE 24

Tr Tree Property: full

A tree F is full if and only if: 1. 2.

A X S 2 C 2 5

slide-25
SLIDE 25

Tr Tree Property: perfect

A perfect tree P is: 1. 2.

A X S 2 C 2 5

slide-26
SLIDE 26

Tr Tree Property: complete

Conceptually: A perfect tree for every level except the last, where the last level if “pushed to the left”. Slightly more formal: For any level k in [0, h-1], k has 2k nodes. For level h, all nodes are “pushed to the left”.

A X S 2 C 2 5 Y Z

slide-27
SLIDE 27

Tr Tree Property: complete

A complete tree C of height h, Ch:

  • 1. C-1 = {}
  • 2. Ch (where h>0) = {r, TL, TR} and either:

TL is __________ and TR is _________ OR TL is __________ and TR is _________

A X S 2 C 2 5 Y Z

slide-28
SLIDE 28

Tr Tree Property: complete

Is every full tree complete? If every complete tree full?

A X S 2 C 2 5 Y Z