COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22

Slides:



Advertisements
Similar presentations
Review: Search problem formulation
Advertisements

2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Data Structures Using C++1 Chapter 11 Binary Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
B+ Trees COMP
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
2014-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Starting at Binary Trees
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW B Trees and B+ Trees COMP 261.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
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 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
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.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
COMP 103 Binary Search Trees II Marcus Frean 2014-T2 Lecture 26
CSE 373 Data Structures Lecture 7
Trees Saurav Karmakar
CC 215 Data Structures Trees
Uninformed search Lirong Xia Spring, Uninformed search Lirong Xia Spring, 2017.
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
Recursive Objects (Part 4)
COMP 103 Introduction to Trees Thomas Kuehne 2013-T2 Lecture 19
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Trees.
Trees.
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
CSE 373 Data Structures Lecture 7
Binary Search Trees (I)
COMP 103 Binary Search Trees.
Depth-first vs breadth-first traversals
i206: Lecture 13: Recursion, continued Trees
Data Structures and Database Applications Binary Trees in C#
Artificial Intelligence (CS 370D)
Searching for Solutions
Trees CMSC 202, Version 5/02.
Tree Searching.
CMSC 202 Trees.
Artificial Intelligence
Non-Linear Structures
Uninformed search Lirong Xia. Uninformed search Lirong Xia.
Uninformed search Lirong Xia. Uninformed search Lirong Xia.
CSC 143 Binary Search Trees.
Tree Searching.
Tree Searching.
Tree Searching.
Trees.
Binary Search Trees CS 580U Fall 17.
Trees.
Presentation transcript:

COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22 Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas Kuehne VUW Lindsay Groves School of Engineering and Computer Science, Victoria University of Wellington 2016-T2 Lecture 22

RECAP-TODAY RECAP Building and traversing general trees Recursive depth-first traversal TODAY Depth-First vs Breadth-First Traversal Iterative traversals using stacks and queues Bounded depth-first search and iterative deepening Best-first search Chapter 16.2

Depth-first Traversal, recursively e.g., list all employee names Use recursion (and iteration to step through the children) public void listAll() { System.out.println(this.name); // Visit the root for (OrgTreeNode child : this.children) child.listAll(); // Visit the children } Can also be done using recursion.

Managing Depth-First Traversal Need to remember what nodes are still being worked on, and which of their children have been processed already In a recursive depth-first traversal (eg. "ListAll" from earlier), activation stacks and loop control take care of the bookkeeping! When doesn’t this work? A B D E C H G M I K F L J A print own name process children... return B print own name process children... return D print own name process children... return …

Breadth-first Traversal What about printing the nodes by level? root first then second level, then third level, …. Or find the shallowest node with a given value. A B Julie Jenny D E F C Jenny Jude Jiles I…. A B D E C H G M I K F L J

Tracking State during Breadth-First Traversal What nodes do we have to remember? Which order do we work on them? A B D E C H G M I K F L J

Breadth-first Traversal: Use a Queue! public void BreadthFirstTraversal(TreeNode root) { Queue<TreeNode> todo = new LinkedList<TreeNode>(); todo.offer(root); // add while (!todo.isEmpty()) { TreeNode node = todo.poll(); // remove System.out.println(node.name()); for (TreeNode child : node.getChildren()) todo.offer(child); } Look Ma’, no recursion! A B D E C H G M I K F L J

Tracking State during Depth-First Traversal What nodes do we have to remember? Which order do we work on them? A B D E C H G M I K F L J

Depth-First Traversal: Use a Stack! public void DepthFirstTraversal(TreeNode root) { Stack <TreeNode> todo = new Stack<TreeNode>(); todo.push(root); // add while (!todo.isEmpty()) { TreeNode node = todo.pop(); // remove System.out.println(node.name()); for (TreeNode child : node.getChildren()) todo.push(child); } Which traversal order does this implement? Look Ma’, no recursion! A B D E C H G M I K F L J

Iterator for binary (search) trees Iterator keeps a stack of nodes on current path Stop when stack is empty To get next node: (outline!) If current node is a leaf, delete it Else push its children onto the stack (NOTE: This is just a brief note added as a hint because it’s in the assignment, not intended as a detailed explanation!)

In-order Traversal, iteratively? H B A C F I E G

Depth-First vs Breadth-First Visit one child and all of its descendants before its siblings May visit root before, after, or between its children + Requires only one branch to manage traversal! − May find “costly” results first, and may not find results at all (infinite search trees) Breadth-First (level-order): Visit nodes in order of increasing depth May choose nodes in a level in a specific order − Requires accumulation of levels to manage traversal + Finds minimal solutions and guarantees success!

Cost of tree traversals For an n-ary tree of height h: Depth-first traversal stores up to h nodes Just on branch from root to current node Breadth- first traversal stores up to nh nodes! All nodes on all levels to current node Impractical for large n. Can we do better?

Depth-first search public TreeNode<E> find(E item) { } Like depth-first traversal, except we stop on finding a node with the value we’re looking for Or more generally with a node satisfying a given search criterion – see discussion of comparators This version returns the node found, and null if not found public TreeNode<E> find(E item) { if (this.value == item ) return this; for (TreeNode<E> child : this.children) TreeNode<E> r = child.find(item); if ( r ) return r; return null; }

Bounded depth-first search Limit the depth to which a dfs will go Pass max depth as an extra argument decrease on each recursive call exit when it reaches zero public TreeNode<E> find(E item, int n) { if ( n == 0 ) return null; if (this.value == item ) return this; for (TreeNode<E> child : this.children) TreeNode<E> r = child.find(item, n-1); if ( r ) return r; return null; }

Iterative deepening for ( int d = 1; d++; d <= max ) } Use bounded dfs with bigger and bigger bound Guaranteed to find shortest path Still only needs to store h nodes for current path Number of nodes at next level is equal to number of nodes at all earlier levels, so cost of revisiting them is not significant! public TreeNode<E> find(E item) { for ( int d = 1; d++; d <= max ) TreeNode<E> r = child.find(item, d); if ( r ) return r; return null; }

Best-first search Suppose we want to find the cheapest path to a node with a given value, where we have costs associated with edges, and cost of a path is the sum of the edge costs. At each step, we want to explore the node with the least cost so far, rather than the newest/oldest as in dfs/bfs. How can we modify our search algorithm to achieve this? Can we use a suitable data structure to make this efficient?