CS 225 Data Structures Feb. 14 Tre rees Wad ade Fag agen-Ulm - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Feb. 14 Tre rees Wad ade Fag agen-Ulm - - PowerPoint PPT Presentation

CS 225 Data Structures Feb. 14 Tre rees Wad ade Fag agen-Ulm lmschneid ider Lecture Resources Queue.h 4 template <class QE> 5 class Queue { 6 public: 7 class QueueIterator : public


slide-1
SLIDE 1

CS 225

Data Structures

  • Feb. 14 – Tre

rees

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2
slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5

Lecture Resources

slide-6
SLIDE 6

template <class QE> class Queue { public:

class QueueIterator : public std::iterator<std::bidirectional_iterator_tag, QE> { public: QueueIterator(unsigned index); QueueIterator& operator++(); bool operator==(const QueueIterator &other); bool operator!=(const QueueIterator &other); QE& operator*(); QE* operator->(); private: int location_; };

/* ... */ private: QE* arr_; unsigned capacity_, count_, entry_, exit_; };

Queue.h

4 5 6

7 8 9 10 11 12 13 14 15 16 17

18 19 20 21 22 23 24 25 26

slide-7
SLIDE 7

Big Id Ideas

How does the Queue and the QueueIterator interact?

slide-8
SLIDE 8

Trees

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

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

A tree is:

slide-9
SLIDE 9

A Rooted Tree

“Mario Family Line” <http://limitbreak.gameriot.com/blogs/ Caveat-Emptor/Mario-Family-Line>

slide-10
SLIDE 10
slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13

More Specific Trees

We’ll focus on binary trees:

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

a path from the root

slide-14
SLIDE 14

More Specific Trees

We’ll focus on binary trees:

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

graph

slide-15
SLIDE 15

More Specific Trees

We’ll focus on binary trees:

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

Tree Terminology

  • What’s the longest “word” you can make using the vertex labels in the tree (repeats

allowed)?

slide-17
SLIDE 17

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.
slide-18
SLIDE 18

Tree Terminology

  • What’s the longest “word” you can make using the vertex labels in the tree (repeats

allowed)?

  • 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.
slide-19
SLIDE 19

Tree Terminology

  • What’s the longest “word” you can make using the vertex labels in the tree (repeats

allowed)?

  • 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.
slide-20
SLIDE 20

Tree Terminology

  • What’s the longest “word” you can make using the vertex labels in the tree (repeats

allowed)?

  • 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.
slide-21
SLIDE 21

Tree Terminology

  • What’s the longest “word” you can make using the vertex labels in the tree (repeats

allowed)?

  • 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.
slide-22
SLIDE 22

Binary ry Tree – Defined

A binary tree T is either:

  • OR
  • A

X S 2 C 2 5

slide-23
SLIDE 23

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

Tree Property: fu full

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

A X S 2 C 2 5

slide-25
SLIDE 25

Tree Property: perfect

A perfect tree P is: 1. 2.

A X S 2 C 2 5

slide-26
SLIDE 26

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

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

Tree Property: complete

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

A X S 2 C 2 5 Y Z