TREES AND ORDERS OF GROWTH 7

Similar documents
TREES AND ORDERS OF GROWTH 8

TREES AND ORDERS OF GROWTH 7

ORDERS OF GROWTH, DATA ABSTRACTION 4

20 Some terminology regarding trees: Parent node: A node that has branches. Parent nodes can have multiple branches.

CS 61A Orders of Growth & Linked Lists Spring 2018 Discussion 6: March 7, Warmup

Growth & Mutable Data Summer 2017 Discussion 6: July 11, Time and Growth. Big Theta Notation

SEQUENCES AND TREES 4

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Intro. Speed V Growth

TREES AND MUTATION 5

61A LECTURE 16 TREES, ORDERS OF GROWTH. Steven Tang and Eric Tzeng July 22, 2013

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Introduction to the Analysis of Algorithms. Algorithm

Quiz 1 Solutions. (a) If f(n) = Θ(g(n)) and g(n) = Θ(h(n)), then h(n) = Θ(f(n)) Solution: True. Θ is transitive.

CSI33 Data Structures

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

COMP Analysis of Algorithms & Data Structures

Balanced Trees Part One

CSI33 Data Structures

CSC236 Week 5. Larry Zhang

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

COMP Analysis of Algorithms & Data Structures

CSI33 Data Structures

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CSE 100 Advanced Data Structures

Midterm solutions. n f 3 (n) = 3

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Data Structures CSCI C343, Fall 2014

CS:3330 (22c:31) Algorithms

Elementary maths for GMT. Algorithm analysis Part I

CSI33 Data Structures

Data Structures and Algorithms

TREES. Trees - Introduction

Friday Four Square! 4:15PM, Outside Gates

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

CS240 Fall Mike Lam, Professor. Algorithm Analysis

How fast is an algorithm?

Spring 2018 Mentoring 8: March 14, Binary Trees

Last week. Last week. Last week. Today. Binary Trees. CSC148 Intro. to Computer Science. Lecture 8: Binary Trees, BST

Analysis of Algorithms

Multiple-choice (35 pt.)

Binary Search Tree. Revised based on textbook author s notes.

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

Basic Data Structures (Version 7) Name:

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

TREES AND SEQUENCES 3

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Lecture 16: Binary Search Trees

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

CMSC351 - Fall 2014, Homework #2

Last week. Another example. More recursive examples. How about these functions? Recursive programs. CSC148 Intro. to Computer Science

n 1 i = n + i = n + f(n 1)

Binary Trees, Binary Search Trees

EE 368. Week 6 (Notes)

Recitation 9. Prelim Review

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Binary search trees. We can define a node in a search tree using a Python class definition as follows: class SearchTree:

Quiz 1 Practice Problems

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Trees. CSE 373 Data Structures

ASYMPTOTIC COMPLEXITY

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

EECS 203 Spring 2016 Lecture 8 Page 1 of 6

CS 171: Introduction to Computer Science II. Binary Search Trees

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Section 4 SOLUTION: AVL Trees & B-Trees

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Multiway Search Trees. Multiway-Search Trees (cont d)

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

Augmenting Data Structures

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees. Linda Shapiro Spring 2016

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Algorithms. AVL Tree

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Programming II (CS300)

Choice of C++ as Language

CS 350 : Data Structures B-Trees

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Data Structures Question Bank Multiple Choice

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

ASYMPTOTIC COMPLEXITY

COMP Analysis of Algorithms & Data Structures

Trees. (Trees) Data Structures and Programming Spring / 28

CS350: Data Structures B-Trees

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

Section 05: Solutions

Lecture 21: Red-Black Trees

Recursive Data Structures and Grammars

Transcription:

TREES AND ORDERS OF GROWTH 7 COMPUTER SCIENCE 61A October 17, 2013 1 Trees In computer science, trees are recursive data structures that are widely used in various settings. This is a diagram of a simple tree. 7 1 19 3 11 20 2 8 17 4 0 6 17 Notice that the tree branches downward in computer science, the root of a tree starts at the top, and the leaves are at the bottom. Trees consist of three components: an entry, a left child, and a right child. 1. Entry: Each tree houses one item (entry). The data could be numbers, strings, tuples, etc. 2. Left: The left child of the current node. Note that the child is another tree. 3. Right: The right child of the current node. Note that the child is another tree. 1

