ch5-2-tree-traversal-method

# ch5-2-tree-traversal-method - Binary Tree Traversal Methods...

This preview shows pages 1–12. Sign up to view the full content.

Binary Tree Traversal Methods In a traversal of a binary tree, each element of the binary tree is visited exactly once. During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Binary Tree Traversal Methods Preorder Inorder Postorder Level order
Preorder Traversal void preOrder(treePointer ptr) { if (ptr != NULL ) { visit(t); preOrder(ptr->leftChild); preOrder(ptr->rightChild); } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Preorder Example (Visit = print) a b c a b c
Preorder Example (Visit = print) a b c d e f g h i j a b d g h e i c f j

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Preorder Of Expression Tree + a b - c d + e f * / Gives prefix form of expression! / * + a b - c d + e f
Inorder Traversal void inOrder(treePointer ptr) { if (ptr != NULL ) { inOrder(ptr->leftChild); visit(ptr); inOrder(ptr->rightChild); } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Inorder Example (Visit = print) a b c b a c
Inorder Example (Visit = print) a b c d e f g h i j g d h b e i a f j c

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Inorder By Projection (Squishing) a b c d e f g h i j g d h b e i a f j c
Inorder Of Expression Tree + a b - c d + e f * / Gives infix form of expression (sans parentheses)!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern