Data Structures Lecture 6

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

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

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

Data Structures and Algorithms. Trees

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

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

Trees 3/19/14. Mammal. Dog Pig Cat

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

Outline. Definitions Traversing trees Binary trees

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 02/06/2006 Trees 1

Trees and Binary Trees

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 1/18/2005 4:17 AM Trees 1

Data Structures. Trees, Binary trees & Binary Search Trees

Trees. Trees. CSE 2011 Winter 2007

Stock Fraud. Nancy Amato. Parasol Lab, Dept. CSE, Texas A&M University

What is a Tree. Trees. Tree ADT ( 6.1.2) Tree Terminology. subtree. In computer science, a tree is an abstract model

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

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

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

Ch02 Data Structures

Binary trees. Binary trees. Binary trees

A Hierarchical Structure. Lecture11: Trees I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

Ch02 Data Structures

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

COSC 2011 Section N. Trees: Terminology and Basic Properties

DataStruct 7. Trees. Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011.

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

A Hierarchical Structure. Lecture11: Tree I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

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

CSC148 Week 6. Larry Zhang

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

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

COMP26120: Algorithms and Imperative Programming. Lecture 1 Trees

Topics on the Midterm

Binary Trees

Programming II (CS300)

Trees. CSE 373 Data Structures

March 20/2003 Jayakanth Srinivasan,

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

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

Tree Traversal. Lecture12: Tree II. Preorder Traversal

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

Binary Trees, Binary Search Trees

CE 221 Data Structures and Algorithms

csci 210: Data Structures Trees

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

Chapter 20: Binary Trees

TREES. Trees - Introduction

Topic 14. The BinaryTree ADT

Why Do We Need Trees?

Trees Algorhyme by Radia Perlman

tree nonlinear Examples

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

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

BBM 201 Data structures

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

DATA STRUCTURES USING C

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

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

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

CS302 - Data Structures using C++

INF2220: algorithms and data structures Series 1

TREES Lecture 12 CS2110 Fall 2016

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

CS 206 Introduction to Computer Science II

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Recursive Data Structures and Grammars

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

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

Introduction to Computers and Programming. Concept Question

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

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

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

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

CSI33 Data Structures

Data Structures Lecture 5

Algorithms and Data Structures

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

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

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. Tree Structure Binary Tree Tree Traversals

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

Data Structure - Binary Tree 1 -

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

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

Binary Trees Fall 2018 Margaret Reid-Miller

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

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

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

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

CSE 214 Computer Science II Introduction to Tree

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

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

CSI33 Data Structures

Data Structures and Algorithms for Engineers

TREES Lecture 10 CS2110 Spring2014

Lecture 26. Introduction to Trees. Trees

Transcription:

Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 6

Announcement Project Proposal due on Nov. 9 Remember to bring a hardcopy of your project proposal to the class next week Also submit your project proposal file to your TAs Discuss potential difficulties of your project We can select some topics and explain in the lab

Announcement We will have programming lectures and tests on Nov. 6 in 逸仙樓 5F. HWs Review BMI Generic Progression Keyword Counting The Ordered List HTML Tag Matching Web Site Score

Abstract Non-linear Data Structures Trees and their variations

Abstract Data Type (ADT) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations We have discussed Array ADT, List ADT, Stack ADT, and Queue ADT All of them are linear ADT

A Hierarchical Structure

Linux/Unix file systems

Tree: A Hierarchical ADT A tree (upside down) is an abstract model of a hierarchical structure Computers R Us A tree consists of nodes with a parent-child relation Sales Manufacturing R&D Each element (except the top element) has a parent and zero or more children elements US International Laptops Europe Asia Canada Desktops

Tree Terminology Root: a node without any parent (A) Internal node: a node with at least one child (A, B, C, F) A External node (a.k.a. leaf ): a node without children (E, I, J, K, G, H, D) B C D Subtree: tree consisting of a node and its descendants E F G H I J K subtree

