© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.

Slides:



Advertisements
Similar presentations
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Trees Chapter 8.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Nature Lover’s View Of A Tree root branches leaves.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Evaluation of Expressions
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications.
Trees Nature Lover’s View Of A Tree root branches leaves.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Lecture 8 Tree.
Tree Data Structures.
درختها Trees ساختمان داده ها والگوريتمها مقايسه ليست خطي و درخت Linear lists are useful for serially ordered data. – (e 0, e 1, e 2, …, e n-1 ) – Days.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
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.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
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.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
CHP-3 STACKS.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
Review Use of Stack Introduction Stack in our life Stack Operations
Chapter 12 Abstract Data Type.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
CHAPTER 4 Trees.
Stack application: postponing data usage
PART II STACK APPLICATIONS
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Binary Trees, Binary Search Trees
Trees and Binary Trees.
Stacks, Queues, and Deques
Week nine-ten: Trees Trees.
Section 9.3 by Andrew Watkins
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees.
Dr.Surasak Mungsing CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ.
(Part 2) Infix, Prefix & Postfix
การวิเคราะห์และออกแบบขั้นตอนวิธี
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Binary Trees, Binary Search Trees
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson

2 © University of Auckland Non computer science view of a tree leaves branches root

3 © University of Auckland The computer science view root branches leaves

4 © University of Auckland Linear Lists And Trees Linear lists are useful for serially ordered data. (e 0, e 1, e 2, …, e n-1 ) Days of week. Months in a year. Students in this class. Trees are useful for hierarchically ordered data. Employees of a corporation. President, vice presidents, managers, and so on. Java’s classes. Object is at the top of the hierarchy. Subclasses of Object are next, and so on.

5 © University of Auckland Hierarchical Data & Trees The element at the top of the hierarchy is the root Elements next in the hierarchy are the children of the root Elements next in the hierarchy are the grandchildren of the root, and so on… Elements at the lowest level of the hierarchy are the leaves

6 © University of Auckland descendant of root grand children of root children of root Java’s Classes Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException root

7 © University of Auckland Definition A tree t is a finite nonempty set of elements One of these elements is called the root The remaining elements, if any, are partitioned into trees, which are called the subtrees of t

8 © University of Auckland Subtrees Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException root

9 © University of Auckland Leaves Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException

10 © University of Auckland Parent, Grandparent, Siblings, Ancestors, Descendents Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException

11 © University of Auckland Levels Level 4 Level 3 Level 2 Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException Level 1

12 © University of Auckland Caution Some CS texts start level numbers at 0 rather than at 1 Root is at level 0 Its children are at level 1 The grand children of the root are at level 2 And so on

13 © University of Auckland height = depth = number of levels Level 4 Level 3 Level 2 Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException Level 1

14 © University of Auckland Node Degree = Number Of Children Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException

15 © University of Auckland Tree Degree = Max Node Degree Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException Degree of tree = 3

16 © University of Auckland Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are partitioned into two binary trees These are called the left and right subtrees of the binary tree

17 © University of Auckland Differences Between a Tree & a Binary Tree No node in a binary tree may have a degree more than 2 There is no limit on the degree of a node in a tree A binary tree may be empty; a tree cannot be empty The subtrees of a binary tree are ordered Those of a tree are not ordered

18 © University of Auckland The subtrees of a binary tree are ordered; those of a tree are not ordered. a b a b Trees 1 & 2 are different when viewed as binary trees (because of left right ordering). But they are the same when viewed as trees 12 Differences Between a Tree & a Binary Tree

19 © University of Auckland Arithmetic Expressions (a + b) * (c + d) + e – f/g*h Expressions comprise three kinds of entities. Operators (+, -, /, *). Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d), etc.). Delimiters ((, )).

20 © University of Auckland Operator Degree Number of operands that the operator requires. Binary operator requires two operands.  a + b  c / d  e - f Unary operator requires one operand.  + g  - h

21 © University of Auckland Infix Form Normal way to write an expression. Binary operators come in between their left and right operands.  a * b  a + b * c  a * b / c  (a + b) * (c + d) + e – f/g*h

22 © University of Auckland Operator Priorities How do you figure out the operands of an operator?  a + b * c  a * b + c / d This is done by assigning operator priorities.  priority(*) = priority(/) > priority(+) = priority(-) When an operand lies between two operators, the operand associates with the operator that has higher priority.

23 © University of Auckland Tie Breaker When an operand lies between two operators that have the same priority, the operand associates with the operator on the left.  a + b - c  a * b / c / d

24 © University of Auckland Delimiters Sub-expression within delimiters is treated as a single operand, independent from the remainder of the expression.  (a + b) * (c – d) / (e – f) single operand

25 © University of Auckland Infix Expression Is Hard To Parse Need operator priorities, apply tie breaker, and find delimiters. This makes computer evaluation more difficult than is necessary. Postfix and prefix expression forms do not rely on operator priorities, a tie breaker, or delimiters (eg Polish notation) So it is easier for a computer to evaluate expressions that are in these forms.

26 © University of Auckland Postfix Form The postfix form of a variable or constant is the same as its infix form.  a, b, 3.25 The relative order of operands is the same in infix and postfix forms. Operators come immediately after the postfix form of their operands.  Infix = a + b  Postfix = ab+

27 © University of Auckland Postfix Examples Infix = a * b + c  Postfix = ab*c+ Infix = (a + b) * (c – d) / (e + f)  Postfix = ab+cd-*ef+/ postfix

28 © University of Auckland Postfix Evaluation Scan postfix expression from left to right pushing operands on to a stack. When an operator is encountered, pop as many operands as this operator needs; evaluate the operator & push the result back on to the stack. This works because, in postfix, operators come immediately after their operands.

29 © University of Auckland Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack a  a b + c d - * e f + / b

30 © University of Auckland Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack (a + b)  a b + c d - * e f + / c d

31 © University of Auckland Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack (a + b)  a b + c d - * e f + / (c – d)

32 © University of Auckland Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack (a + b)*(c – d)  a b + c d - * e f + / e f

33 © University of Auckland Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack (a + b)*(c – d)  a b + c d - * e f + / (e + f)  a b + c d - * e f + /