We visit nodes according to some rule the way we want

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: nodes are visited in the order we have specified. How does this work? A recursive process. y v s x Inorder traversal is x r just calls This is so that the physical order of the data does not data does not confuse. confuse. z t The function does assume that there are no duplicates. This is called inorder traversal. inorder Well, trees sort as well. w This function works even if the tree is empty. w x, t, w, r, y, v, s, z z • visit all nodes in left sub-tree sub- y • visit the node v • visit all nodes in right sub-tree sub- s r t This applies to any sub-tree. sub- 7 void InorderTraverse(nodePtr curr) { if (curr != 0) { InorderTraverse(currInorderTraverse(curr->left); Visit(curr); InorderTraverse(currInorderTraverse(curr->right); } } void InorderTraverse(nodePtr curr) { if (curr != 0) { InorderTraverse(currInorderTraverse(curr->left); Visit(curr); InorderTraverse(currInorderTraverse(curr->right); } } A recursive function, started by calling InorderTraverse(root); A recursive function, started by calling InorderTraverse(root); Another function – just add the required prefixes. 5 Two other ways of accessing the nodes. preorder and postorder postorder 0 Pr...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online