Understanding Tree Traversal Techniques

treetraversal n.w
1 / 11
Embed
Share

Explore the concepts of tree traversal, including different traversal orders in binary trees and the importance of visiting each node precisely once. Learn how to implement pre-order traversal for copying and navigating through tree structures effectively.

  • Tree Traversal
  • Binary Trees
  • Preorder
  • Algorithm
  • Data Structures

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. TreeTraversal

  2. Traversals of Binary Trees Arrays: iterate through an array by increasing index. Linked lists traverse by following next addresses in each node. Trees? How do you make sure you hit every node once and only once? tree traversal walking through tree and visiting all nodes once 2

  3. class NodeT { int data; NodeT *left; //left child NodeT *right; // right child NodeT *parent; //NULL for root of tree //optional: int height; // height up from lowest descendent leaf public: NodeT(int x); ~NodeT(); void printNodeT(); }; Node Class Definition for a Binary Tree:

  4. Traversals of Binary Trees tree traversal Three kinds of binary tree traversal: Preorder e.g., copying Inorder e.g., bst (more later) Postorder e.g., deleting or freeing nodes order in which we visit the subtree root with respect to its children Why do we worry about traversing in different orders? Trees store data we may want to find or represent data in different ways depending on the data and the solution we are looking for 4 NOTE: WE ALWAYS START AT THE ROOT!

  5. Tree Traversal: Preorder Used for copying: 1. Visit root, 2. traverse left, 3. traverse right You want to make a copy of the parent before you can make a copy of either of its children. So you can connect everything! Preorder: a, b, d, g, e, h, c, f, i, j <- left right ->

  6. 1. Visit root, 2. traverse left, 3. traverse right Tree Traversal: Preorder I wrote it all out Preorder: a, b, d, g, e, h, c, f, i, j NOTE: As soon as you get to a child, it immediately becomes the new parent (of the rest of the subtree) 1. So A is parent (and root, and thus starting node). Visit a, go to left child. 2. Now b is the parent (of the subtree). Visit b, go to its left child d. 3. Now d is the parent it is visited. 4. Go to left (NULL) 5. Go to right child g, it becomes the parent 6. G has no left child, has no right child, d has already been visited 7. Go to b s right child, e, which becomes the parent it is visited. 8. E s left child is h, becomes the parent, is visited. 9. H has no left, no right. 10. Go to e s right, nothing. 11. Now the b subtree has been visited, so go to A s right child (c). It becomes the parent and is visited. 12. Go to C s left f becomes the parent and is visited. 13. Go to F s left I is the parent and is visited 14. I has no left, no right, 15. Go to F s right 16. J becomes the parent and is visited. 17. Try going to c s right. 18. We re done! <- left right ->

  7. Tree Traversals: InOrder Used with BST: (again, it s coming!) 1. Traverse left (go till no more lefts), 2. visit root, 3. traverse right (always go to the left if there s a left) Again, as soon as you visit a node, it becomes the parent (so, in this case, you don t visit it until there are no more left children!) InOrder (left, center, right) d, g, b, h, e, a, i, f, j, c 7 <- left right ->

  8. Tree Traversal: Postorder Used for deleting: 1. Traverse left, 2. traverse right, 3. visit root Again, as soon as you visit a node, it becomes the parent (so, in this case, you don t visit it until there are no more left or right children!) Postorder: (left, right, parent) g, d, h, e, b,i, j, f, c, a 8 <- left right ->

  9. Pre? In? Post? 36 16 48 15 21 40 11 23 44 41 PRE: 36 16 15 11 21 23 48 40 44 41 IN: 11 15 16 21 23 36 40 41 44 48 POST: 11 15 23 21 16 41 44 40 48 36 <- left right ->

  10. Given this code, what is printed out? void BST::printTreeio(NodeT *n) { //recursive!!!! function if (n == NULL) { return; } else { printTreeio(n->left); n->printNode(); printTreeio(n->right); } } 36 16 48 15 21 40 Aside: yeah, you could write this using while loops. 11 23 44 Feel free to try! I m not going there!!! 41 <- left right ->

  11. Takeaways: Tree traversal: visiting each node once and only once! Start traversal AT THE ROOT! 3 ways to traverse: Each serves a different purpose Preorder: for copying a tree Visit the parent, then left child, then right child InOrder: Visit left child, then parent, then right child PostOrder: for deleting a tree Visit left child, then right child, then parent

Related


More Related Content