Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Similar documents
Partha Sarathi Mandal

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Binary Trees, Binary Search Trees

Data Structure - Binary Tree 1 -

March 20/2003 Jayakanth Srinivasan,

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Introduction to Computers and Programming. Concept Question

TREES. Trees - Introduction

CE 221 Data Structures and Algorithms

Data Structures, Algorithms & Data Science Platforms

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Formal Languages and Automata Theory, SS Project (due Week 14)

CSI33 Data Structures

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Tree Structures. A hierarchical data structure whose point of entry is the root node

Trees. (Trees) Data Structures and Programming Spring / 28

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

Why Do We Need Trees?

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

CSI33 Data Structures

Binary Trees

Recursive Data Structures and Grammars

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Stacks, Queues and Hierarchical Collections

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Binary Trees. Height 1

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

12 Abstract Data Types


Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Trees. Trees. CSE 2011 Winter 2007

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Programming II (CS300)

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION


Topic 14. The BinaryTree ADT

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

Analysis of Algorithms

Trees. Truong Tuan Anh CSE-HCMUT

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Trees. CSE 373 Data Structures

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Lecture 26. Introduction to Trees. Trees

LECTURE 13 BINARY TREES

Largest Online Community of VU Students

17CS33:Data Structures Using C QUESTION BANK

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Trees: examples (Family trees)

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

Data Structures and Algorithms for Engineers

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Tree Data Structures CSC 221

tree nonlinear Examples

CSC148 Week 6. Larry Zhang

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

BBM 201 Data structures

Name CPTR246 Spring '17 (100 total points) Exam 3

Data Structures - Binary Trees 1

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Garbage Collection: recycling unused memory

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

EE 368. Week 6 (Notes)

Trees. Tree Structure Binary Tree Tree Traversals

Trees, Binary Trees, and Binary Search Trees

CSE 230 Intermediate Programming in C and C++ Binary Tree

CS61B Lecture #20: Trees. Last modified: Wed Oct 12 12:49: CS61B: Lecture #20 1

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

COMP 250 Fall binary trees Oct. 27, 2017

TREES Lecture 10 CS2110 Spring2014

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

Department of Computer Science and Technology

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Carlos Delgado Kloos Mª Carmen Fernández Panadero Raquel M. Crespo García Dep. Ingeniería Telemática Univ. Carlos III de Madrid

Data Structure. IBPS SO (IT- Officer) Exam 2017

Introduction to Binary Trees

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

CS301 - Data Structures Glossary By

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Transcription:

SE-286: Data Structures t and Programming Tree Virendra Singh Indian Institute of Science Bangalore Lecture 11 Courtesy: Prof. Sartaj Sahni 1

Trees

Nature Lover sviewofatree leaves branches root 3

Computer Scientist s s View root leaves branches nodes 4

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

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. Elements that have no children are leaves. 6

Java s Classes (Part Of Figure 1.1) Object root children of root Number Throwable OutputStream Integer Double Exception FileOutputStream grand children of root RuntimeException great grand child of root 7

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

Subtrees Object root Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 9

Leaves Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 10

Parent, Grandparent, Siblings, Ancestors, Descendants Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 11

Levels Object Level 1 Level 2 Number Throwable OutputStream Integer Double Exception FileOutputStream Level 3 RuntimeException Level 4 12

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. 13

height = depth = number of levels Object Level 1 Level 2 Number Throwable OutputStream Integer Double Exception FileOutputStream Level 3 RuntimeException Level 4 14

Node Degree = Number Of Children Object 3 2 Number 1 Throwable 1 OutputStream 0 0 1 0 Integer Double Exception FileOutputStream RuntimeException 0 15

Tree Degree = Max Node Degree Object 3 2 Number 1 Throwable 1 OutputStream 0 0 1 0 Integer Double Exception FileOutputStream Degree of tree = 3. RuntimeException 0 16

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

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 ee of a node in a tree. A binary tree may be empty; a tree cannot be empty. 18

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

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. 20

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 ee of a node in a tree. A binary tree may be empty; a tree cannot be empty. 21

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

Binary Tree Properties & Representation 23

Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each of first h levels. minimum number of nodes is h 24

Maximum Number Of Nodes All possible nodes at first h levels l are present. Maximum number of nodes = 1+2+4+8+ + + + +2 h-1 Sep 3,2010 = 2 h SE286@SERC 25-1

Number Of Nodes & Height Let n be the number of nodes in a binary tree whose height is h. h <= n <= 2 h 1 log 2 (n+1) <= h <= n 26

Full Binary Tree A full binary tree of a given height h has 2 h 1 nodes. Height 4 full binary tree. 27

