{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

10b - TreesSlides

# We visit nodes according to some rule the way we want

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

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

{[ snackBarMessage ]}

Ask a homework question - tutors are online