SEQUENCES AND TREES 4

Similar documents
TREES AND SEQUENCES 3

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

Trees & Sequences Spring 2018 Discussion 3: February 14, 2018 Solutions. 1 Sequences and Lists

TREES AND MUTATION 5

TREES AND ORDERS OF GROWTH 7

Lists, Mutability, ADTs, and Trees Spring 2019 Guerrilla Section 2: March 2, 2019 Solutions. 1 Sequences. Questions. lst = [1, 2, 3, 4, 5] lst[1:3]

Lists, Mutability, ADTs, and Trees Spring 2019 Guerrilla Section 2: March 2, Sequences. Questions. lst = [1, 2, 3, 4, 5] lst[1:3]

TREES AND ORDERS OF GROWTH 7

LINKED LISTS AND MIDTERM REVIEW 6

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

TREES AND ORDERS OF GROWTH 8

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

TREES. Trees - Introduction

Trees. Tree Structure Binary Tree Tree Traversals

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

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

Structure and Interpretation of Computer Programs

DATA ABSTRACTION AND SEQUENCES 3

Self-Balancing Search Trees. Chapter 11

CS F-11 B-Trees 1

CS24 Week 8 Lecture 1

CSE 230 Intermediate Programming in C and C++ Binary Tree

Data Structures and Algorithms

Structure and Interpretation of Computer Programs

Module 9: Binary trees

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

CS 206 Introduction to Computer Science II

STREAMS, ITERATORS, AND BINARY TREES 10

CSC148 Week 6. Larry Zhang

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Lower Bound on Comparison-based Sorting

Structure and Interpretation of Computer Programs

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

Data Structures in Java

Trees. Eric McCreath

Binary Trees

Trees. See Chapter 18 of Weiss

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

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CSE 214 Computer Science II Introduction to Tree

CS350: Data Structures B-Trees

CSC/MAT-220: Lab 6. Due: 11/26/2018

(2,4) Trees. 2/22/2006 (2,4) Trees 1

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))

Programming Languages and Techniques (CIS120)

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Structure and Interpretation of Computer Programs

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Binary Trees, Binary Search Trees

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

Design and Analysis of Algorithms Lecture- 9: B- Trees

CSC148H Week 8. Sadia Sharmin. July 12, /29

Spring 2018 Mentoring 8: March 14, Binary Trees

Midterm 2 Review Fall 2017 October 13, Lists & Tree Recursion. Instructions

Chapter 20: Binary Trees

TREES Lecture 12 CS2110 Spring 2018

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

BM267 - Introduction to Data Structures

20. Inheritance and Polymorphism

Programming II (CS300)

Trees. Truong Tuan Anh CSE-HCMUT

1 A node in a tree. Trees. Datatypes ISC

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

CMPT 225. Red black trees

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

TREES Lecture 12 CS2110 Spring 2019

CS 350 : Data Structures B-Trees

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

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Binary Trees and Binary Search Trees

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

CMPT 225. Binary Search Trees

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Lecture 16: Binary Search Trees

Balanced Search Trees

CS115 - Module 10 - General Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees!

Trees. CSE 373 Data Structures

Tree Structures. A hierarchical data structure whose point of entry is the root node

TREES Lecture 12 CS2110 Fall 2016

Algorithms and Data Structures CS-CO-412

Module 8: Binary trees

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

CS115 - Module 8 - Binary trees

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Red-Black trees are usually described as obeying the following rules :

Friday Four Square! 4:15PM, Outside Gates

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Figure 1: A complete binary tree.

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

CSI33 Data Structures

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

LINKED LISTS AND MIDTERM REVIEW

Data Structures and Algorithms

Algorithms. Deleting from Red-Black Trees B-Trees

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Transcription:

SEQUENCES AND TREES 4 COMPUTER SCIENCE 61A February 19, 2015 1 List Comprehension A list comprehension is a compact way to create a list whose elements are the results of applying a fixed expression to elements in another sequence. [<map exp> for <name> in <iter exp> if <filter exp>] Let s break down an example: [x * x - 3 for x in [1, 2, 3, 4, 5] if x % 2 == 1] In this list comprehension, we are creating a new list after performing a series of operations to our initial sequence [1, 2, 3, 4, 5]. We only keep the elements that satisfy the filter expression x % 2 == 1 (1, 3, and 5). For each retained element, we apply the map expression x*x - 3 before adding it to the new list that we are creating, resulting in the output [-2, 6, 22]. Note: The if clause in a list comprehension is optional. 1.1 Questions 1. What would Python print? >>> [i + 1 for i in [1, 2, 3, 4, 5] if i % 2 == 0] >>> [i * i for i in [5, -1, 3, -1, 3] if i > 2] >>> [[y * 2 for y in [x, x + 1]] for x in [1, 2, 3, 4]] 1

