1 / 11

Tree Traversals & Maps

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

julian-best
Download Presentation

Tree Traversals & Maps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

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

  4. 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 } }

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

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

  7. Set Data Structures • Types • Set • Map • Hash Table h(n) Set Map Hash Table

  8. 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

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

  10. 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

  11. Maps • Complexity (average case) Put Get Remove 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 tableO(1) O(1) O(1)

More Related