DISCUSSION 7: TREES AND ORDERS OF GROWTH Page 2 Some terminology regarding trees: Parent node: A node that has children. Parent nodes can have multiple children. Child node: A node that has a parent. A child node can only belong to one parent. Root: The top node. There is only one root. Because every other node branches directly or indirectly from the root, it is possible to start from the root and reach any other node in the tree. The root is, of course, a parent it is the only node that is not a child. For example, the node that contains the 7 at the top is the root. Leaf: Nodes that have no children. For example, the nodes that contain the bottom 4, 0, 6, 17, and 20 are leaves. The node that contains 19 is not a leaf, since it has one child. Subtree: Notice that each child of a parent is itself the root of a smaller tree (for example, the node containing 1 is the root of another tree). This is why trees are recursive data structures: trees are made up of subtrees, which are trees themselves. Depth: How far away a node is from the root. In other words, how many generations away from the root is the specific child node? In the diagram, the node containing 19 has depth 1; the node containing 3 has depth 2. We define the root of a tree to have depth 0. Height: The depth of the lowest leaf. In the diagram, the nodes containing 4, 0, 6, and 17 are all the lowest leaves, and they have depth 3. Thus, the entire tree has height 3. In computer science, there are many different types of trees some vary in the number of children each node has, and others vary in the structure of the tree. 1.1 Our Implementation In CS 61A, we will be working with a special type of tree: the binary tree, which has an extra condition that each node can have at most two children. class Tree: def init (self, entry, left=none, right=none): self.entry = entry self.left = left self.right = right def repr (self): args = repr(self.entry) if self.left or self.right: args +=, {0}, {1}.format(repr(self.left), repr(self.right))

DISCUSSION 7: TREES AND ORDERS OF GROWTH Page 3 return Tree({0}).format(args) 1.2 Questions 1. Define a function square tree(t) that squares every item in t. You can assume that every item is a number. def square_tree(t): """Mutates a Tree t by squaring all its elements""" 2. Define a function height(t) that returns the height of a Tree. The height of a Tree is defined as the length of the longest path from the root node down to a leaf node. If an Tree just consists of a root with no children, its height is 0. If it helps, there is a Python built-in function max that takes an arbitrarily long sequence of numbers and returns the maximum value in the sequence. def height(t): """Returns the height of the Tree t.""" 3. Let s actually define tree size(t), which returns how many items the Tree t contains. Try to solve this without the given size or items property. def tree_size(t): """Returns the number of items in a Tree t."""

DISCUSSION 7: TREES AND ORDERS OF GROWTH Page 4 4. Define the procedure find path that, given an Tree t and an entry entry, returns a tuple containing the nodes along the path required to get from the root of t to entry. If entry is not present in t, return False. Assume that the elements in t are unique. For instance, for the following tree, find path should return: 2 7 15 2 6 >>> find_path(tree_ex, 5) (2, 7, 6, 5) def find_path(t, entry): 5 11 1.3 Binary Search Trees Note that there is a special subtype of binary trees: the binary search tree, which must meet the following conditions: The left subtree of a node contains only nodes with values less than the node s value. The right subtree of a node contains only nodes with values greater than the node s value. The left and right subtrees must also be binary search trees. There must no duplicate nodes.

DISCUSSION 7: TREES AND ORDERS OF GROWTH Page 5 Here is an example of a binary search tree: 9 4 19 2 6 1.4 Questions 1. Write a function that prints out all of the values in a binary search tree in increasing order. def print_inorder(t): 2 Orders of Growth When we talk about the efficiency of a procedure (at least for now), we are often interested in how much more expensive it is to run the procedure with a larger input. That is, as the size of the input grows, how do the speed of the procedure and the space its process occupies grow? For expressing all of these, we use what is called the big Theta notation. For example, if we say the running time of a procedure foo is in Θ(n 2 ), we mean that the running time of the process, R(n), will grow proportionally to the square of the size of the input n. More generally, we can say that foo is in some Θ(f(n)) if there exist some constants k 1 and k 2 such that k 1 f(n) R(n) k 2 f(n) (1) for n > N, where N is sufficiently large. This is a mathematical definition of big Theta notation. To prove that foo is in Θ(f(n)), we only need to find constants k 1 and k 2 where the above holds. There is also another way to express orders of growth: big O notation. This denotes the worst case complexity of a procedure, whereas big Theta notation gives a rough approximation of the actual complexity. Still, big O notation can be useful when it is not possible

