Cse 373 April 14th – TreEs pt 2.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees CS 110: Data Structures and Algorithms First Semester,
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
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
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
Trees – Part 2 CS 367 – Introduction to Data Structures.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
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.
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.
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 Saurav Karmakar
CSE373: Data Structures & Algorithms
COMP 53 – Week Fourteen Trees.
Chapter 25 Binary Search Trees
April 19th – Avl Operations
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
Problems with Linked List (as we’ve seen so far…)
Cse 373 May 15th – Iterators.
Trees Lecture 12 CS2110 – Fall 2017.
Programming Abstractions
Trees Tree nomenclature Implementation strategies Traversals
Week 11 - Friday CS221.
Source: Muangsin / Weiss
March 31 – Priority Queues and the heap
Cse 373 April 12th – TreEs.
COMP 103 Binary Search Trees.
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Trees.
Data Structures and Database Applications Binary Trees in C#
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Trees Lecture 12 CS2110 – Spring 2018.
7/23/2009 Many thanks to David Sun for some of the included slides!
Trees 7/14/2009.
Lesson Objectives Aims
B-Tree Insertions, Intro to Heaps
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
ITEC 2620M Introduction to Data Structures
Trees CMSC 202, Version 5/02.
A Robust Data Structure
Lecture 12 CS203 1.
Binary Tree Traversals
CMSC 202 Trees.
Priority Queues & Heaps
Topic 5: Heap data structure heap sort Priority queue
CSE 12 – Basic Data Structures
CSC 143 Java Trees.
CS 367 – Introduction to Data Structures
Tree.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Lecture 9: Intro to Trees
Non-Linear data structures
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Binary Search Tree.
CS2005 Week 8 Lectures Maps & Binary Trees.
Presentation transcript:

Cse 373 April 14th – TreEs pt 2

Assorted minutiae HW3 Out last night No need to submit testing code System.currentTimeMillis() HW1 wrong submissions Grades posted by Sunday – Canvas announcement Regrade requests https://catalyst.uw.edu/webq/survey/ejmcc/330190

Today’s lecture Tree traversals Depth first search Breadth first search Tree properties Balance

Tree traversals What is the point of a traversal? Some way to get through elements of the tree Useful for more than just trees

Tree traversals Array implementations Traversal is easy, search left-to-right Traversal is complete, no element is missed Doesn’t take advantage of heap property

Tree traversals Node implementations Not as easy No inherent ordering Two main approaches: Depth first search Breadth first search

Depth First Search All tree traversals start at the root As the name implies, traverse down the tree first. Left or right does not explicitly matter, but left usually comes first.

Depth first search How do we search this tree?

Depth first search Left node first

Depth first search Keep going down the left nodes

Depth first search Until you reach the bottom

Depth first search What next?

Depth first search Need some way to indicate that you are completely searched (tell the parent)

Depth first search Parent now knows it is can search the other child

Depth first search Leaves are searched when their data is observed

Depth first search Now that both of its children have been completely searched

Depth first search It needs to indicate that to its parent

Depth first search That parent then knows to search its right child

Depth first search That parent then knows to search its right child

Depth first search This process repeats

Depth first search This process repeats

Depth first search This process repeats

Depth first search This process repeats

Depth first search This process repeats

Depth first search This process repeats

Depth first search This process repeats

Depth first search Now the left tree is completely searched and we can search the right

Depth first search On the new subtree, we begin search from the left

Depth first search On the new subtree, we begin search from the left

Depth first search And we find the object we’re looking for

Depth First Search How does this work in application? For each node, it searches its left subtree entirely and then moves to the right tree Here search works by breaking the problem down into sub-problems This is a good indication that we use recursion

Recursion Recap from 143 What is recursion? A problem that calls itself (with a smaller version of the input) Example: Linked list search Take a few minutes and discuss a recursive approach (sorted or unsorted)

Recursion public boolean LLsearch(int toFind){ return LLsearch(toFind,first) } private boolean LLsearch(int toFind,Node curr){ if(curr == null) return false; if(curr.data == toFind) return true; return LLsearch(toFind,curr.next);

Recursion How do we apply this approach to DFS? Discuss among yourselves Consider what is the “subproblem” What does it look like?

Recursion public boolean DFSearch(int toFind){ return DFSearch(toFind,root) } private boolean DFSearch(int toFind,Node curr){ if(curr == null) return false; if(curr.data == toFind) return true; if(DFSearch(toFind,curr.left)) return true; if(DFSearch(toFind,curr.right)) return true; return false;

Depth First Search Treat each subtree as a subproblem and solve recursively. Will go to maximum depth first. When the node is found, the result will return up the stack What might be a different approach?

Alternate approach How else to traverse?

Alternate approach Search the tree from top to bottom

Breadth First Search If we want to traverse the tree from top to bottom, how might we go about doing this? Discuss among yourselves for a minute Can this approach be reduced to a subproblem? Not easily!

Breadth First Search Consider the approach Start with the root Search all nodes of depth 1 Search all nodes of depth 2 … How do we get this ordering?

Breadth First Search What if we use a Queue? Enqueue the root Then what?

Breadth first search A enqueue(A) B C D E F G H I J K L M N O Queue:

Breadth First Search What if we use a Queue? enqueue the root while the queue has elements: dequeue the node if it matches our search string return true if it doesn’t, enqueue its non-null children return false;

Breadth first search A dequeue and check the node B C D E F G H I J K L M N O Queue:

Breadth first search A enqueue the children B C D E F G H I J K L M N O Queue: B | C |

Breadth first search A repeat B C D E F G H I J K L M N O Queue:

Breadth first search A B C D E F G H I J K L M N O Queue: C | D | E |

Breadth first search A B C D E F G H I J K L M N O Queue:

Breadth first search A B C D E F G H I J K L M N O Queue:

Breadth first search A B C D E F G H I J K L M N O Queue:

Breadth first search A B C D E F G H I J K L M N O Queue:

Breadth first search A B C D E F G H I J K L M N O Queue:

Breadth first search A B C D E F G H I J K L M N O Queue:

Breadth first search A B C D E F G H I J K L M N O Queue:

Breadth first search A B C D E F G H I J K L M N O Queue:

Breadth first search A B C D E F G H I J K L M N O Queue:

Breadth first search A And now we’ve found it! B C D E F G H I J K L M Queue: L | M | N | O

Breadth First Search Use a queue to keep track of the order What happens if we use a stack? Depth first search! These things are related! Next week Balance and the O(n) problem