DISCUSSION 4: SEQUENCES AND TREES Page 2 2. Define a function foo that takes in a list lst and returns a new list that keeps only the even-indexed elements of lst and multiples each of those elements by the corresponding index. def foo(lst): >>> x = [1, 2, 3, 4, 5, 6] >>> foo(x) [0, 6, 20] return [ ] 2 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 4 0 8 6 16 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. 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 of the tree. In our example, the node that contains 7 is the root. Leaf: A node that has no children. In our example, the nodes that contain 4, 0, 6, 17, and 20 are leaves. Subtree: Notice that each child of a parent is itself the root of a smaller tree. In our 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.

DISCUSSION 4: SEQUENCES AND TREES Page 3 Depth: How far away a node is from the root. In other words, the length of the path from the root to the node. In the diagram, the node containing 19 has depth 1; the node containing 3 has depth 2. We define the depth of the root of a tree is 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 4. Thus, the entire tree has height 4. 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. 2.1 Implementation A tree has both a root value and a sequence of branches. In our implementation, we represent the branches as lists of subtrees. The arguments to the constructor, tree, as a value for the root and a list of branches. The selectors are root and branches. # Constructor def tree(value, branches=[]): for branch in branches: assert is_tree(branch), branches must be trees return [value] + list(branches) # Selectors def root(tree): return tree[0] def branches(tree): return tree[1:] We have also provided two convenience functions, is leaf and is tree: def is_leaf(tree): return not branches(tree) def is_tree(tree): if type(tree)!= list or len(tree) < 1: return False for branch in branches(tree): if not is_tree(branch): return False return True

DISCUSSION 4: SEQUENCES AND TREES Page 4 It s simple to construct a tree. Let s try to create the following tree: 1 3 2 t = tree(1, [tree(3, [tree(4), tree(5), tree(6)]), tree(2)]) 4 5 6 The use of whitespace can help with legibility, but it is not required. 2.2 Questions 1. Define a function square tree(t) that squares every item in the tree t. It should return a new tree. You can assume that every item is a number. def square_tree(t): Return a tree with the square of every element in t 2. Define a function height(t) that returns the height of a tree. Recall that the height of a tree is the length of the longest path from the root to a leaf. def height(t): Return the height of a tree

DISCUSSION 4: SEQUENCES AND TREES Page 5 3. Define a function tree size(t) that returns the number of nodes in a tree. def tree_size(t): Return the size of a tree. 4. Define a function tree max(t) that returns the largest number in a tree. def tree_max(t): Return the max of a tree.

DISCUSSION 4: SEQUENCES AND TREES Page 6 2.3 Extra Questions 1. An expression tree is a tree that contains a function for each non-leaf root, which can be either add or mul. All leaves are numbers. Implement eval tree, which evaluates an expression tree to its value. You may find the functions reduce and apply to all, introduced during lecture, useful. def reduce(fn, s, init): reduced = init for x in s: reduced = fn(reduced, x) return reduced def apply_to_all(fn, s): return [fn(x) for x in s] from operator import add, mul def eval_tree(tree): Evaluates an expression tree with functions as root >>> eval_tree(tree(1)) 1 >>> expr = tree(mul, [tree(2), tree(3)]) >>> eval_tree(expr) 6 >>> eval_tree(tree(add, [expr, tree(4)])) 10

DISCUSSION 4: SEQUENCES AND TREES Page 7 2. We can represent the hailstone sequence as a tree in the figure below, showing the route different numbers take to reach 1. Remember that a hailstone sequence starts with a number n, continuing to n/2 if n is even or 3n + 1 if n is odd, ending with 1. Write a function hailstone tree(n, h) which generates a tree of height h, containing hailstone numbers that will reach n. 1 2 4 8 16 32 64 5 10 128 21 20 3 def hailstone_tree(n, h): Generates a tree of hailstone numbers that will reach N, with height H. >>> hailstone_tree(1, 0) [1] >>> hailstone_tree(1, 4) [1, [2, [4, [8, [16]]]]] >>> hailstone_tree(8, 3) [8, [16, [32, [64]], [5, [10]]]]

DISCUSSION 4: SEQUENCES AND TREES Page 8 3. Define the procedure find path(tree, x) that, given a rooted tree tree and a value x, returns a list containing the nodes along the path required to get from the root of tree to a node x. If x is not present in tree, return None. Assume that the elements in tree are unique. For the following tree, find path(t, 5) should return [2, 7, 6, 5] 2 7 15 3 6 5 11 def find_path(tree, x): Return a path in a tree to a leaf with value X, None if such a leaf is not present. >>> t = tree(2, [tree(7, [tree(3), tree(6, [tree(5), tree (11)])]), tree(15)]) >>> find_path(t, 5) [2, 7, 6, 5] >>> find_path(t, 6) [2, 7, 6] >>> find_path(t, 10)