Tree Terminology Ancestors of a node: parent, grandparent, grand-grandparent, etc. A Depth of a node: number of ancestors B C D Height of a tree: maximum depth of any node (3) Descendant of a node: child, grandchild, grand-grandchild, etc. E F G H I J K

Tree ADT We use positions to define the tree ADT The positions in a tree are its nodes and neighboring positions satisfy the parent-child relationships method root() parent(v) children(v) isroot(v) isexternal(v) isinternal(v) description Return the tree s root; error if tree is empty Return v s parent; error if v is a root Return v s children (an iterable collection of nodes) Test whether v is a root Test whether v is an external node Test whether v is an internal node

Tree ADT Generic methods (not necessarily related to a tree structure): method isempty() size() iterator() positions() replace(v,e) description Test whether the tree has any node or not Return the number of nodes in the tree Return an iterator of all the elements stored in the tree Return an iterable collection of all the nodes of the tree Replace with e and return the element stored at node v

A Linked Structure for Tree A node is represented by an object storing Element A parent node A sequence of children nodes (parent node) (a list of children) (element) (child node)

A Linked Structure for Tree Node objects implement the Position ADT B B A D F A D F C E C E

Tree Traversal Visit all nodes in a tree Do some operations during the visit Computers R Us Sales Manufacturing R&D US International Laptops Desktops Europe Asia Canada

Preorder Traversal A node is visited (so is the operation) before its descendants Application: Print a structured document Algorithm preorder(v) visit(v) for each child w of v preorder (w)

Preorder Traversal 1 Make Money Fast! 2 1. Motivations 2. Methods References 5 9 3 4 1.1 Greedy 1.2 Avidity 6 7 8 2.1 Stock Fraud 2.2 Ponzi Scheme 2.3 Bank Robbery For your project, you can print a structured web site with its sub links using preorder traversal

Postorder Traversal A node is visited after its descendants Application: Compute space used by files in a directory and its subdirectories Algorithm postorder(v) for each child w of v postorder (w) visit(v)

Postorder Traversal 9 cs16/ 3 homeworks/ 7 programs/ 8 todo.txt 1K 1 2 4 5 6 h1c.doc 3K h1nc.doc 2K DDR.java 10K Stocks.java 25K Robot.java 20K For your project, you can compute the score of a web site and its sub links Using postorder traversal

Binary Tree A binary tree is a tree with the following properties: A Each internal node has at most two children B C The children of a node are an ordered pair (left and right) We call the children of an internal node left child and right child D E F G H I

Binary Tree Alternative recursive definition: a binary tree is either A a tree consisting of a single node, or a tree whose root has an ordered pair of children, each of which is a binary tree B C D E F G H I

Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators + external nodes: operands Example: arithmetic expression tree for the expression: 2-3 b (2 (a - 1) + (3 b)) a 1

Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Want a fast meal? Yes No On a diet? On expense account? Yes No Yes No Subway Mc Donald s 王品台塑我家牛排

Proper Binary Trees Each internal node has exactly 2 children

Proper Binary Trees n :number of total nodes e :number of external nodes i :number of internal nodes h :height (maximum depth of a node) Properties: 1. e = i + 1 2. n = 2e - 1 3. h i 4. h (n - 1)/2 5. e 2 h 6. h log 2 e 7. h log 2 (n + 1) - 1

Properties 1. e = i+1 2. n = e+i = 2e-1 = 2i+1

Properties 3. h <= i 4. h<= (n-1)/2

Properties 5. e <= 2 h 6. h >= log 2 e 7. h >= log 2 ((n+1)/2) = log 2 (n+1) -1

BinaryTree ADT The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods: position left(p) position right(p) boolean hasleft(p) boolean hasright(p) Update methods may be defined by data structures implementing the BinaryTree ADT

