Announcements PA1 available, due 01/28, 11:59p. HW2 available, due - - PowerPoint PPT Presentation

announcements
SMART_READER_LITE
LIVE PREVIEW

Announcements PA1 available, due 01/28, 11:59p. HW2 available, due - - PowerPoint PPT Presentation

Announcements PA1 available, due 01/28, 11:59p. HW2 available, due 02/05, 11:59p. MT1 2/4, 7-9p. Lower bound on sorting: Spose you have an unsorted array of length 3 Need to produce a result for every possible arrangement (p 1 p 2 p 3 ) p 1


slide-1
SLIDE 1

Announcements –

PA1 available, due 01/28, 11:59p. HW2 available, due 02/05, 11:59p. MT1 2/4, 7-9p. Lower bound on sorting:

Spose you have an unsorted array of length 3 Need to produce a result for every possible arrangement (p1 p2 p3)

p1 < p2 p2< p3 p1p2p3 p1 < p3 p1< p3 p3p2p1 p2 < p3 p1p3p2 p3p1p2 p2p1p3 p2p3p1

Observations: 1) Insertion sort 2) Number of paths from root to a terminal node must be n!

slide-2
SLIDE 2

Lower bounds on (< based) sorting algorithms (n items):

  • Every permutation must be differentiated in the algorithm

_____________________

  • Comparisons induce a binary tree.
  • Observe that a binary tree of height k has at most 2k leaves.
  • What is the minimum height of a comparison tree for sorting

input of size n?

slide-3
SLIDE 3

Trees:

“… most important nonlinear structure[s] in computer science.”

  • - Donald Knuth, Art of Computer Programming Vol 1

A tree: _____________________________________ We’ll study more specific trees:

slide-4
SLIDE 4

Tree terminology:

  • What’s the longest English 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. Fo For the e res est of the e ex exer ercises es, as assume e the e tree ee is rooted ed.

  • One of the vertices is called the “root” of the tree. Guess which one it is.
  • Make an English 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-5
SLIDE 5

A rooted tree:

slide-6
SLIDE 6

Binary tree, recursive definition:

A binary tree T is either

  • OR
slide-7
SLIDE 7

An (important) example of a function on a binary tree: height(r) -- length of longest path from root r to a leaf

Given a binary tree T, write a recursive defn of the height of T, height(T):

slide-8
SLIDE 8

Full Binary tree: a tree in which every node has 2 or 0 children

F is a full binary tree if and only if:

  • F={} OR,
  • F={r, TL, TR}, and
slide-9
SLIDE 9

Perfect Binary tree:

Check for understanding:

How many nodes in a perfect tree of height h?

P0: P2: P1:

Perfect tree of height h, Ph:

  • P-1 is an empty tree
  • if h > -1, then Ph is {r, TL, TR}, where TL and TR are Ph-1.
slide-10
SLIDE 10

Complete Binary tree: for any level k in [0,h-1], level k has 2k nodes, and on level h,

all nodes are "pushed to the left." http://xlinux.nist.gov/dads//HTML/completeBinaryTree.html

Check for understanding:

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

Complete tree of height h, Ch:

  • if h= -1, then Ch is {}
  • if h > -1, then Ch is {r, TL, TR}, and either:

TL is _______ and TR is ________ OR TL is _______ and TR is ________

I U L A P D N O H W E B

slide-11
SLIDE 11

Rooted, directed, ordered, binary trees

Tree ADT: insert remove traverse

1 2 3 4 5 6 7 8 9 10 11 12 13 template<class T> class tree { public: … private: struct Node { T data; Node * left; Node * left; }; Node * root; … };

slide-12
SLIDE 12

Theorem: Our implementation of an n item binary tree has ______ null pointers.