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

Similar documents
CSC148 Week 6. Larry Zhang

Trees. Tree Structure Binary Tree Tree Traversals

CSI33 Data Structures

Binary Trees

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

Programming II (CS300)

TREES. Trees - Introduction

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

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

March 20/2003 Jayakanth Srinivasan,

Chapter 20: Binary Trees

Binary Trees, Binary Search Trees

Algorithms and Data Structures

CE 221 Data Structures and Algorithms

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

Trees. CSE 373 Data Structures

CS24 Week 8 Lecture 1

COSC 2011 Section N. Trees: Terminology and Basic Properties

CS350: Data Structures Tree Traversal

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

Why Do We Need Trees?

Trees. Eric McCreath

Trees. Truong Tuan Anh CSE-HCMUT

CSI33 Data Structures

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

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

CSC148-Section:L0301

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

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

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

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

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Garbage Collection: recycling unused memory

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Trees. Trees. CSE 2011 Winter 2007

Scientific Programming. Trees

Principles of Computer Science

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

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

Binary Trees. Height 1

Binary Trees Fall 2018 Margaret Reid-Miller

CSE 214 Computer Science II Introduction to Tree

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2)

INF2220: algorithms and data structures Series 1

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Introduction to Computers and Programming. Concept Question

Data Structures and Algorithms

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

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

COMP 250 Fall recurrences 2 Oct. 13, 2017

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Data Structures and Algorithms for Engineers

ITI Introduction to Computing II

CS302 - Data Structures using C++

Data Structure - Binary Tree 1 -

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

CS 206 Introduction to Computer Science II

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Recursion. Presentation Subtitle. Brad Miller David Ranum 1 12/19/2005. Luther College

Cpt S 122 Data Structures. Data Structures Trees

TREES Lecture 10 CS2110 Spring2014

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

LECTURE 11 TREE TRAVERSALS

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

ITI Introduction to Computing II

Trees Algorhyme by Radia Perlman

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

Recursive Data Structures and Grammars

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Spring 2018 Profs Bill & Jon

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Lecture 26. Introduction to Trees. Trees

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

BBM 201 Data structures

tree nonlinear Examples

Objectives. In this session, you will learn to:

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

Name CPTR246 Spring '17 (100 total points) Exam 3

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Advanced Tree Data Structures

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Trees and Binary Trees

Data Structures Lecture 6

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Trees. Make Money Fast! Stock Fraud. Bank Robbery. Ponzi Scheme. Trees Goodrich, Tamassia

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

Trees. Data structures and Algorithms. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme

Topic 14. The BinaryTree ADT

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Transcription:

CSC148H Week 8 Sadia Sharmin July 12, 2017 1/29

Motivating Trees I A data structure is a way of organizing data I Stacks, queues, and lists are all linear structures I They are linear in the sense that data is ordered (i.e. first piece of data is followed by second is followed by third...) I This makes sense for many applications: I Function calls in programs (stack) I Alineupinabank(queue) I Event-handling based on timestamps (priority queue) 2/28

Motivating Trees... I It doesn t make sense to organize certain types of data into a linear structure I Consider directories in a file system. They have a natural hierarchical structure that is di cult to represent linearly I If we want to use a list, we might try storing the root directory at the first position and its subdirectories and files to its right I But how would we know when the files of a subdirectory end and we are back up one level? I Other examples: I I Structure of an HTML document Structure of a Python program 3/28

Tree Definition I A tree has one node called the root. This is at the very top of the tree structure. I The root can have a set of nodes that are connected to it (each of these nodes is a child of the root node). I Each of these nodes can have their own children. I Nodes at the bottom of the tree structure have no children. These nodes are called leaves. I These nodes often also have a value or label. 4/28

Tree Definition I A tree has a set of nodes (often with values or labels), and directed edges that connect nodes I One node is the root I Every node besides the root has exactly one parent 5/28

Sample Tree Courses CSC148 CSC209 Lectures Assignments Tests I How many nodes are there? What are they? I How many edges are there? What are they? I What is the root? 6/28

Tree Terminology I Parent: a node is the parent of all nodes to which it has outgoing edges I Siblings: set of nodes that share a common parent I Leaf: node that has no children (i.e. no outgoing edges) I Internal node: nonleaf node I Path: sequence of nodes n 1, n 2,...,n k, where there is an edge from n 1 to n 2, n 2 to n 3,etc. I Descendant: node n is a descendant of some other node p if there is a path from p to n I Subtree: a subtree of tree T is a tree whose root node r is a node in T, and which consists of all the descendants of r and the edges among them 7/28

Tree Terminology... I Branching Factor: maximum number of children of any node I Level (Depth): the level (or depth) of node n is the number of edges on the path from the root node to n. The level of the root is 0 I Length of a path: number of edges on a path I Tree height: maximum of all node levels 8/28

Another Sample Tree Sports Baseball Basketball AL NL East Central West East Central West Bluejays What is the height of the tree? Branching factor? Depth of Baseball? Length of path from Sports to AL? 9/28

Common Operations on Trees I Traverse a tree: visit the nodes in some order and apply some operation to each node I Insert a new node I Remove a node I Attach a subtree at a node I Remove a subtree We will focus on binary trees: trees where each node has at most two children. 10 / 28

Representing Binary Trees We will represent binary trees in our programs in two ways: I List of lists, or I Nodes and references 11 / 28

