Week nine-ten: Trees Trees.

Slides:



Advertisements
Similar presentations
Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks
Advertisements

Trees1 More on Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.
Trees Briana B. Morrison Adapted from Alan Eugenio.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
© 2004 Goodrich, Tamassia Trees1 Chapter 7 Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
Trees1 Recursion Recursion is a concept of defining a method that makes a call to itself.
Chapter 7: Trees Objectives:
© 2004 Goodrich, Tamassia Trees1 Lecture 04 Trees Topics Trees Binary Trees Binary Search trees.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
© 2004 Goodrich, Tamassia Trees1 this is a tree. © 2004 Goodrich, Tamassia Trees2 What is a Tree? In computer science, a tree is an abstract model of.
Trees COMP53 Oct 31, What is a Tree? A tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation.
Trees1 Part-C Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees.
Make Money Fast! Stock Fraud Apply shortcuts Bank Robbery Just For Laughs Trees This one is cool eITnotes.com For more notes and topics visit:
Trees CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Chapter 7:Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures.
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
Trees. What is a Tree? In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent- child relation.
Trees 4/23/2017 Trees.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Data Structures TREES.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2013 Goodrich, Tamassia, Goldwasser.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CH 7. TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.
1 COMP9024: Data Structures and Algorithms Week Five: Trees Hui Wu Session 1, 2015
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees 5/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Trees 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Searching (and into Trees)
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees
University of Palestine
Trees 8/7/2018 2:27 PM Binary Trees © 2010 Goodrich, Tamassia Trees.
Trees 二○一八年八月二十六日 Part-C Trees Trees.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Trees Chuan-Ming Liu Computer Science & Information Engineering
© 2013 Goodrich, Tamassia, Goldwasser
Binary Trees, Binary Search Trees
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
TREES General trees Binary trees Binary search trees AVL trees
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Chapter 7. Trees & Binary Trees
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees
Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb CLRS, Section 10.4.
Trees Palestine Gaza West Bank 48 Stolen Land Trees Trees
Copyright © Aiman Hanna All rights reserved
CE 221 Data Structures and Algorithms
Trees.
Lecture 9 CS2013.
CS210- Lecture 9 June 20, 2005 Announcements
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

Week nine-ten: Trees Trees

Outline (Chapter 8) General Trees Binary Trees Tree Traversal Pre-order Traversal In-order Traversal Post-order Traversal Euler Tour

General Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery

Motivation (Why Trees) Trees are one of the most important nonlinear data structures Trees also provide a natural organization for data Applications: File systems GUI Databases Websites Family Trees

Tree Definitions and Properties A tree is an abstract data type that stores elements hierarchically. each element in a tree has a parent element and zero or more children elements. A tree with 17 nodes representing the organization of a fictitious corporation.

Tree Definitions and Properties The root of T has no parent. Two nodes that are children of the same parent are siblings. A node v is external or leaves if v has no children. A node v is internal if it has one or more children. Ancestors of a node: parent, grandparent, grand-grandparent, etc. Descendant of a node: child, grandchild, grand-grandchild, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node Tree representing a portion of a file system.

Tree Definitions and Properties Subtree is tree consisting of a node and its descendants An edge of tree T is a pair of nodes (u,v) such that u is the parent of v, or vice versa. A path of T is a sequence of nodes such that any two consecutive nodes in the sequence form an edge. For example, the path (cs252/, projects/, demos/, market). Tree representing a portion of a file system.

The Tree Abstract Data Type We use positions to abstract nodes A position object for a tree supports the method: getElement(): Returns the element stored at this position. Accessor methods: root(): Returns the position of the root of the tree (or null if empty). parent(p): Returns the position of the parent of position p (or null if p is the root). children(p): Returns an iterable collection containing the children of position p (if any). numChildren(p): Returns the number of children of position p.

The Tree Abstract Data Type Generic methods: size(): Returns the number of positions (and hence elements) that are contained in the tree. isEmpty(): Returns true if the tree does not contain any positions (and thus no elements). iterator(): Returns an iterator for all elements in the tree (so that the tree itself is Iterable). positions(): Returns an iterable collection of all positions of the tree. Query methods: isInternal(p): Returns true if position p has at least one child. isExternal(p): Returns true if position p does not have any children. isRoot(p): Returns true if position p is the root of the tree.

A Tree Interface in Java 9-Dec-18 Computer Science Department

Binary Trees A binary tree is an ordered tree with the following properties: Every node has at most two children. Each child node is labeled as being either a left child or a right child. A left child precedes a right child in the order of children of a node. The subtree rooted at a left or right child of an internal node v is called a left subtree or right subtree, respectively, of v. A binary tree is proper or full if each node has either zero or two children. A binary tree that is not proper is improper. Applications: (arithmetic expressions, decision processes, searching) A B C D E F G H I

Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions A decision tree providing investment advice.

Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands

