TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.

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.
Data Structures Lecture 6 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Trees Briana B. Morrison Adapted from Alan Eugenio.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
© 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.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis 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.
CSC401 – Analysis of Algorithms Lecture Notes 4 Trees and Priority Queues Objectives: General Trees and ADT Properties of Trees Tree Traversals Binary.
General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Trees Chapter 8.
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.
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.
Trees by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
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.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
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.
Trees trees binary trees traversals of trees template method pattern
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
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.
Trees Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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.
Elementary Data Structures
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,
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.
© 2013 Goodrich, Tamassia, Goldwasser
Binary Trees, Binary Search Trees
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
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Elementary Data Structures
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
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.
Presentation transcript:

TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1

Tree in C++ 2  Outlines:  What is a Tree  Tree Terminology  ADT: Tree  Representation of Tree Node  Left Child, Right Sibling Representation  Tree Traversal  Preorder Traversal  Postorder Traversal  Inorder Traversal

Tree in C++ 3  Outlines:  Binary Tree  ADT: Binary Tree  Differences Between A Tree and A Binary Tree  Data Structure for Binary Trees  Arithmetic Expression Tree  Maximum Number of Nodes in a Binary Tree  Full Binary Tree

Tree in C++ 4 What is a Tree A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. consists of nodes with a parent-child relation. Applications: Organization charts File systems Programming environments Computers”R”Us SalesR&DManufacturing LaptopsDesktops Local International EuropeAsiaCanada

Tree in C++ 5 Tree Terminology Root: node without parent (A) Siblings: nodes share the same parent Internal node: node with at least one child (A, B, C, F) External node (leaf): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Descendant of a node: child, grandchild, grand-grandchild, etc. A B D C GH E F I J K subtree

Tree in C++ 6 Tree Terminology …. Cont. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Degree of a node: the number of its children Degree of a tree: the maximum number of its node. Subtree: tree consisting of a node and its descendants. A B D C GH E F I J K subtree

Tree in C++ 7 ADT: Tree We use positions (p) to abstract nodes Generic methods: integer size() boolean isEmpty() objectIterator elements() positionIterator positions() Accessor methods: position root() position parent(p) positionIterator children(p)

Tree in C++ 8 ADT: Tree … Cont. Query methods: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) Update methods: swapElements(p, q) object replaceElement(p, o) Additional update methods may be defined by data structures implementing the Tree ADT

Tree in C++ 9 Representation of Tree Node List Representation ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) The root comes first, followed by a list of links to sub-trees DataLink 1Link 2…Link n How many link fields are needed in such a representation?

Tree in C++ 10 Representation of Tree Node Every tree node: object – useful information children – pointers to its children Data      

Tree in C++ 11 Representation of Tree Node A node is represented by an object storing Element. Parent node. Sequence of children nodes. B D A CE F  B  ADF  C  E

Tree in C++ 12 Left Child, Right Sibling Representation Data Left Child Right Sibling A BCD IHGFE JKL

Tree in C++ 13 Tree Traversal Two main methods: Preorder Postorder Preorder: visit the root traverse in preorder the children (subtrees) Postorder traverse in postorder the children (subtrees) visit the root

Tree in C++ 14 Preorder Traversal A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Application: print a structured document Become Rich 1. Motivations3. Success Stories2. Methods 2.1 Get a CS BSc 2.2 Start a Web Site 1.1 Enjoy Life 1.2 Help Poor Friends 2.3 Acquired by Google

Tree in C++ 15 Postorder Traversal In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories cs16/ homeworks/ todo.txt 1K programs/ DDR.cpp 10K Stocks.cpp 25K h1c.doc 3K h1nc.doc 2K Robot.cpp 20K

Tree in C++ 16 Inorder Traversal In an inorder traversal a node is visited after its left subtree and before its right subtree

Tree in C++ 17 Binary Tree A binary tree is a tree with the following properties: Each internal node has at most two children (degree of two). 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 FG D E H I

Tree in C++ 18 ADT: Binary Tree The Binary Tree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT. Additional methods: position leftChild(p) position rightChild(p) position sibling(p) Update methods may be defined by data structures implementing the Binary Tree ADT.

Tree in C++ 19 ADT: Binary Tree Examples of the Binary Tree A B C G E I D H F Complete Binary Tree A B A B Skewed Binary Tree E C D 5

Tree in C++ 20 Differences Between A Tree and A Binary Tree a) The subtrees of a binary tree are ordered; those of a tree are not ordered. b) Binary tree can be empty while general tree can not. c) Nodes in binary tree can not more than 2 where as in tree there is no limit. Are different when viewed as binary trees. Are the same when viewed as trees. A B A B

Tree in C++ 21 Data Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node Right child node B D A CE   B AD CE 

Tree in C++ 22 Arithmetic Expression Tree 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))    2 a1 3b

23 Infix: All binary operators appear between the two operands Example: ((1+2) * (3-4)) Prefix: All operators appear near the beginning of the equation, with the two operands appearing right after. Example: * – 3 4 Postfix: The operators will appear near the end Example: * Assignment: (9 + (12 / 3)) * ((12 /4) – 2)

Tree in C++ 24 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? How about drink?How about shopping? Fruits Coffee Drive to City center Continue driving Yes No YesNoYesNo

Tree in C++ 25 Maximum Number of Nodes in a Binary Tree The maximum number of nodes on depth i of a binary tree is 2 i, where i>=0. The maximum nubmer of nodes in a binary tree of height k is 2 k+1 -1, where k>=0. Prove by induction

Tree in C++ 26 Full Binary Tree A full binary tree of a given height k has 2 k+1 –1 nodes. Height 3 full binary tree.