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.

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.
CS 171: Introduction to Computer Science II
Data Structures Lecture 6 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Trees Briana B. Morrison Adapted from Alan Eugenio.
1 Trees most slides taken from Mike Scott, UT Austin.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
© 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.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Chapter 7: Trees Objectives:
© 2004 Goodrich, Tamassia Trees1 Lecture 04 Trees Topics Trees Binary Trees Binary Search trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
© 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.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Trees Chapter 8.
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.
Lecture12: Tree II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
Trees 4/23/2017 Trees.
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.
1 Binary Trees (7.3) CSE 2011 Winter November 2015.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
Lecture11: Tree I Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
© 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)
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.
Trees Chuan-Ming Liu Computer Science & Information Engineering
© 2013 Goodrich, Tamassia, Goldwasser
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Week nine-ten: Trees 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
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.
Presentation transcript:

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 relation Applications: Organization charts Organization charts File systems File systems Programming environments Programming environments Computers”R”Us SalesR&DManufacturing LaptopsDesktops US International EuropeAsiaCanada

3 Outline Recursive Structure Definitions Tree Terminology Binary Tree Interface Example - Expression Trees Binary Tree Implementation Tree Traversals Tree Property-based methods

4 List Definition - Recursive A list is either empty or it contains a head element and a sublist where the sublist must be a list.

5 Tree Definition - Recursive A tree is either empty or it consists of an element called the root, and a (possibly empty) list of subtrees, where each subtree is a tree. The term node is used to refer to an element at a particular location in a tree.

6 A path is the unique shortest sequence of edges from a node to an ancestor. The length of a path is the number of edges in it. The height of a node = tree.height – length(path(root, node)). The height of a tree is the height of its root. The depth (level) of a node is the length of the path from the root to the node. The degree of a node is the number of children it has. The degree (arity) of a tree is the maximum degree of its nodes.

7 If node N2 is the root of a direct subtree of node N1, then node N2 is a child of node N1 and node N1 is the parent of node N2. The root node of a tree has no parent node in that tree. A node N1 is an ancestor of a node N2, if N1 is the parent of node N2 or if N1 is the ancestor of a parent of N2 (this is a recursive definition: ancestors(N) = parent(N) + ancestors(parent(N))). A node N2 is a descendent of a node N1, if N1 is an ancestor of node N2. A node with no children is called a leaf node. A node with children is called an interior node. Two nodes are siblings if they have the same parent.

8 Height(10)? Height(11)? Path (43,99)? Path (21,67)? Children(25)? Descendants(43)? Siblings(25)? Depth(99)? Depth(10)? Arity(tree)? Degree(99)? Degree(10)?

9 A binary tree is a tree in which nodes can have a maximum of 2 children. A binary tree is oriented if every node with 2 children differentiates between the children by calling them the left child and right child and every node with one child designates it either as a left child or right child. A node in a binary tree is full if it has arity 2. A full binary tree of height h has leaves only on level h and each of its interior nodes is full. A complete binary tree of height h is a full binary tree of height h with 0 or more of the rightmost leaves of level h removed.

10 Examples of Binary Trees Expert System Example

11 Binary Tree Traversals There are four common binary tree traversals: – Preorder: process root then left subtree then right subtree – Inorder: process left subtree then root then right subtree – Postorder: process left subtree then right subtree then root – Levelorder: process nodes of level i, before processing nodes of level i + 1

12  Linked Structure for Trees A node is represented by an object storing Element Element Parent node Parent node Sequence of children nodes Sequence of children nodes Node objects implement the Position ADT B D A CE F B  ADF  C  E

13 Array-Based Representation of Binary Trees nodes are stored in an array … let rank(node) be defined as follows: rank(root) = 1 if node is the left child of parent(node), rank(node) = 2*rank(parent(node)) if node is the right child of parent(node), rank(node) = 2*rank(parent(node)) A HG FE D C B J

14 Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators internal nodes: operators external nodes: operands external nodes: operands Example: arithmetic expression tree for the expression (2  ( a  1)  (3  b))    2 a1 3b

15 Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer internal nodes: questions with yes/no answer external nodes: decisions external nodes: decisions Example: dining decision Want a fast meal? How about coffee?On expense account? StarbucksSpike’sAl FornoCafé Paragon Yes No YesNoYesNo

16 Print Arithmetic Expressions Specialization of an inorder traversal print operand or operator when visiting node print operand or operator when visiting node print “(“ before traversing left subtree print “(“ before traversing left subtree print “)“ after traversing right subtree print “)“ after traversing right subtree Algorithm printExpression(v) if hasLeft (v) print( “(’’ ) inOrder (left(v)) print(v.element ()) if hasRight (v) inOrder (right(v)) print ( “)’’ )    2 a1 3b ((2  ( a  1))  (3  b))

17 Evaluate Arithmetic Expressions Specialization of a postorder traversal recursive method returning the value of a subtree recursive method returning the value of a subtree when visiting an internal node, combine the values of the subtrees 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   