Numbering Nodes In A Full Binary Tree Number the nodes 1 through 2 h 1. Number by levels from top to bottom. Within a level l number from left to right. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 28

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Parent of node i is node i/2 2, unless i = 1. Node 1 is the root and has no parent. 29

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Left child of node i is node 2i, unless 2i > n, where n is the number of nodes. If 2i > n, node i has no left child. 30

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes. If 2i+1 > n, node i has no right child. 31

Complete Binary Tree With n Nodes Start with a full binary tree that has at least n nodes. Number the nodes as described earlier. The binary tree defined by the nodes numbered 1 through n is the unique n node complete binary tree. 32

Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Complete binary tree with 10 nodes. 33

Binary Tree Traversal Methods In a traversal of a binary tree, each element of the binary tree is visited exactly once. During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken. 34

Binary Tree Traversal Methods Preorder Inorder Postorder Level order 35

Preorder Traversal public static void preorder(binarytreenode t) { if (t!= null) { visit(t); preorder(t.leftchild); preorder(t.rightchild); } } 36

Preorder Example (visit = print) a b c a b c 37

Preorder Example (visit = print) a b d e g h i f c j a b d g h e i c f j 38

Preorder Of Expression Tree / * + - a b c d e + f / *+ab- cd+e f Gives prefix form of expression! 39

Inorder Traversal public static void inorder(binarytreenode t) { if (t!= null) { inorder(t.leftchild); visit(t); inorder(t.rightchild); } } 40

Inorder Example (visit = print) a b c b a c 41

Inorder Example (visit = print) a b d e g h i f c j g d h b e i a f j c 42

Inorder By Projection (Squishing) a b d e g h i f c j g d h b e i a f j c 43

Inorder Of Expression Tree / * + - a b c d e + f a + b * c - d / e + f Gives infix form of expression (sans parentheses)! 44

Postorder Traversal public static void postorder(binarytreenode t) { if (t!= null) { postorder(t.leftchild); postorder(t.rightchild); rightchild); visit(t); } } 45

Postorder Example (visit = print) a b c b c a 46

Postorder Example (visit = print) a b d e g h i f c j g h d i e b j f c a 47

Postorder Of Expression Tree / * + - a b c d e + f ab+cd- *ef +/ Gives postfix form of expression! 48

Traversal Applications a d b e g h i Make a clone. Determine height. f c j Determine number of nodes. 49

Level Order Let t be the tree root. while (t!= null) { visit t and put its children on a FIFO queue; remove a node from the FIFO queue and call it t; // remove returns null when queue is empty } 50

Level-Order Example (visit = print) a b d e g h i f c j a b c d e f g h i j 51

Binary Tree Construction Suppose that the elements in a binary tree are distinct. i Can you construct the binary tree from which h a given traversal sequence came? When a traversal sequence has more than one element, the binary tree is not uniquely defined. Therefore, the tree from which h the sequence was obtained cannot be reconstructed uniquely. 52

Some Examples preorder = ab b a a b inorder b a = ab a b postorder = ab d b b a a level order a a = ab b b 53

Binary Tree Construction Can you construct the binary tree, given two traversal sequences? Depends on which two sequences are given. 54

Preorder And Postorder preorder = ab a a postorder = ba b b Preorder and postorder do not uniquely define a binary tree. Nor do preorder and level order (same example). Nor do postorder and level order (same example). 55

Inorder And Preorder inorder = g d h b e i a f j c preorder = abdgheicfj Scan the preorder left to right using the inorder to separate left and right subtrees. a is the root of the tree; gdhbei are in the left subtree; fjc are in the right subtree. a gdhbei fjc 56

Inorder And Preorder a gdhbei fjc preorder = a bdgheicfj b is the next root; gdh are in the left subtree; ei are in the right subtree. a gdh b fjc ei 57

Inorder And Preorder a gdh b ei fjc preorder = a b d g h e i c f j d is the next root; g is in the left subtree; h is in the right subtree. a g d b h ei 58 fjc

Inorder And Postorder Scan postorder from right to left using inorder to separate left and right subtrees. inorder = g d h b e i a f j c postorder = g h d i e b j f c a Tree root is a; gdhbei are in left subtree; fjc are in right subtree. 59

Inorder And Level Order Scan level order from left to right using inorder to separate left and right subtrees. inorder = g d h b e i a f j c level order = a b c d e f g h i j Tree root is a; gdhbei are in left subtree; fjc are in right subtree. 60

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, 325 3.25, (a + b), (c + d), etc.). Delimiters ((, )). 61

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 62

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 63

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. 64

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 65

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

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. 67

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

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

Unary Operators Replace with new symbols. + a => a@ + a + b => a @ b + - a =>a? - a-b => a? b - 70

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. 71

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 72

Binary Tree Form a + b a + b -a - a 73

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

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. / * + + - e f a b c d 75