Representation 1: List of Lists I First element of the list contains the label of the root node I Second element is the list that represents the left subtree, or None I Third element is the list that represents the right subtree, or None 12 / 28

Convert to List of Lists A B C D E 13 / 28

Convert to List of Lists A B C D E [ A, [ B, [ D, None, [ E, None, None]], None], [ C, None, None]] 13 / 28

Converting to a Tree What is the tree represented by this list? [5, [4, None, None], [3, [2, None, None], [1, None, None]]] 14 / 28

Converting to a Tree What is the tree represented by this list? [5, [4, None, None], [3, [2, None, None], [1, None, None]]] 5 4 3 2 1 14 / 28

Implementation of List of Lists # A binary tree (BT) is None or a list of three elements def binary_tree(value): (value) -> BT Create BT with value as root and no children. return [value, None, None] def insert_left(bt, value): (BT, value) -> NoneType Insert value as the left node of the root of bt. if not bt: raise ValueError( cannot insert into empty tree ) left_branch = bt.pop(1) if not left_branch: bt.insert(1, [value, None, None]) else: bt.insert(1, [value, left_branch, None]) 15 / 28

List of Lists: Methods Let s write some new methods. I insert_right: add a value as the right child I preorder: return a list of node values in preorder I inorder: return a list of node values in inorder I postorder: return a list of node values in postorder I contains: return True i the binary tree contains a given value 16 / 28

Representation 2: Nodes and References I Since the left and right children of a node are each roots of (sub)trees, we can model a tree as a recursive data structure I Our tree objects will have attributes for the root value, left child and right child class BinaryTree: def init (self, value): self.key = value self.left = None self.right = None def insert_left(self, value): if not self.left: self.left = BinaryTree(value) else: t = BinaryTree(value) t.left = self.left self.left = t 17 / 28

Example of Node Insertion A A C B B (a) Original Tree (b) Original Tree after inserting node C Figure: Inserting a Node Using Code on Previous Slide 18 / 28

Tree Traversals I Traversal: accessing each element of a structure I We say we have visited an element when we have done something with it (e.g. print, change, etc.) I Lists have two obvious traversals: left to right, and right to left I What are some ways we can systematically visit each node in atree? 18 / 31

Visit each level one-by-one I We can start at the root I Then look at all of the root s immediate children I Then look at all the children of the first child, and then the second child, then third, and so on I And repeat this through the whole tree I This algorithm is called breadth-first search 19 / 31

Depth-first search I We can start at the root I Then look at one of the root s immediate children I Then look at one of this child s children I Then one of that child s children and so on, until we reach a leaf. Then we go back to the last parent and repeat for the next child. I Basically, we go as deep as we can into the tree on one child before moving on to the next sibling I This search algorithm is called depth-first search 20 / 31

DFS Tree Traversals... I Preorder: Visit the root node, do a preorder traversal of the left subtree, and do a preorder traversal of the right subtree I Inorder: Do an inorder traversal of the left subtree, visit the root node, and then do an inorder traversal of the right subtree I Postorder: do a postorder traversal of the left subtree, do a postorder traversal of the right subtree, and visit the root node 21 / 31

Example: Tree Traversals A B C D E I Preorder: 21 / 28

Example: Tree Traversals A B C D E I Preorder: A, B, D, E, C I Inorder: 21 / 28

Example: Tree Traversals A B C D E I Preorder: A, B, D, E, C I Inorder: D, E, B, A, C I Postorder: 21 / 28

Example: Tree Traversals A B C D E I Preorder: A, B, D, E, C I Inorder: D, E, B, A, C I Postorder: E, D, B, C, A 21 / 28

Traversal Code Recursion helps us write concise traversal code directly from its definition: from nr import BinaryTree def preorder(t): (BinaryTree) -> list if not t: return [] return [t.key] + preorder(t.left) + preorder(t.right) 22 / 28

Expression Trees + * A * + C B C A B (a) Tree for A +(B C) (b) Tree for (A + B) C Figure: Two expression trees forcing di erent orders of operation 23 / 28

Uses of Expression Trees A slightly modified inorder traversal gives us a fully parenthesized expression corresponding to the tree s order of operations. from nr import BinaryTree def expr(tree): if not tree: return s = ( + expr(tree.left) s = s + str(tree.key) s = s + expr(tree.right)+ ) return s 24 / 28

Uses of Expression Trees... I A postorder evaluation gives us a way to evaluate the expression in an expression tree I Postorder will recursively calculate the value for the left subtree, then the value for the right subtree I The parent of these two subtrees will be an operator that we can apply to the above results to yield the result for the entire tree I... an exercise for you! 25 / 28

Mystery Traversal Code I We re going to look at code that uses a queue to do a tree traversal I It works on the list of lists representation of trees that we ve been using I Two questions I What does it do on a sample tree? I What does it do in general? 26 / 28

Mystery Traversal Code... from my_queue import Queue def some_order(t): if t: q = Queue() q.enqueue(t) while not q.is_empty(): t = q.dequeue() print(t[0]) if t[1]: q.enqueue(t[1]) if t[2]: q.enqueue(t[2]) treelist = [ a, [ b, [ c, [ d, None, None], None], None], [ e, None, [ f, None, [ g, [ h, None, None], None]]]] 27 / 28

That Sample Tree [ a, [ b, [ c, [ d, None, None], None], None], [ e, None, [ f, None, [ g, [ h, None, None], None]]]] a b e c f d g h 28 / 28