CSC148 Week 6. Larry Zhang

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

Binary Trees

Trees. Tree Structure Binary Tree Tree Traversals

TREES. Trees - Introduction

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

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

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

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

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

Programming II (CS300)

Binary Trees, Binary Search Trees

CE 221 Data Structures and Algorithms

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

Trees. CSE 373 Data Structures

Why Do We Need Trees?

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

March 20/2003 Jayakanth Srinivasan,

Trees. Trees. CSE 2011 Winter 2007

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

Chapter 20: Binary Trees

COSC 2011 Section N. Trees: Terminology and Basic Properties

CSC148-Section:L0301

Cpt S 122 Data Structures. Data Structures Trees

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.

CS 206 Introduction to Computer Science II

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

Trees. Truong Tuan Anh CSE-HCMUT

Data Structure - Binary Tree 1 -

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

INF2220: algorithms and data structures Series 1

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Introduction to Computers and Programming. Concept Question

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

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

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

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

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

Garbage Collection: recycling unused memory

Algorithms and Data Structures

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

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

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

Trees and Binary Trees

TREES Lecture 10 CS2110 Spring2014

CS302 - Data Structures using C++

Principles of Computer Science

Binary Trees Fall 2018 Margaret Reid-Miller

Data Structures and Algorithms

CSE 214 Computer Science II Introduction to Tree

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

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

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

INF2220: algorithms and data structures Series 1

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

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

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

CSC148-Section:L0301

Topic 14. The BinaryTree ADT

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

CS350: Data Structures Tree Traversal

Algorithms. Deleting from Red-Black Trees B-Trees

Data Structures Lecture 6

CSI33 Data Structures

Trees. Eric McCreath

Binary Trees. Height 1

Binary Search Trees. See Section 11.1 of the text.

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

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

Trees. 9/21/2007 8:11 AM Trees 1

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

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

Data Structures. Trees, Binary trees & Binary Search Trees

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

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

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

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

TREES Lecture 12 CS2110 Fall 2016

Objectives. In this session, you will learn to:

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

Data Structures and Algorithms for Engineers

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

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

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

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

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

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

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

CS24 Week 8 Lecture 1

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

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

Data Structures - Binary Trees 1

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

COMP : Trees. COMP20012 Trees 219

DATA STRUCTURES USING C

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

Transcription:

CSC148 Week 6 Larry Zhang 1

Announcements Test 1 coverage: trees (topic of today and Wednesday) are not covered Assignment 1 slides posted on the course website. 2

Data Structures 3

Data Structures A data structure is a way of organizing data. When data are organized in a good data structure, operations that read or write the data can be much more efficient. Bad programmers worry about the code. Good programmers worry about data structures and their relationships. -- Linus Torvalds 4

A simple data structure we already know... Lists Lists are linear structures They are linear in the sense that data is ordered (i.e. first piece of data is followed by second is followed by third...) The list data structure naturally makes sense for many applications: Function calls in programs (stack) A lineup in a bank (queue) etc. 5

Other structures The linear structure doesn t naturally make sense many other applications. Consider the directory structure in a file system. They have a natural hierarchical structure that is difficult to represent linearly. Representing this hierarchical structure using a list is not convenient. 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. But how would we know when the files of a subdirectory end and we are back up one level? Other examples of this hierarchical structure A Python program A University / company 6

Tree: a hierarchical data structure Definition: A tree has a set of nodes (often with values or labels), and directed edges that connect nodes. One specified node is the root. Every node besides the root has exactly one parent. How many nodes and edges are there in this tree? 7

Tree terminology Parent: a node is the parent of all nodes to which it has outgoing edges. Siblings: set of nodes that share a common parent. Leaf: node that has no children (no outgoing edges). Internal node: non-leaf node. Path: sequence of nodes n1,n2,...,nk, where there is an edge from n1 to n2, n2 to n3, etc. Descendant: node n is a descendant of some other node p if there is a path from p to n. 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. 8

