Trees 8/7/2018 2:27 PM Binary Trees © 2010 Goodrich, Tamassia 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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Trees Briana B. Morrison Adapted from Alan Eugenio.
© 2004 Goodrich, Tamassia Trees1 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.
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.
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 4/23/2017 Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
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.
1 Binary Trees (7.3) CSE 2011 Winter November 2015.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
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.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CH 7. TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
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)
CS 201 Data Structures and Algorithms
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 二○一八年八月二十六日 Part-C Trees Trees.
Trees Another Abstract Data Type (ADT)
Trees Chuan-Ming Liu Computer Science & Information Engineering
CMSC 341 Introduction to Trees.
© 2013 Goodrich, Tamassia, Goldwasser
Binary Trees, Binary Search Trees
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
Introduction to Trees IT12112 Lecture 05.
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 Another Abstract Data Type (ADT)
Trees.
Week nine-ten: Trees Trees.
Trees Another Abstract Data Type (ADT)
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
Lecture 8: Iterators, Trees
Data Structures and Algorithm Analysis Trees
Copyright © Aiman Hanna All rights reserved
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Binary Tree Properties & Representation
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Trees 8/7/2018 2:27 PM Binary Trees © 2010 Goodrich, Tamassia Trees

Binary Tree (BT) A binary tree is a tree with the following properties: Each internal node has one or two children (exactly two for proper binary trees) The children of a node are an ordered pair We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either 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 Applications: arithmetic expressions decision processes searching A B C D E F G H © 2010 Goodrich, Tamassia Trees

Example: Arithmetic Expression Trees Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2  (a - 1) + (3  b)) Proper binary tree if no unary operator! +  - 2 a 1 3 b © 2010 Goodrich, Tamassia Trees

Example: Decision Trees Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Proper binary tree! Want a fast meal? Yes No How about coffee? On expense account? Yes No Yes No Starbucks Spike’s Al Forno Café Paragon © 2010 Goodrich, Tamassia Trees

© 2010 Goodrich, Tamassia Trees

Properties of Binary Trees Notation n: # of nodes ne: # of external nodes ni: # of internal nodes h: height Properties: 1  ne  2h h  ni  2h-1 h+1  n  2h+1-1 log2(n+1)-1  h  n-1 Quiz! level=0 Level d has at most 2d nodes. level=1 level=2 level=3 © 2010 Goodrich, Tamassia Trees

Properties of Proper Binary Trees Notation n: # of nodes ne: # of external nodes ni: # of internal nodes h: height Properties: h+1  ne  2h h  ni  2h-1 2h+1  n  2h+1-1 log2(n+1)-1  h  (n-1)/2 ne = ni+1 Quiz! Proper! level=0 Level d has at most 2d nodes. level=1 level=2 level=3 © 2010 Goodrich, Tamassia Trees

Proof by Induction: ne = ni+1 for Proper Binary Trees 8/7/2018 2:27 PM Proof by Induction: ne = ni+1 for Proper Binary Trees Prove that ne = ni+1 When ni = 1 ... When ni = 2 ... Assume the identity holds when ni  k, then when ni = k+1 … ni = 1 ni = 2 ni = k+1 © 2010 Goodrich, Tamassia Trees

Quiz Give a proper binary tree with n node Trees 8/7/2018 2:27 PM Quiz Give a proper binary tree with n node Explain why n is always odd. If n is 51, what is the numbers of internal and external nodes, respectively? © 2010 Goodrich, Tamassia Trees

Proof by Induction: n0 = n2+1 for General Binary Trees nk : # of nodes with k children, k=0, 1, 2 Proof by induction… © 2010 Goodrich, Tamassia Trees

© 2010 Goodrich, Tamassia Trees

BinaryTree ADT The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods: position p.left() position p.right() Update methods may be defined by data structures implementing the BinaryTree ADT Proper binary tree: Each node has either 0 or 2 children © 2010 Goodrich, Tamassia Trees

Linked Structure for Trees A node is represented by an object storing Element Parent node Sequence of children nodes Link to list of pointers to children  B A  D F  B Link to parent A D F C  E  C E © 2010 Goodrich, Tamassia Trees

