Binary Tree Traversals

Slides:



Advertisements
Similar presentations
Lecture 11 Binary Search Tree Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Advertisements

1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Binary Trees CS 110: Data Structures and Algorithms First Semester,
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself. 
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
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.
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 TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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.
Traversing Trees. Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. Consider the three operations: V: Visit a node.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
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. 
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Trees Saurav Karmakar
Articulation Points 2 of 2 (Algorithm)
Chapter 12 – Data Structures
Data Structures Michael J. Watts
Recursive Objects (Part 4)
Trees ---- Soujanya.
Chapter 15 Lists Objectives
Paul Tymann and Andrew Watkins
Section 8.1 Trees.
Cse 373 April 14th – TreEs pt 2.
i206: Lecture 13: Recursion, continued Trees
Binary Tree Applications
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
ITEC 2620M Introduction to Data Structures
Lesson 6. Types Equality and Identity. Collections.
Paul Tymann, Andrew Watkins,
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Tree Chapter 8 (cont’) Part2.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Paul Tymann, Andrew Watkins,
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 20: Binary Trees.
List Iterator Implementation
Binary Trees.
Binary Tree Traversal.
CS203 Lecture 14.
Binary Tree Implementation And Applications
Red Black Trees Top-Down Deletion.
Data Structures Using C++ 2E
CS2005 Week 8 Lectures Maps & Binary Trees.
Trees.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Binary Tree Traversals Lecture 38 Fri, Apr 28, 2006 1/16/2019 Binary Trees

Topics Binary tree traversals Binary tree iterators Pre-order traversals In-order traversals Post-order traversals Level-order traversals Binary tree iterators Pre-order iterators In-order iterators Post-order iterators Level-order iterators 1/16/2019 Binary Trees

Binary Tree Traversals Four Standard Traversals Pre-order Traversal. In-order Traversal. Post-order Traversal. Level-order Traversal. 1/16/2019 Binary Trees

Pre-order Traversal At each node, Also called an VLR traversal. First visit the node, Then perform a pre-order traversal of the left subtree, Then perform a pre-order traversal of the right subtree. Also called an VLR traversal. Visit - Left - Right. 1/16/2019 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 1/16/2019 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 1/16/2019 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 1/16/2019 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 1/16/2019 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 1/16/2019 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 1/16/2019 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees

Pre-Order Traversal Order: 10, 20, 40, 50, 30, 60, 70 10 20 30 40 50 1/16/2019 Binary Trees

In-order Traversal At each node, Also called an LVR traversal. First, perform an in-order traversal of the left subtree, Then visit the node, Then perform an in-order traversal of the right subtree. Also called an LVR traversal. Left - Visit - Right. 1/16/2019 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 40 1/16/2019 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 20 40 1/16/2019 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 20 40 50 1/16/2019 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 1/16/2019 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 60 1/16/2019 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 1/16/2019 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees

In-Order Traversal Order: 40, 20, 50, 10, 60, 30, 70 10 20 30 40 50 60 1/16/2019 Binary Trees

Post-order Traversal At each node, Also called an LRV traversal. First perform a post-order traversal of the left subtree, Then perform a post-order traversal of the right subtree, Then visit the node. Also called an LRV traversal. Left - Right - Visit. 1/16/2019 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 40 1/16/2019 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 40 50 1/16/2019 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 1/16/2019 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 1/16/2019 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 70 1/16/2019 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 20 30 40 50 60 70 1/16/2019 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees

Post-Order Traversal Order: 40, 50, 20, 60, 70, 30, 10 10 20 30 40 50 1/16/2019 Binary Trees

Level-order Traversal Traverse the levels of the tree from top to bottom. Within each level, visit the nodes from left to right. 1/16/2019 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 1/16/2019 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 1/16/2019 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 1/16/2019 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 1/16/2019 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 1/16/2019 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 1/16/2019 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 1/16/2019 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 1/16/2019 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 Order: 10, 20, 30, 40, 50, 60, 70 1/16/2019 Binary Trees

Binary Tree Iterators A binary tree iterator systematically visits each node of a binary tree. The four standard iterators correspond to the four standard traversal methods. Pre-order iterator In-order iterator Post-order iterator Level-order iterator 1/16/2019 Binary Trees

Pre-order Iterator Visit the nodes in a pre-order traversal. Use a stack to store unvisited nodes. Begin with the root node. Visit the node. If there is a right subtree, push the pointer to it. If there is a left subtree, move to the left subtree and continue recursively. If there is no left subtree, pop a node and continue recursively. 1/16/2019 Binary Trees

Pre-order Iterator 10 20 30 40 50 60 70 10 10 10 20 20 20 30 30 30 40 50 60 70 1/16/2019 Binary Trees

In-order Iterator Visit the nodes in an in-order traversal. Use a stack to store unvisited nodes. Move from the root to the “leftmost” node, pushing nodes along the way, etc. 1/16/2019 Binary Trees

In-order Iterator 10 20 30 40 50 60 70 10 10 20 20 30 30 40 50 60 70 1/16/2019 Binary Trees

Post-order Iterator Visit the nodes in a post-order traversal. Use a stack to store unvisited nodes. Move to the “leftmost” leaf, pushing nodes along the way, etc. 1/16/2019 Binary Trees

Post-order Iterator 10 20 30 40 50 60 70 10 10 20 20 30 30 40 50 60 70 1/16/2019 Binary Trees

Level-order Iterator Visit the nodes in a level-order traversal. Use a queue to store unvisited nodes. Begin with the root node. Enqueue the node’s children, left first. Visit the node. Dequeue a node and continue recursively until the queue is empty. 1/16/2019 Binary Trees

Level-order Iterator 10 20 30 40 50 60 70 10 20 20 30 30 40 40 50 50 60 60 70 70 1/16/2019 Binary Trees