Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

Trees Types and Operations
Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
CS 261 – Recitation 9 & 10 Graphs & Final review
CS 171: Introduction to Computer Science II
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.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Trees, Tre-Like Structures, Binary Search Trees, Balanced Trees, Tree Traversals, DFS and BFS Svetlin Nakov Telerik Software Academy academy.telerik.com.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Lecture 17 Non-Linear data structures Richard Gesick.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Tree Data Structures.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
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.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
Chapter 12 Abstract Data Type.
COMP 53 – Week Fourteen Trees.
Tree.
Section 8.1 Trees.
Tree Traversals – reminder
עצים root הגדרת עץ חופשי: גרף לא מכוון ללא מעגלים
Tree Traversals – reminder
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Lecture No.20 Data Structures Dr. Sohail Aslam
Section 9.3 by Andrew Watkins
Chapter 20: Binary Trees.
AVL Tree Chapter 6 (cont’).
Binary Tree Traversal Methods
Non-Linear data structures
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order: left, right, root General graph traversals (searches) Depth-first search Breadth-first search

Inorder(tree t) 1. if t = nil 2. return 3. inorder(t.left) 4. visit(t) // e.g., print it 5. inorder(t.right)

Inorder (Infix) (a BST will always work well with in-order traversal)

Pre-order (Prefix)

Post-order (Postfix)

Post-order 1 3 * *13-610

Depth-first search (DFS) חיפוש לעומק)) DFS: Search one subtree completely before other Pre-order traversal is an example of a DFS: Visit root, left subtree (all the way), visit right subtree We can do it in other order: Visit root, right subtree, left subtree

Depth-first search (DFS) DFS: visit all descendents before siblings

DFS(tree t) 1. q  new stack 2. push(q, t) 3. while (not empty(q)) 4. curr  pop(q) 5. visit curr // e.g., print curr.datum 6. push(q, curr.left) 7. push(q, curr.right) This version for binary trees only!

Breadth-first search (BFS) (חיפוש לרוחב) BFS: visit all siblings before their descendents

BFS(tree t) 1. q  new queue 2. enqueue(q, t) 3. while (not empty(q)) 4. curr  dequeue(q) 5. visit curr // e.g., print curr.datum 6. enqueue(q, curr.left) 7. enqueue(q, curr.right) This version for binary trees only!

DFS(tree t) 1. q  new stack 2. push(q, t) 3. while (not empty(q)) 4. curr  pop(q) 5. visit curr // e.g., print curr.datum 6. push(q, curr.left) 7. push(q, curr.right) This version for binary trees only!

DFS(tree t) Void Graph::dfs (Vertex v) { v.visted = true; for each w adjacent to v if (!w.visited) dfs(w); } This version for any type of trees (graph)

Graphs vs. Trees  Graphs don’t have any root  Graphs can be directed or undirected  Trees only grow down (upside-down)  (Why do trees grow upside down for Computer scientists???)  Graphs can have cycles, trees can’t (why?)

DFS Example on Graph source vertex

AVL (Adelson, Velskii, Landis)  Balance the tree (not our targil)  The left and right branches of the tree can only differ by one level  Ensures log N depth (much better for searching)  Takes log N to add or delete

AVL Tree Not AVL Tree

AVL Trees  Trees often need to be “rotated” when deleting or inserting to keep AVL balance  Nice link on this process Nice link on this process  Sample AVL code Sample AVL code