A N A L Y T I C C O M B I N A T O R I C S P A R T O N E 6. Trees http://aofa.cs.princeton.edu
Review First half of class • Introduced analysis of algoritihms. • Surveyed basic mathematics needed for scientific studies. AN INTRODUCTION TO THE A NALYSIS A LGORITHMS OF • Introduced analytic combinatorics. S E C O N D E D I T I O N R O B E R T S E D G E W I C K P H I L I P P E F L A J O L E T Analysis of Algorithms 1 2 Recurrences 3 Generating Functions 4 Asymptotics 5 Analytic Combinatorics Note: Many applications beyond analysis of algorithms. 2
Orientation Second half of class • Surveys fundamental combinatorial classes. • Considers techniques from analytic combinatorics to study them . AN INTRODUCTION TO THE A NALYSIS A LGORITHMS OF • Includes applications to the analysis of algorithms. S E C O N D E D I T I O N R O B E R T S E D G E W I C K P H I L I P P E F L A J O L E T chapter combinatorial classes type of class type of GF 6 Trees unlabeled OGFs 7 Permutations labeled EGFs 8 Strings and Tries unlabeled OGFs 9 Words and Mappings labeled EGFs Note: Many more examples in book than in lectures. 3
A N A L Y T I C C O M B I N A T O R I C S P A R T O N E 6. Trees •Trees and forests •Binary search trees •Path length OF •Other types of trees http://aofa.cs.princeton.edu 6a.Trees.Trees
Anatomy of a binary tree Definition. A binary tree is an external node or an internal node and two binary trees. level root (depth) 0 1 internal node 2 3 external 4 node leaf 5 6 height h ( t ) 5
Binary tree enumeration (quick review) How many binary trees with N nodes? T 1 = 1 T 2 = 2 T 3 = 5 T 4 = 14 6
Symbolic method: binary trees How many binary trees with N nodes? Atoms type class size GF Class T , the class of all binary trees � � external node 0 1 Size | t |, the number of internal nodes in t � • 1 z internal node � | � | � � � � � � ( � ) = � = OGF � ≥ � � ∈ � “a binary tree is an external node or an internal node connected to two binary trees” � = � � + � × � • × � Construction or � ( � ) = � + �� ( � ) � OGF equation � � � � � � � [ � � ] � ( � ) = ∼ � + � � � �� � 7
Forest and trees Each forest with N nodes corresponds to A tree with N +1 nodes add a root [ � � ] � ( � ) = [ � � + � ] � ( � ) �� ( � ) = � ( � ) GF that GF that enumerates forests enumerates trees 8
Anatomy of a (general) tree Definition. A forest is a sequence of disjoint trees. Definition. A tree is a node (called the root ) connected to the roots of trees in a forest. level root (depth) 0 1 node 2 3 4 leaf height h ( t ) 9
Forest enumeration How many forests with N nodes? F 1 = 1 F 2 = 2 F 3 = 5 F 4 = 14 10
Tree enumeration How many trees with N nodes? G 1 = 1 G 2 = 1 G 3 = 2 G 3 = 5 G 4 = 14 11
Symbolic method: forests and trees How many forests and trees with N nodes? Atoms Class F , the class of all forests type class size GF Size | f |, the number of nodes in f Z node 1 z Class G , the class of all trees Size | g |, the number of nodes in g � = ��� ( � ) ��� � = � × � Construction � � ( � ) = ��� � ( � ) = �� ( � ) OGF equations � − � ( � ) � ( � ) − �� ( � ) � = � Solution � � � � � � � � = � � − � ∼ � � − � � � � = � � = Extract coefficients ∼ √ � + � � √ �� � �� � 12
Forest and binary trees Each forest with N nodes corresponds to "rotation" correspondence A binary tree with N nodes Connect each node to its • left child • right sibling 13
Aside: Drawing a binary tree Approach 1: • y-coordinate: height minus node depth • x-coordinate: inorder node rank 10 Problem: distracting long edges 9 8 7 . . . 0 1 2 3 4 5 . . . . Design decision: Reduce visual clutter by omitting external nodes 14
Aside: Drawing a binary tree Approach 2: • y-coordinate: height minus node depth • x-coordinate: centered and evenly spaced by level Drawing shows tree profile 15
Typical random binary tree shapes (400 nodes) Challenge: characterize analytically 16
A N A L Y T I C C O M B I N A T O R I C S P A R T O N E 6. Trees •Trees and forests •Binary search trees •Path length OF •Other types of trees http://aofa.cs.princeton.edu 6b.Trees.BSTs
Binary search tree (BST) Fundamental data structure in computer science: • Each node has a key, with comparable values. • Keys are all distinct. • Each node’s left subtree has smaller keys. • Each node’s right subtree has larger keys. v smaller larger than v than v Section 3.2 18
BST representation in Java Java definition: A BST is a reference to a root Node. Notes: • Key and Value are generic types. A Node is comprised of four fields: • Key is Comparable. • A Key and a Value. • A reference to the left and right subtree. larger keys smaller keys private class Node BST { private Key key; Node key val private Value val; private Node left, right; public Node(Key key, Value val) { left right this.key = key; this.val = val; } BST with larger keys BST with smaller keys } Binary search tree 19
BST implementation (search) public class BST<Key extends Comparable<Key>, Value> to search for M S { go left private Node root; E X private class Node then right { /* see previous slide */ } A M successful! public Value get(Key key) { Node x = root; C H while (x != null) { int cmp = key.compareTo(x.key); if (cmp < 0) x = x.left; to search for Q else if (cmp > 0) x = x.right; S else if (cmp == 0) return x.val; go left } return null; E X then right } A M public void put(Key key, Value val) then right { /* see next slide */ } C H unsuccessful } 20
BST implementation (insert) public void put(Key key, Value val) { root = put(root, key, val); } to insert Q S go left private Node put(Node x, Key key, Value val) { E X if (x == null) return new Node(key, val); then right int cmp = key.compareTo(x.key); A M if (cmp < 0) x.left = put(x.left, key, val); then right else if (cmp > 0) x.right = put(x.right, key, val); else if (cmp == 0) x.val = val; Q C H return x; } concise, but tricky, then attach recursive code Q here 21
Key fact The shape of a BST depends on the order of insertion of the keys. Best case Typical case Worst case H S A C S E X C A E M X A M E C H H search cost guaranteed ~lg N M Average search cost ? S X Average search cost ~ N /2 (a problem) Reasonable model: Analyze BST built from inserting keys in random order. 22
Typical random BSTs (80 nodes) Challenge: characterize analytically (explain difference from random binary trees) 23
BST shape is a property of permutations , not trees (!) 1 3 2 4 1 1 1 1 3 3 3 2 2 4 1 3 4 2 1 1 1 1 3 3 3 4 2 4 Note: Balanced shapes are more likely. 24
Mapping permutations to trees via BST insertion "result in this tree shape when inserted into an Q. How many permutations map to this tree? initially empty BST" 2 1 3 2 3 1 A. 2 root must be 4 4 2 1 3 5 6 4 2 3 1 5 6 Q. How many permutations map to this tree? 4 2 1 5 3 6 4 2 3 5 1 6 ways to mix 4 2 1 5 6 3 4 2 3 5 6 1 left and right 4 2 5 1 3 6 4 2 5 3 1 6 4 2 5 1 6 3 4 2 5 3 6 1 4 2 5 6 1 3 4 2 5 6 3 1 � � � 4 5 2 1 3 6 4 5 2 3 1 6 · � · � = �� A. � 4 5 2 1 6 3 4 5 2 3 6 1 4 5 2 6 1 3 4 5 2 6 3 1 4 5 6 2 1 3 4 5 6 2 3 1 1, 2, and 3 5 and 6 perms mapping perms mapping on the left on the right to left subtree to right subtree 25
Mapping permutations to trees via BST insertion Q. How many permutations map to a general binary tree t ? root is | t L | + 1 left subtree t L right subtree t R | t R | nodes | t L | nodes A. Let P t be the number of perms that map to t first element | t R | larger | t L | smaller must be � | � � | + | � � | � elements elements | t L | + 1 � � = · � � � · � � � | � � | much, much larger when t L ≈ t R than when t L ≪ t R (explains why balanced shapes are more likely) 26
Two binary tree models that are fundamental (and fundamentally different) BST model • Balanced shapes much more likely. • Probability root is of rank k : 1/ N . Catalan model • Each tree shape equally likely. • Probability root is of rank k : � � � � − � � � � � − � � � � � � � − � + � � − � � � � � � � + � � 27
Recommend
More recommend