Trees – Part 2 CS 367 – Introduction to Data Structures.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Binary Trees CS 110: Data Structures and Algorithms First Semester,
Advanced Data Structures
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
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.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
BST Data Structure A BST node contains: A BST contains
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Binary Trees – Part I CS 367 – Introduction to Data Structures.
Binary Trees. Node structure Data A data field and two pointers, left and right.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Game Playing. Introduction Why is game playing so interesting from an AI point of view? –Game Playing is harder then common searching The search space.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS 61B Data Structures and Programming Methodology July 15, 2008 David Sun.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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
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.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
Graphs – Part II CS 367 – Introduction to Data Structures.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Binary Search Trees (BST)
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Adversarial Search. Regular Tic Tac Toe Play a few games. –What is the expected outcome –What kinds of moves “guarantee” that?
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.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Trees Saurav Karmakar
Chapter 12 – Data Structures
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Trees ---- Soujanya.
Problems with Linked List (as we’ve seen so far…)
Section 8.1 Trees.
Trees.
Cse 373 April 14th – TreEs pt 2.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Binary Tree Traversal Methods
Lesson Objectives Aims
Binary Tree Traversal Methods
Abstract Data Structures
Depth-First Searches Introduction to AI.
Binary Tree Traversal Methods
2018, Fall Pusan National University Ki-Joune Li
CSC 143 Java Trees.
CS 367 – Introduction to Data Structures
Binary Trees.
Trees.
Binary Tree Traversal.
Binary Search Trees CS 580U Fall 17.
Binary Search Tree.
Presentation transcript:

Trees – Part 2 CS 367 – Introduction to Data Structures

Tree Traversal Sometimes necessary to scan entire tree –imagine a system that has no keys, just states AI algorithms are a great example –to find the best state, must search all nodes Two ways to search an entire tree –breadth first search all nodes at one level, and then go to next level –depth first go all the way down one branch and then the next and so on

Breadth First Search state 0 state 1 state 2 state 3state 4 state 5state 6 state 7state 8 state 9 Search Order state 0 state 1 state 2 state 3 state 4 state 5 state 6 state 7 state 8 state 9

Breadth First Search Best way to implement a breadth first search is with a queue –visit a node –enqueue all of the nodes children –dequeue the next item from the queue –repeat until the queue is empty

Breadth First Search state 0 state 1 state 2 state 3state 4 state 5state 6 Initial Queue s0 pop s0 push children s1s2 pop s1 push children s2s3s4 pop s2 push children s3s4s5s6 pop s23 no children s4s5s6 etc. …

Breadth First Search public void printTree-Breadth() { if(root == null) { System.out.println(“Empty tree.”); return; } Queue queue = new QueueList(); queue.enqueue(root); while(!queue.isEmpty()) { TreeNode tmp = queue.dequeue(); System.out.println(tmp.data.toString()); if(tmp.left != null) { queue.enqueue(tmp.left); } if(tmp.right != null) { queue.enqueue(tmp.right); } }

Breadth First Search Advantage –guaranteed to find the wanted state if it exists Disadvantage –excessive memory requirements –M needed = 2 level - 1

Depth First Search Three possible orderings for depth first –preorder visit node, then its left child, then its right child –inorder visit left child, then the node, then right child –postorder visit left child, then right child, then the node All depth first searches are easy to implement with recursion

Depth First - Preorder state 0 state 1 state 2 state 3state 4 state 5state 6 state 7state 8 state 9 Search Order state 0 state 1 state 3 state 7 state 8 state 4 state 2 state 5 state 6 state 7

Depth First - Preorder public void printTree-Preorder() { if(root == null) { System.out.println(“Empty tree”); } else { printTree-Preorder(root); } } private void printTree-Preorder(Node node) { if(node == null) return; System.out.println(node.data.toString()); printTree-Preorder(node.left); printTree-Preorder(node.right); }

Depth First - Inorder state 0 state 1 state 2 state 3state 4 state 5state 6 state 7state 8 state 9 Search Order state 7 state 3 state 8 state 1 state 4 state 0 state 5 state 2 state 9 state 6

Depth First - Inorder public void printTree-Inorder() { if(root == null) { System.out.println(“Empty tree”); } else { printTree-Inorder(root); } } private void printTree-Inorder(Node node) { if(node == null) return; printTree-Inorder(node.left); System.out.println(node.data.toString()); printTree-Inorder(node.right); }

Depth First – Postorder state 0 state 1 state 2 state 3state 4 state 5state 6 state 7state 8 state 9 Search Order state 7 state 8 state 3 state 4 state 1 state 5 state 9 state 6 state 2 state 0

Depth First - Postorder public void printTree-Postorder() { if(root == null) { System.out.println(“Empty tree”); } else { printTree-Postorder(root); } } private void printTree-Postorder(Node node) { if(node == null) return; printTree-Postorder(node.left); printTree-Postorder(node.right); System.out.println(node.data.toString()); }

Depth First Search Advantages –requires much less memory than breadth first M needed = level Disadvantage –may never find the solution some search spaces have an infinite number of states (or very nearly infinite) this means a single “branch” is infinite we’ll never search other branches

Application of Tree Traversal We’ve already shown how tree traversal can be used to print out all of the nodes –this is good for debugging, but not needed for much else Consider a computer chess game –each node represents a state of the game where each piece is on the board –for the computer to decide it’s next move, it would like to pick a node that will lead to the best state

Chess Game We’ll say a node contains the following –the position of each piece on the board –a value indicating how favorable the current positions are high value means it’s good –check mating opponent would be the highest value a low value means it’s bad –being in check mate will be the lowest value –each node will have from 0 to 16 children why?

Chess Game There are millions of more states but they won’t all fit on the slide.

Chess Game Consider the first possible move –can move any one of 8 pawns or 2 knights that means we have 10 successor states –opponent will then have 10 possible moves –obviously, the number of states available at just the second level is immense the overall search space is virtually infinite Two possible solutions –use a breadth first search and only go to a certain level possibly monumental memory requirements –use a depth first search but only go so far along each branch limits the memory requirements