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 < p 2 p 1 < p 3 p 2 < p 3 p 2 p 1 p 3 p 2 < p 3 p 1 < p 3 p 1 p 2 p 3 Observations: p 3 p 1 p 2 p 2 p 3 p 1 p 3 p 2 p 1 p 1 p 3 p 2 1) Insertion sort 2) Number of paths from root to a terminal node must be n!
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 2 k leaves. • • What is the minimum height of a comparison tree for sorting input of size n?
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:
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.
A rooted tree:
Binary tree, recursive definition: A binary tree T is either • OR •
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):
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, T L , T R }, and
Perfect Binary tree: Perfect tree of height h, P h : • P -1 is an empty tree • if h > -1, then P h is {r, T L , T R }, where T L and T R are P h-1 . P 0 : P 2 : P 1 : Check for understanding: How many nodes in a perfect tree of height h?
Complete Binary tree: for any level k in [0,h-1], level k has 2 k nodes, and on level h, all nodes are "pushed to the left." Complete tree of height h, C h : • if h= -1, then C h is {} B • if h > -1, then C h is {r, T L , T R }, and either: U I T L is _______ and T R is ________ L D H E OR A P N O W T L is _______ and T R is ________ http://xlinux.nist.gov/dads//HTML/completeBinaryTree.html Check for understanding: Is every full tree complete? Is every complete tree full?
Rooted, directed, ordered, binary trees 1 template<class T> 2 class tree { 3 public: 4 … 5 private: Tree ADT: 6 struct Node { 7 T data; 8 Node * left; insert 9 Node * left; 10 }; remove 11 Node * root; 12 … traverse 13 };
Theorem: Our implementation of an n item binary tree has ______ null pointers.
Recommend
More recommend