Linked Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node Right child node B A D  B C E   A D     C E © 2010 Goodrich, Tamassia Trees

Vector Representation of Binary Trees Nodes of tree T are stored in vector S 1 A A B D … G H … 1 2 3 10 11 2 3 B D Node v is stored at S[f(v)] f(root) = 1 if v is the left child of parent(v), f(v) = 2*f(parent(v)) if v is the right child of parent(v), f(v) = 2*f(parent(v)) + 1 f() is known as level numbering 4 5 6 7 E F C J 10 11 Quiz! G H © 2010 Goodrich, Tamassia Trees

Vector Representation of Binary Trees: More Examples © 2010 Goodrich, Tamassia Trees

Quiz 1 A The binary tree is stored level-by-level in a one-dimensional array. What are the indices of nodes N and M? 2 3 B D F C J G H N L K P M © 2010 Goodrich, Tamassia Trees

Vector Representation of Binary Trees: Analysis Notation n: # of nodes in tree T N: size of vector S Time complexity Space complexity O(N), which is O(2n) in the worse case Major drawback! (So we always want to keep trees as shallow as possible!) © 2010 Goodrich, Tamassia Trees

© 2010 Goodrich, Tamassia Trees

Traversal of Binary Trees Basic types of traversal Preorder Postorder Inorder Level order © 2010 Goodrich, Tamassia Trees

Postorder Traversal for BT Can be applied to any “bottom-up” evaluation problems Evaluate an arithmetic expression Directory size computation (for general trees) © 2010 Goodrich, Tamassia Trees

Evaluate Arithmetic Expressions Based on 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 v.isExternal() return v.element() x  evalExpr(v.left()) y  evalExpr(v.right())  operator stored at v return x  y +  - 2 5 1 3 Postorder traversal ≡ Postfix notation © 2010 Goodrich, Tamassia Trees

Inorder traversal ≡ Projection! Inorder traversal: a node is visited after its left subtree and before its right subtree Application Draw a binary tree Print arithmetic expressions with parentheses Algorithm inOrder(v) if v is NULL return inOrder(v.left()) visit(v) inOrder(v.right()) 6 2 8 Inorder traversal ≡ Projection! 1 4 7 9 3 5 © 2010 Goodrich, Tamassia Trees

Inorder Traversal: Examples Properties of inorder traversal Very close to infix notation Can be obtained by tree projection © 2010 Goodrich, Tamassia Trees

Print Arithmetic Expressions Algorithm printExpression(v) if v is NULL return print(“(’’) inOrder(v.left()) print(v.element()) inOrder(v.right()) print (“)’’) Based on inorder traversal Print operand or operator when visiting node Print “(“ before traversing left subtree Print “)“ after traversing right subtree +  - 2 a 1 3 b ((2  (a - 1)) + (3  b)) © 2010 Goodrich, Tamassia Trees

Draw a Binary Tree Since inorder traversal is equivalent to tree projection, it is easy to draw a binary tree using inorder traversal. © 2010 Goodrich, Tamassia Trees

Euler Tour Traversal +   2 - 3 2 5 1 Generic traversal of a binary tree Includes a special cases the preorder, postorder and inorder traversals Walk around the tree and visit each node three times: On the left (preorder)  + x 2 – 5 1 x 3 2 From below (inorder)  2 x 5 – 1 + 3 x 2 On the right (postorder)  2 5 1 – x 3 2 x + + Quiz! L  R  B 2 - 3 2 5 1 © 2010 Goodrich, Tamassia Trees

Euler Tour Traversal Applications Determine the number of descendants of each node in a tree Fully parenthesize an arithmetic expression from an expression tree © 2010 Goodrich, Tamassia Trees

Quiz Determine a binary tree Inorder=[c q f p x z k y] Preorder=[f c q z x p y k] Inorder=[z b j d e f m c] Postorder=[b z d e m c f j] Preorder=[a b c] Postorder=[c b a] © 2010 Goodrich, Tamassia Trees

© 2010 Goodrich, Tamassia Trees

From General to Binary Trees How to transform a general tree to a binary tree  Left-child right-sibling representation Quiz! © 2010 Goodrich, Tamassia Trees