Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
Chapter 10: Data Structures II
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Department of Computer Science University of Maryland, College Park
Midterm 2 Overview Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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,
Maps & Hashing Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
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.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Emma Price 1.  To be able to:  Explain what a binary tree is.  To traverse a binary tree using the three different methods. 2.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Trees Dr. Andrew Wallace PhD BEng(hons) EurIng
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
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.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Review for Final Exam – cs411/511 Definitions (5 questions, 2 points each) Algorithm Analysis (3 questions, 3 points each) General Questions (3 questions,
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.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
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.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Trees Chapter 15.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Binary search tree. Removing a node
Week 6 - Wednesday CS221.
Tree.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Abstract Data Structures
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Non-Linear data structures
Binary Tree Implementation And Applications
Binary Trees, Binary Search Trees
Presentation transcript:

Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

Tree Traversal Goal Visit every node in binary tree Approaches Depth first Preorder  parent before children Inorder  left child, parent, right child Postorder  children before parent Breadth first  closer nodes first

Tree Traversal Methods Pre-order 1. Visit node// first 2. Recursively visit left subtree 3. Recursively visit right subtree In-order 1. Recursively visit left subtree 2. Visit node// second 3. Recursively right subtree Post-order 1. Recursively visit left subtree 2. Recursively visit right subtree 3. Visit node// last

Tree Traversal Methods Breadth-first BFS(Node n) { Queue Q = new Queue(); Q.enqueue(n);// insert node into Q while ( !Q.empty()) { n = Q.dequeue();// remove next node if ( !n.isEmpty()) { visit(n); // visit node Q.enqueue(n.Left());// insert left subtree in Q Q.enqueue(n.Right());// insert right subtree in Q }

Tree Traversal Examples Pre-order (prefix) +  2 3 / 8 4 In-order (infix) 2  / 4 Post-order (postfix) 2 3  8 4 / + Breadth-first +  /  / 2384 Expression tree

Tree Traversal Examples Pre-order 44, 17, 32, 78, 50, 48, 62, 88 In-order 17, 32, 44, 48, 50, 62, 78, Binary search tree Sorted order! Post-order 32, 17, 48, 62, 50, 88, 78, 44 Breadth-first 44, 17, 78, 32, 50, 88, 48, 62

Set Data Structures Types Set Map Hash Table SetMapHash Table h(n)

Maps Map (associative array) Unordered collection of objects (values) One or more keys associated with each object Can use key to retrieve object Example A[“key1”] = … key1 key2 key3 key4

Maps Methods Void Put( Key, Object )// inserts element Object Get( Key )// returns element Void Remove( Key )// removes element

Maps Implementation approaches Two parallel arrays Unsorted Sorted Linked list Binary search tree Hash table Java Collections Framework TreeMap  uses red-black (balanced) tree HashMap  uses hash table

Maps Complexity (average case) PutGetRemove Unsorted array O(1) O(n) O(n) Sorted array O(n) O(log(n)) O(n) Linked list O(1)O(n)O(n) Binary search tree O(log(n)) O(log(n)) O(log(n)) Hash table O(1) O(1) O(1)