LECTURE 11 TREE TRAVERSALS

Similar documents
Figure 1. A breadth-first traversal.

LECTURE 17 GRAPH TRAVERSALS

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

CS24 Week 8 Lecture 1

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

LECTURE 26 PRIM S ALGORITHM

Tree traversals and binary trees

11 TREES DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

implementing the breadth-first search algorithm implementing the depth-first search algorithm

LECTURE 13 BINARY TREES

Trees. Eric McCreath

Trees. CSE 373 Data Structures

Trees and Tree Traversal

An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include:

Trees. Carlos Moreno uwaterloo.ca EIT

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

Binary Trees

LECTURE 18 AVL TREES

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

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

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

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

Recursive Data Structures and Grammars

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Trees. Truong Tuan Anh CSE-HCMUT

Stacks, Queues & Trees. The Stack Interface. The Stack Interface. Harald Gall, Prof. Dr.

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Why Do We Need Trees?

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Brute Force: Selection Sort

Algorithms and Data Structures

Advanced Data Structures and Algorithms

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

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

12 Abstract Data Types

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

Trees. Tree Structure Binary Tree Tree Traversals

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

CSE 530A. B+ Trees. Washington University Fall 2013

CSE 214 Computer Science II Introduction to Tree

DATA STRUCTURES AND ALGORITHMS

[ DATA STRUCTURES ] Fig. (1) : A Tree

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

CS 171: Introduction to Computer Science II. Binary Search Trees

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

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

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Data Structure Advanced

Binary Trees, Binary Search Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Programming II (CS300)


ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

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

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III

LECTURE 3 ALGORITHM DESIGN PARADIGMS

Analysis of Algorithms

Data Structures Question Bank Multiple Choice

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Unweighted Graphs & Algorithms

CSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Algorithms for Data Structures: Uninformed Search. Phillip Smith 19/11/2013

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

Backtracking. Chapter 5

Graphs & Digraphs Tuesday, November 06, 2007

Binary Trees. Height 1

Algorithms. Deleting from Red-Black Trees B-Trees

Module 6: Binary Trees

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

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

CSC148H Week 8. Sadia Sharmin. July 12, /29

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

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

CMSC 341 Lecture 15 Leftist Heaps

Lecture 37 Section 9.4. Wed, Apr 22, 2009

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

CMSC 341 Lecture 15 Leftist Heaps

lecture27: Graph Traversals

Recitation 9. Prelim Review

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

An Introduction to Trees

08 STACKS DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

CSCE 2014 Final Exam Spring Version A

Data Structures and Algorithms

CSCI-1200 Data Structures Fall 2014 Lecture 18 Trees, Part III

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

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

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Transcription:

DATA STRUCTURES AND ALGORITHMS LECTURE 11 TREE TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD BACKGROUND All the objects stored in an array or linked list can be accessed sequentially When discussing deques, we introduced iterators in C++: These allow the user to step through all the objects in a container Question: How can we iterate through all the objects in a tree in a predictable and efficient manner? 2 Lecture 11 - Tree Traversals 1

TYPES OF TRAVERSALS We have already seen one traversal: The breadth-first traversal visits all nodes at depth k before proceeding onto depth k + 1 Easy to implement using a queue Another approach is to visit always go as deep as possible before visiting other siblings: depth-first traversals 3 Breadth-first traversals visit all nodes at a given depth Can be implemented using a queue Run time is Θ(n) Memory is potentially expensive: maximum nodes at a given depth Order: A B H C D G I E F J K 4 Lecture 11 - Tree Traversals 2

The implementation: Create a queue and push the root node onto the queue While the queue is not empty: Push all of its children of the front node onto the queue Pop the front node 5 BACKTRACKING To discuss depth-first traversals, we will define a backtracking algorithm for stepping through a tree: At any node, we proceed to the first child that has not yet been visited Or, if we have visited all the children (of which a leaf node is a special case), we backtrack to the parent and repeat this decision making process We end once all the children of the root are visited 6 Lecture 11 - Tree Traversals 3

DEPTH-FIRST TRAVERSAL We define such a path as a depth-first traversal We note that each node could be visited twice in such a scheme The first time the node is approached (before any children) The last time it is approached (after all children) 7 DEPTH-FIRST TRAVERSAL PRE-ORDER Visiting each node first results in the sequence A, B, C, D, E, F, G, H, I, J, K, L, M 8 Lecture 11 - Tree Traversals 4

DEPTH-FIRST TRAVERSAL POST-ORDER Visiting the nodes with their last visit: D, C, F, G, E, B, J, K, L, I, M, H, A 9 DEPTH-FIRST TRAVERSAL IMPLEMENTATION Depth-first traversals can be implemented with recursion: template <typename Type> void Simple_tree<Type>::depth_first_traversal() const { // Perform pre-visit operations on the element std::cout << element << ' '; // Perform a depth-first traversal on each of the children for ( ece250::single_node<simple_tree *> *ptr = children.head(); ptr!= 0; ptr = ptr->next() ) { ptr->retrieve()->depth_first_traversal(); } } // Perform post-visit operations on the element std::cout << element << ' '; 10 Lecture 11 - Tree Traversals 5

DEPTH-FIRST TRAVERSAL IMPLEMENTATION Alternatively, we can use a stack: Create a stack and push the root node onto the stack While the stack is not empty: Pop the top node Push all of the children of that node to the top of the stack in reverse order Run time is Θ(n) The objects on the stack are all unvisited siblings from the root to the current node If each node has a maximum of two children, the memory required is Q(h): the height of the tree With the recursive implementation, the memory is Q(h): recursion just hides the memory 11 Push the root directory A 12 Lecture 11 - Tree Traversals 6

Pop A and push its two sub-directories: B and H 13 Pop B and push C, D, and G 14 Lecture 11 - Tree Traversals 7

Pop H and push its one sub-directory I 15 Pop C: no sub-directories 16 Lecture 11 - Tree Traversals 8

Pop D and push E and F 17 Pop G 18 Lecture 11 - Tree Traversals 9

Pop I and push J and K 19 Pop E 20 Lecture 11 - Tree Traversals 10

Pop F 21 Pop J 22 Lecture 11 - Tree Traversals 11

Pop K and the queue is empty 23 Lecture 11 - Tree Traversals 12