CC 215 DATA STRUCTURES TREES TRAVERSALS Dr. Manal Helal - Fall 2014 Lecture 9 AASTMT Engineering and Technology College 1.

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Binary Tree Traversals
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
1 Binary Tree Traversals Binary Tree Traversal classification BreadthFirst traversal DepthFirst traversal Accept method of BinaryTree class Binary Tree.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Graph Traversals Depth-First Traversal. The Algorithm.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Transforming Infix to Postfix
1 Binary Tree Traversals Binary Tree Traversal classification Tree traversal animations DepthFirst traversal abstraction Accept method of AbstractTree.
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,
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
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 
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
Discrete Structures Trees (Ch. 11)
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Discrete Mathematics Chapter 10 Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Chapter 12 Abstract Data Type.
Paul Tymann and Andrew Watkins
Tree.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
CS212: Data Structures and Algorithms
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Paul Tymann, Andrew Watkins,
Binary Tree Traversal Methods
Binary Tree Traversals
Binary Tree Traversals
Section 9.3 by Andrew Watkins
Binary Tree Traversals
Binary Tree Chapter 8 (cont’) Part2.
Binary Trees, Binary Search Trees
Paul Tymann, Andrew Watkins,
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
Binary Tree Traversals
Presentation transcript:

CC 215 DATA STRUCTURES TREES TRAVERSALS Dr. Manal Helal - Fall 2014 Lecture 9 AASTMT Engineering and Technology College 1

Readings  Reading  Chapter

Binary Tree Traversals  Tree Traversal classification  BreadthFirst traversal  DepthFirst traversals: Pre-order, In-order, and Post- order  Reverse DepthFirst traversals  Invoking BinaryTree class Traversal Methods  accept method of BinaryTree class  BinaryTree Iterator  Using a BinaryTree Iterator  Expression Trees  Traversing Expression Trees

Tree Traversal Classification  The process of systematically visiting all the nodes in a tree and performing some processing at each node in the tree is called a tree traversal.  A traversal starts at the root of the tree and visits every node in the tree exactly once.  There are two common methods in which to traverse a tree: 1. Breadth-First Traversal (or Level-order Traversal). 2. Depth-First Traversal: Preorder traversal Inorder traversal (for binary trees only) Postorder traversal

Breadth-First Traversal Let queue be empty; if(tree is not empty) queue.enqueue(tree); while(queue is not empty){ tree = queue.dequeue(); visit(tree root node); if(tree.leftChild is not empty) enqueue(tree.leftChild); if(tree.rightChild is not empty) enqueue(tree.rightChild); } Note: When a tree is enqueued, it is the address of the root node of that tree that is enqueued visit means to process the data in the node in some way

Breadth-First Traversal (Contd.) The BinaryTree class breadthFirstTraversal method: public void breadthFirstTraversal(Visitor visitor){ QueueAsLinkedList queue = new QueueAsLinkedList(); if(!isEmpty()) // if the tree is not empty queue.enqueue(this); while(!queue.isEmpty() && !visitor.isDone()){ BinaryTree tree = (BinaryTree)queue.dequeue(); visitor.visit(tree.getKey()); if (!tree.getLeft().isEmpty()) queue.enqueue(tree.getLeft()); if (!tree.getRight().isEmpty()) queue.enqueue(tree.getRight()); }

Breadth-First Traversal (Contd.) Breadth-First traversal visits a tree level-wise from top to bottom K F U P M S T A R

Breadth-First Traversal (Contd.) Exercise: Write a BinaryTree instance method for Reverse Breadth-First Traversal R A T S M P U F K

Depth-First Traversals CODEfor each Node:Name public void preorderTraversal(Visitor v){ if(!isEmpty() && ! v.isDone()){ v.visit(getKey()); getLeft().preorderTraversal(v); getRight().preorderTraversal(v); } Visit the node Visit the left subtree, if any. Visit the right subtree, if any. Preorder (N-L-R) public void inorderTraversal(Visitor v){ if(!isEmpty() && ! v.isDone()){ getLeft().inorderTraversal(v); v.visit(getKey()); getRight().inorderTraversal(v); } Visit the left subtree, if any. Visit the node Visit the right subtree, if any. Inorder (L-N-R) public void postorderTraversal(Visitor v){ if(!isEmpty() && ! v.isDone()){ getLeft().postorderTraversal(v) ; getRight().postorderTraversal(v); v.visit(getKey()); } Visit the left subtree, if any. Visit the right subtree, if any. Visit the node Postorder (L-R-N)

Preorder Depth-first Traversal N-L-R “A node is visited when passing on its left in the visit path” K F P M A U S R T

Inorder Depth-first Traversal L-N-R “A node is visited when passing below it in the visit path” P F A M K S R U T Note: An inorder traversal can pass through a node without visiting it at that moment.

Postorder Depth-first Traversal L-R-N “A node is visited when passing on its right in the visit path” P A M F R S T U K Note: An postorder traversal can pass through a node without visiting it at that moment.

Reverse Depth-First Traversals  There are 6 different depth-first traversals: NLR (pre-order traversal) NRL (reverse pre-order traversal) LNR (in-order traversal) RNL (reverse in-order traversal) LRN (post-order traversal) RLN (reverse post-order traversal)  The reverse traversals are not common  Exercise: Perform each of the reverse depth-first traversals on the tree:

Expression Trees  An arithmetic expression or a logic proposition can be represented by a Binary tree:  Internal vertices represent operators  Leaves represent operands  Subtrees are subexpressions  A Binary tree representing an expression is called an expression tree.  Build the expression tree bottom-up:  Construct smaller subtrees  Combine the smaller subtrees to form larger subtrees

Example: Expression Trees  Leaves are operands (constants or variables)  The internal nodes contain operators  Will not be a binary tree if some operators are not binary

Preorder, Postorder and Inorder  Preorder traversal  node, left, right  prefix expression ++a*bc*+*defg

Preorder, Postorder and Inorder  Postorder traversal  left, right, node  postfix expression abc*+de*f+g*+  Inorder traversal  left, node, right  infix expression a+b*c+d*e+f*g

Expression Trees (Contd.) Example: Create the expression tree of (A + B) 2 + (C - 5) / 3

Expression Trees (Contd.) Example: Create the expression tree of the compound proposition:  (p  q)  (  p   q)

Traversing Expression Trees An inorder traversal of an expression tree produces the original expression (without parentheses), in infix order A preorder traversal produces a prefix expression A postorder traversal produces a postfix expression Prefix: + ^ + A B 2 / - C 5 3 Infix: A + B ^ 2 + C – 5 / 3 Postfix: A B + 2 ^ C / +

Example: UNIX Directory

Example: Unix Directory Traversal PreOrder PostOrder

Convert a Generic Tree to a Binary Tree