The Binary Tree Abstract Data Type A binary tree is a specialization of a tree

Properties of Proper Binary Trees Notation n number of nodes e number of external nodes i number of internal nodes h height Properties: e = i + 1 n = 2e - 1 h  i h  (n - 1)/2 e  2h h  log2 e h  log2 (n + 1) - 1

Implementing Trees Linked Structure for General Trees Linked Structure for Binary Trees Array-Based Representation of a Binary Tree 9-Dec-18 Computer Science Department

Linked Structure for Trees A node is represented by an object storing (Element, Parent node, Sequence of children nodes). Each node stores a single container of references to its children

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

Array-Based Representation of Binary Trees For every position p of T , let f ( p) be the integer defined as follows. • If p is the root of T, then f(p)=0. • If p is the left child of position q, then f(p) = 2f(q)+1. • If p is the right child of position q, then f(p) = 2f(q)+2.

Tree Traversal Algorithms A traversal of a tree T is a systematic way of accessing, or “visiting,” all the positions of T . The specific action associated with the “visit” of a position p incrementing a counter performing some complex computation for p. 9-Dec-18 Computer Science Department

Preorder Traversals of General Trees In a preorder traversal of a tree T , the root of T is visited first and then the sub- trees rooted at its children are traversed recursively. 9-Dec-18 Computer Science Department

Preorder Traversals of General Trees 9-Dec-18 Computer Science Department

Post-order Traversals of General Trees In a post-order traversal of a tree T , the algorithm recursively traverses the subtrees rooted at the children of the root first, and then visits the root 9-Dec-18 Computer Science Department

Post-order Traversals of General Trees 9-Dec-18 Computer Science Department

In-order Traversal of a Binary Tree During an in-order traversal, we visit a position between the recursive traversals of its left and right subtrees. 9-Dec-18 Computer Science Department

In-order Traversal of a Binary Tree 9-Dec-18 Computer Science Department

In-order Traversal Applications Print Arithmetic Expressions: the in-order traversal visits positions in a consistent order with the standard representation of the expression, 3 + 1 × 3/9 − 5 + 2 . . . (without parentheses). 9-Dec-18 Computer Science Department

In-order Traversal Applications Binary search tree: Let S be a set whose unique elements have an order relation. For example, S could be a set of integers. A binary search tree for S is a proper binary tree T such that, for each internal position p of T : • Position p stores an element of S, denoted as e(p). • Elements stored in the left subtree of p (if any) are less than e(p). • Elements stored in the right subtree of p (if any) are greater than e(p). 9-Dec-18 Computer Science Department

In-order Traversal Applications Tree Drawing: is the problem of computing a graphical layout of a binary tree x-coordinates increase left to right y-coordinates increase top to bottom the upper left is the corner of the drawing. 9-Dec-18 Computer Science Department

Euler Tour Traversal The Euler tour traversal of a tree T can be informally defined as a “walk” around T , where we start by going from the root toward its leftmost child, viewing the edges of T as being “walls” that we always keep to our left. There are two notable “visits” to each position p: A “pre visit” occurs when first reaching the position, that is, when the walk passes immediately left of the node in our visualization. A“postvisit”occurswhenthewalklaterproceedsupwardfromthatposition, that is, when the walk passes to the right of the node in our visualization.

Euler Tour Traversal (Binary Tree) For the case of a binary tree, we can customize the algorithm to include an explicit “in visit” action a binary Euler tour can produce a traditional parenthesized arithmetic expression, such as "((((3+1)x3)/((9-5)+2))-((3x(7-4))+6))" “Pre visit” action: if the position is internal, print “(”. “In visit” action: print the value or operator stored at the position. “Post visit” action: if the position is internal, print “)”. 9-Dec-18 Computer Science Department

Euler Tour Traversal (Binary Tree) 9-Dec-18 Computer Science Department

References Chapter 8, Data Structures and Algorithms by Goodrich and Tamassia.

Exercises Answer the following questions based on the given tree: 9-Dec-18 Computer Science Department

Exercises Which node is the root? What are the internal nodes? How many descendants does node cs016/ have? How many ancestors does node cs016/ have? What are the siblings of node homeworks/? Which nodes are in the subtree rooted at node projects/? What is the depth of node papers/? What is the height of the tree? 9-Dec-18 Computer Science Department

Draw the binary tree representation of the following arithmetic expression: “(((5+2)∗(2−1))/((2+9)+((7−2)−1))∗8)” 9-Dec-18 Computer Science Department