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.

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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 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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
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.
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:
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.
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.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
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.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Chapter 12 Abstract Data Type.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Trees Another Abstract Data Type (ADT)
Csc 2720 Instructor: Zhuojun Duan
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.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Trees and Binary Trees.
Data Structures & Algorithms
Trees Another Abstract Data Type (ADT)
Week nine-ten: Trees Trees.
Trees Another Abstract Data Type (ADT)
Data Structures – Week #5
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Dr.Surasak Mungsing CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
การวิเคราะห์และออกแบบขั้นตอนวิธี
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
8.2 Tree Traversals Chapter 8 - Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

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.

Nature Lover’s View Of A Tree leaves branches root

Computer Scientist’s View root leaves branches nodes Branches meet at nodes.

Trees Another Abstract Data Type (ADT) Simulates a hierarchical tree structure Has no cycles Two vertices are connected by exactly one path Recursively defined as a collection of nodes, each node has a value and a list of references to the children No reference is duplicated None points to root [https://en.wikipedia.org/wiki/Tree_(data_structure)] [https://en.wikipedia.org/wiki/Tree_(graph_theory)]

Tree [https://en.wikipedia.org/wiki/Tree_(data_structure)]

Not Tree Top left: Undirected cycle Top right: Cycle Bottom left: Cycle Bottom right: two non-connected components [https://en.wikipedia.org/wiki/Tree_(data_structure)]

Linear Lists And Trees Linear lists are useful for serially ordered data. (e0, e1, e2, …, en-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. Continent, country, state, city North America, USA, FL, Gainesville

Hierarchical Data And 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. (descendants) Elements that have no children are leaves. (external nodes) Internal node: node with at least one child

Example Tree root President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee children of root grand children of root great grand child of root

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.

Subtrees root President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee

Leaves President VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee

Parent, Grandparent, Siblings, Ancestors, Descendants President VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee

Levels Level 1 President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee Level 2 Level 3 Level 4

Caution Some 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. We shall number levels with the root at level 1.

height = depth = number of levels President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee Level 1

Node Degree = Number Of Children President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee 3 2 1 1 1

Tree Degree = Max Node Degree President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee 3 2 1 Note that it is possible to have a tree whose degree is (say) 3 and whose root has a degree that is < 3. Degree of tree = 3.

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.

Differences Between A Tree & A Binary Tree No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. A binary tree may be empty; a tree cannot be empty.

Differences Between A Tree & A Binary Tree The subtrees of a binary tree are ordered; those of a tree are not ordered. a b Are different when viewed as binary trees. Are the same when viewed as trees.

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

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

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 + 3.25

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.

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

Delimiters Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. (a + b) * (c – d) / (e – f)

Infix Expression Is Hard To Parse Need operator priorities, tie breaker, and 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. So it is easier for a computer to evaluate expressions that are in these forms.

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+

Postfix Examples Infix = a + b * c Infix = a * b + c Infix = (a + b) * (c – d) / (e + f) Postfix = a b + c d - * e f + /

Unary Operators Replace with new symbols. + a => a @ + a + b => a @ b + - a => a ? - a-b => a ? b - @ and ? are new symbols that are used to denote the unary + and – operators, respectively.

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 on to the stack. This works because, in postfix, operators come immediately after their operands.

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack a b + c d - * e f + / a b + c d - * e f + / b a

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack a b + c d - * e f + / d a b + c d - * e f + / c a b + c d - * e f + / (a + b) a b + c d - * e f + / a b + c d - * e f + /

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack a b + c d - * e f + / (c – d) (a + b)

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack

Postfix Evaluation (a + b) * (c – d) / (e + f) a b + c d - * e f + / stack

Prefix Form The prefix 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 prefix forms. Operators come immediately before the prefix form of their operands. Infix = a + b Postfix = ab+ Prefix = +ab

Binary Tree Form + a b a + b - a - a Each leaf represents a variable or constant. Each nonleaf represents an operator. The left operand (if any) of this operator is represented by the left subtree, the right subtree represents the right operand of the operator. Binary Expression Tree [https://en.wikipedia.org/wiki/Binary_expression_tree]

Binary Tree Form (a + b) * (c – d) / (e + f) / + a b - c d e f *

Merits Of Binary Tree Form Left and right operands are easy to visualize. Code optimization algorithms work with the binary tree form of an expression. Simple recursive evaluation of expression. + a b - c d e f * / Evaluate an expression– if there is no root, return null; if the root is a leaf, return the value of variable or constant in the leaf; otherwise, evaluate the left and right operands recursively; compute the root operator; return the result.