5-Binary Tree Traversals

# 5-Binary Tree Traversals - Binary Tree Traversals Review...

• Notes
• 5

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

Binary Tree Traversals Review recursive traversals Discuss in relation to Preorder Traversal just after P has been visited. The idea is to keep on a stack, pointers to all the nodes on the path from the predecessor of node p (which is about to be visited) to the root. PreorderTraversal p=t while ((p is not null)||(s isn’t empty)) if (p is not null) visit(p) push(p, s) if (left(p) is not null) p=left(p) else p=right(p) else do { q=pop(s) if (s isn’t empty) rtptr=right(item(s)) else rtptr=null } while (s isn’t empty and q is rtptr) p=rtptr

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

InorderTraversal p=t while ((p is not null)||(s isn’t empty)) if (p is not null) push(p, s) if (left(p) is not null) p=left(p) else {visit(p) p=right(p)} else do { q=pop(s) if (s isn’t empty) rtptr=right(item(s)) else rtptr=null } while (s isn’t null and q is rtptr) if (s isn’t empty) visit(item(s)) p=rtptr PostorderTraversal p=t while ((p is not null)||(s isn’t empty)) if (p is not null) push(p, s) if (left(p) is not null) p=left(p) else p=right(p) else do { q=pop(s) visit(q) if (s isn’t empty) rtptr=right(item(s)) else rtptr=null } while (s isn’t empty and q is rtptr) p=rtptr Application of the Traversals.
This is the end of the preview. Sign up to access the rest of the document.
• Fall '16
• James Korsh
• Graph Theory, Pointer, Tree traversal, Traversals

{[ 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