CS350: Data Structures Tree Traversal

Similar documents
Chapter 20: Binary Trees

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CS350: Data Structures Binary Search Trees

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Binary Search Trees. See Section 11.1 of the text.

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

CS 350 : Data Structures Binary Search Trees

CSI33 Data Structures

Binary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

Principles of Computer Science

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

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

TREES. Trees - Introduction

Binary Trees

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

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

Why Do We Need Trees?

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

CS350: Data Structures AA Trees

Binary Trees, Binary Search Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS350: Data Structures Red-Black Trees

Advanced Tree Data Structures

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

March 20/2003 Jayakanth Srinivasan,

Binary Trees Fall 2018 Margaret Reid-Miller

Programming II (CS300)

Trees. Tree Structure Binary Tree Tree Traversals

Cpt S 122 Data Structures. Data Structures Trees

CMSC 341. Binary Search Trees CMSC 341 BST

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

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

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

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

Binary Trees. Examples:

CSC148 Week 6. Larry Zhang

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE 326: Data Structures Binary Search Trees

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

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

COSC 2011 Section N. Trees: Terminology and Basic Properties

CMSC351 - Fall 2014, Homework #2

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Data Structures and Algorithms for Engineers

Tree Traversal. Lecture12: Tree II. Preorder Traversal

CS 206 Introduction to Computer Science II

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

CS350: Data Structures B-Trees

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

CIS 121. Binary Search Trees

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

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

Binary Trees: Practice Problems

INF2220: algorithms and data structures Series 1

Lecture 23: Binary Search Trees

CSI33 Data Structures

Lecture 37 Section 9.4. Wed, Apr 22, 2009

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

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

3 Trees: traversal and analysis of standard search trees

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

Successor/Predecessor Rules in Binary Trees

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

Data Structures And Algorithms

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

tree nonlinear Examples

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

Data Structures - Binary Trees 1

CSI33 Data Structures

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

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

Trees: examples (Family trees)

TREES Lecture 10 CS2110 Spring2014

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

CS 350 : Data Structures B-Trees

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

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

Binary Trees and Huffman Encoding Binary Search Trees

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

AVL Trees. See Section 19.4of the text, p

CE 221 Data Structures and Algorithms

CS350: Data Structures Heaps and Priority Queues

Trees. Truong Tuan Anh CSE-HCMUT

Multi-Way Search Tree

3 Trees: traversal and analysis of standard search trees. Summer Term 2010

CSE 373 Spring Midterm. Friday April 21st

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

Trees. CSE 373 Data Structures

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

BBM 201 Data structures

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

CS61B Lecture #21. A Recursive Structure. Fundamental Operation: Traversal. Preorder Traversal and Prefix Expressions. (- (- (* x (+ y 3))) z)

Trees (part 2) CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Transcription:

Tree Traversal James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola

Defining Trees Recursively Trees can easily be defined recursively Definition of a binary tree (a tree with arity of 2) - A binary tree is either empty (represented by a null pointer), or is made of a single node, where the left and the right pointers each point to a binary tree Subtree2 Subtree1 2

Defining Trees Recursively More generally, a tree with unspecified arity is defined as - A tree is a collection of nodes (one node is called the root) - A collection of nodes can be empty, otherwise a tree consists of a root node (R) and zero or more non-empty subtrees each of whose roots are connected by an edge to the root R Because they are easily defined recursively, it is also easy to traverse them (visit each node) recursively 3

Duplicating a Tree /** * Return a reference to a node that is the root of a * duplicate of the binary tree rooted at the current node. */ data public BinaryNode<AnyType> duplicate( ) { // Create the new node for the new tree left subtree right subtree BinaryNode<AnyType> root = new BinaryNode<AnyType>(element, null, null); } if( left!= null ) // If there's a left subtree root.left = left.duplicate( ); // Duplicate and attach if( right!= null ) // If there's a right subtree root.right = right.duplicate( ); // Duplicate and attach return root; // Return resulting tree 4

Determining the Size of a Tree The size of a tree can be defined as: - LeftSubtree.size( ) + RightSubtree.size( ) + 1 - Determine the size of each subtree, add them together, add 1 additional node to represent the root node Subtree2 Subtree1 5

Determining the Size of a Tree (Cont.) /** * Return the size of the binary tree rooted at t. */ public static int size( BinaryNode<AnyType> t ) { if( t == null ) return 0; else return 1 + size( t.left ) + size( t.right ); } 6

Tree Traversal A tree traversal is a systematic method of visiting each node in a tree Different tree traversal algorithms visit nodes of a tree in different orders Tree traversal algorithms - Preorder traversal - Postorder traversal - Inorder traversal - Level-order traversal 7

Preorder Traversal In preorder traversal, a node is processed and then its children are processed recursively - The duplicate method is an example of a preorder traversal -- the root is created first, then the subtrees are duplicated A B C D E F Nodes are visited in the following order: G A B D E G C F 8

Preorder Traversal In preorder traversal, a node is processed and then its children are processed recursively - The duplicate method is an example of a preorder traversal -- the root is created first, then the subtrees are duplicated 1 2 6 3 4 7 5 9

Preorder Traversal // Print tree rooted at current node using preorder traversal. public void printpreorder( ) { System.out.println( element ); // Process Node if( left!= null ) left.printpreorder( ); // Process Left Subtree if( right!= null ) right.printpreorder( ); // Process Right Subtree } 10

Postorder Traversal In postorder traversal, a node is processed after both children are processed recursively - The size method is an example of a postorder traversal -- the size of subtrees are determined to find the size of the current tree A B C D E F Nodes are visited in the following order: G D G E B F C A 11

Postorder Traversal In postorder traversal, a node is processed after both children are processed recursively - The size method is an example of a postorder traversal -- the size of subtrees are determined to find the size of the current tree 7 4 6 1 3 5 2 12

Postorder Traversal // Print tree rooted at current node using postorder traversal public void printpostorder( ) { if( left!= null ) left.printpostorder( ); // Process Left Subtree if( right!= null ) right.printpostorder( ); // Process Right Subtree System.out.println( element ); // Process Node } 13

Inorder Traversal In inorder traversal, the left child is processed recursively, then the current node is processed, then the right child is recursively processed A B C D E F Nodes are visited in the following order: G D B E G A F C 14

Inorder Traversal In inorder traversal, the left child is processed recursively, then the current node is processed, then the right child is recursively processed 5 2 7 1 3 6 4 15

Inorder Traversal // Print tree rooted at current node using inorder traversal public void printinorder( ) { if( left!= null ) left.printinorder( ); // Process Left Subtree System.out.println( element ); // Process Current Node if( right!= null ) right.printinorder( ); // Process Right Subtree } 16

Level-order Traversal In level-order traversal, nodes are processed in the tree from top to bottom, left to right - The first level is the root node, the second level consists of the children of the root node, the third level consists of the grandchildren of the root node, etc. A B C D E F Nodes are visited in the following order: G A B C D E F G 17

Level-order Traversal In level-order traversal, nodes are processed in the tree from top to bottom, left to right - The first level is the root node, the second level consists of the children of the root node, the third level consists of the grandchildren of the root node, etc. 1 2 3 4 5 6 7 18

Level-order Traversal A recursive implementation is not well-suited for level-order traversal A queue can be used to implement level-order traversal instead q = [instantiate a queue] q.push(root); while (!q.isempty()) { n = q.dequeue(); visit(n); for each child of n { q.enqueue(child); } } 19