Inorder Traversal A node is visited after its left subtree and before its right subtree Algorithm inorder(v) if hasleft (v) inorder (left (v)) visit(v) if hasright (v) inorder (right (v)) 1 2 3 4 6 5 8 7 9

Print Arithmetic Expressions Specialization of an inorder traversal print operand or operator when visiting node print ( before traversing left subtree print ) after traversing right subtree Algorithm printexpression(v) if hasleft (v) print( ( ) printexpression (left(v)) print(v.element ()) if hasright (v) printexpression (right(v)) print ( ) ) 1 2 4 2-3 6 + 5 a 1 ((2 (a - 1)) + (3 b)) 8 7 9 3 b

Evaluate Arithmetic Expressions Specialization of a postorder traversal recursive method returning the value of a subtree when visiting an internal node, combine the values of the subtrees Algorithm evalexpr(v) if isexternal (v) return v.element () else x evalexpr(leftchild (v)) y evalexpr(rightchild (v)) operator stored at v return x y 1 5 2 2 4-3 5 1 9 + 8 6 7 3 2

Euler Tour Traversal Generic traversal of a binary tree Walk around the tree and visit each node three times: + on the left (preorder) from below (inorder) on the right (postorder) 2 L B R - 3 2 5 1

A template method pattern A generic computation mechanism Specialized for an application by redefining the visit actions Algorithm eulartour(t,v) Perform the action for visiting node v on the left If v has a left child u in T then eulartour(t, u) Perform the action for visiting node v from below If v has a right child w in T then eulartour(t, w) Perform the action for visiting node v on the right

An application of EularTour printexpression On the left action: print ( From below action: print v On the right action: print ) Algorithm printexpression(t,v) if T.isInternal(v) then print ( If v has a left child u in T then printexpression(t, u) print(v) If v has a right child w in T then printexpression(t, w) if T.isInternal(v) then print )

A Linked Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node Right child node Parent node Element Left child Right child

A Linked Structure for Binary Trees B B A D C E A D C E

An Array-Based Representation Nodes are stored in an array A Node v is stored at A[rank(v)] rank(root) = 1 Left in even: if node is the left child of parent(node), rank(node) = 2 rank(parent(node)) Right in odd: if node is the right child of parent(node), rank(node) = 2 rank(parent(node)) + 1 A[0] is always empty A[i] is empty if there is no node in the ith position The array size N is 2 (h+1)

An Array-Based Representation 1 A 2 3 B D E F 4 5 C J 6 7 G H 10 11 A B D G H 0 1 2 3 10 11

HW 6 (Due on 11/16) Compute the score of a website! Construct a tree and its nodes according to a given website An element (referred by a node) represents one web page and has three fields: (name, url, score) Given a keyword and its weight, compute the score of each node Score = number of appearance * weight The score of a node = the score of the content of its url + the scores of its children This can be done by a postorder traversal of a tree Output the hierarchy of the website (with names and scores) using parentheses This can be done by an eular tour

An example input You will be given a website like: Soslab, http://soslab.nccu.edu.tw/welcome.html Publication, http://soslab.nccu.edu.tw/publications.html Projects, http://soslab.nccu.edu.tw/projects.html AppBeach, http://soslab.xyz:7777 Stranger, https://vlab.cs.ucsb.edu/stranger/ Member, http://soslab.nccu.edu.tw/members.html Course, http://soslab.nccu.edu.tw/courses.html

An example output Given a set of keywords, (Yu,1.2), (Fang, 1.8) you shall output something like ( Soslab, 56.6 (Publication, 18) (Projects, 15.6 (AppBeach, 2.6) (Stranger, 8.8) ) (Member, 9.2) (Course, 4.8) ) Fang Yu, 56.6 indicates that the sum of the score in the content of the given url (http://soslab.nccu.edu.tw) and its sub links

Coming Up We will have a programming test in the lab on Nov. 6 The project proposal is due on Nov. 9 We will talk about heap (some kind of a tree) on Nov. 9. Read Chapter 8