Tree terminology cont. Branching factor: maximum number of children of any node. Length of a path: number of edges on the path Level (Depth): the level (or depth) of node n is the length of the path from the root node to n. The level of the root is 0. Tree height: maximum of all node levels. 9

Another tree example What is the height of the tree? Branching factor? Depth of Baseball? Length of path from Sports to AL? 10

Operations on Trees Traverse: visit the nodes in some order and apply some operation to each node Insert a new node Remove a node Attach a subtree at a node Remove a subtree 11

Binary Trees a special kind of trees 12

Binary Tree A binary tree is a tree where each node has at most two children not a binary tree binary tree 13

Represent a binary tree How do we represent a binary tree in a Python program? We will discuss the following two ways: List of lists (nested list) Nodes and references 14

Binary Tree Representation 1: List of lists At the top level, it s a list of three elements [root, left-subtree-of-root, right-subtree-of-root] root is the label of the root node left-subtree-of-root could be either the following: another list of lists, representing the left subtree (which is a binary tree) None: if root has no left child, i.e., the left subtree doesn t exist right-subtree-of-root is symmetrically the same. 15

Convert this tree to the list-of-lists representation [ A, left-subtree, right-subtree] [ A, left-subtree, [ C, None, None]] [ A, [ B, left-sub, None], [ C, None, None]] [ A, [ B, [ D, None, r-sub], None], [ C, None, None] ] [ A, [ B, [ D, None, [E, None, None]], None], [ C, None, None] ] 16

Convert this list to a tree [5, [4, None, None], [3, [2, None, None], [1, None, None]]] 5 4 3 2 1 17

Operations on a binary tree binary_tree(value): returns a binary tree with a single node labelled by value insert_left(bt, value): insert value as the left child node of the root of bt. insert_right(bt, value): insert value as the right child node of the root of bt. 5 5 4 3 2 1 insert_left(bt, 7) 4 7 3 2 1 18

Operations on a binary tree contains(bt, value): return True iff the binary tree bt contains value DEMO: list_of_lists.py 19

Binary Tree Representation 2: Nodes and References 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. Our tree objects will have attributes for the root value, left child and right child. 20

Binary Tree Representation 2: Nodes and References How does insert_left work? bt.insert_left(7) create a single-node binary tree t with 7, t = BinaryTree(7) the current left branch will be t s left branch, t.left = self.left the root will points to 7 (t s root), self.left = t 21

DEMO nodes_and_refs.py 22

Tree Traversals 23

Tree Traversals Traversal: visiting each element of a structure. We say we have visited an element when we have done something with it (e.g. print, change, etc.) What are the obvious traversals of a list? left to right, right to left What about traversals for a binary tree? we have more ways systematically visit each node in a binary tree e.g., preorder, inorder, postorder 24

Tree Traversals Preorder: Visit the root node, do a preorder traversal of the left subtree, and do a preorder traversal of the right subtree. Inorder: Do an inorder traversal of the left subtree, visit the root node, and then do an inorder traversal of the right subtree. Postorder: do a postorder traversal of the left subtree, do a postorder traversal of the right subtree, and visit the root node Note: these procedures are recursive defined. 25

Example: what s the preorder traversal of this tree? Preorder: root, left, right A, tree-b, C A, B, tree-d, C A, B, D, E, C 26

Example: what s the inorder traversal of this tree? inorder: left, root, right tree-b, A, C tree-d, B, A, C D, E, B, A, C 27

Example: what s the postorder traversal? postorder: left, right, root tree-b, C, A tree-d, B, C, A E, D, B, C, A 28

DEMO implementing traverals in list_of_lists.py nodes_and_refs.py Homework: finish implementing all traversals 29

Another (mystery) traversal order We re going to look at code that uses a queue to do a tree traversal. It works on the list of lists representation of trees. Two questions: What does it do on some sample tree? What does it do in general? 30

Mystery Traversal Code 31

Home exercise: Expression Trees Given the binary tree, write a function that returns the string of the fully parenthesized math expression. Which traversal order to use? Given the binary tree, write a function that evaluates the represented math expression. Which traversal order to use now? 32