DISCUSSION 7: TREES AND ORDERS OF GROWTH Page 6 to find a big Theta. The mathematical definition of big O is, for some values k 1 and n, for n > N, where N is sufficiently large. R(n) k 1 f(n) (2) For example, O(n 2 ) states that a function s worst case run time would be in quadratic time. This does not mean the function will never be slower than quadratic time; in fact, it might very well run in linear or even constant time! Fortunately, in CS 61A, we re not that concerned with rigorous mathematical proofs. (You ll get the painful details in CS 61B!) What we want you to develop in CS 61A is the intuition to guess the orders of growth for certain procedures. 2.1 Kinds of Growth Here are some common orders of growth, ranked from best to worst: Θ(1) constant time takes the same amount of time regardless of input size Θ(log n) logarithmic time Θ(n) linear time Θ(n 3 ), Θ(n 3 ), etc. polynomial time Θ(2 n ) exponential time ( intractable ; these are really, really horrible) 2.2 Orders of Growth in Time Time, for us, basically refers to the number of recursive calls or the number of times the suite of a while loop executes. Intuitively, the more recursive calls we make, the more time it takes to execute the function. If the function contains only primitive procedures like + or, then it is constant time Θ(1). If the function is recursive, you need to: Count the number of recursive calls that will be made, given input n. Count how much time it takes to process the input per recursive call. The answer is usually the product of the above two. For example, let s try to sum all of the items in an rlist recursively. Each addition takes us 3 seconds, and there are 10 items. Therefore, we would take 3 10 = 30 seconds to calculate the summation. If the function contains calls of helper functions that are not constant-time, then you need to take orders of growth of the helper functions into consideration as well. In general, how much time the helper function takes would be included.

DISCUSSION 7: TREES AND ORDERS OF GROWTH Page 7 When we talk about orders of growth, we don t really care about constant factors. So if you get something like Θ(1000000n), this is really Θ(n). We can also usually ignore lower-order terms. For example, if we get something like Θ(n 3 +n 2 +4n+399), we can take it to be Θ(n 3 ). This is because the general shape of the plot for n 3 + n 2 + 4n + 399 is very similar to n 3 as we look towards where n approaches infinity. However, note that in practice, these extra terms and constant factors are important. If a program f takes 1000000n time whereas another, g, takes n time, clearly g is faster (and better). 2.3 Questions What is the order of growth in time for the following functions? 1. def factorial(n): if n == 0: return 1 return n * factorial(n - 1) def sum_of_factorial(n): if n == 0: return 1 else: return factorial(n) + sum_of_factorial(n - 1) 2. def fibonacci(n): if n == 1: return 0 elif n == 2: return 1 else: return fibonacci(n - 1) + fibonacci(n - 2) 3. def fib_iter(n): prev, cur, i = 0, 1, 1 while i < n: prev, curr = curr, prev + curr i += 1 return curr

DISCUSSION 7: TREES AND ORDERS OF GROWTH Page 8 4. def mod_7(n): if n % 7 == 0: return 0 else: return 1 + mod_7(n - 1) 5. Given: def bar(n): if n % 2 == 1: return n + 1 return n def foo(n): if n < 1: return 2 if n % 2 == 0: return foo(n - 1) + foo(n - 2) else: return 1 + foo(n - 2) What is the order of growth of foo(bar(n))? 6. def bonk(n): sum = 0 while n >= 2: sum += n n = n / 2 return sum

DISCUSSION 7: TREES AND ORDERS OF GROWTH Page 9 3 Extra Questions 1. Write a function that creates a balanced binary search tree from a given sorted list. Its runtime should be in Θ(n), where n is the number of nodes in the tree. def list_to_bst(lst): 2. Write a function, find path bst, that takes in a binary search tree and a value. It returns the path from the root to the value. If the value is not in the tree, return False. This function should run in O(log n) time, where n is the number of nodes in the tree. def find_path_bst(t, val):

DISCUSSION 7: TREES AND ORDERS OF GROWTH Page 10 3. Given the following function, f, give the running times of the functions g and h in terms of n. def f(x, y): if x == 0 or y == 0: return 1 if x < y: return f(x, y-1) if x > y: return f(x-1, y) else: return f(x-1, y) + f(x, y-1) def g(n): return f(n, n) def h(n): return f(n, 1)