lecture-14

Visitsubtreeroot item traversesubtreeroot right cpsc

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: traverse left and right E 4 H F void traverse(const Node* subtreeRoot) { 8 7 if(subTreeRoot == NULL) return; What order will the visit(subTreeRoot->item); nodes be visited? traverse(subtreeRoot->leftChild); traverse(subtreeRoot->rightChild); } CPSC 223  ­ ­ Fall 2010 Traversing binary trees When should we “visit” a node? •  In an “inorder” traversal –  Traverse left –  Visit the node –  Then traverse right 25 OUTPUT: A B C D E F G H 4 2 D B G 7 5 A C 1 3 E H F 8 6 void traverse(const Node* subtreeRoot) { if(subTreeRoot == NULL) What order will the return; traverse(subtreeRoot->leftChild); nodes be visited? visit(subTreeRoot->item); traverse(subtreeRoot->right); } CPSC 223  ­ ­ Fall 2010 26 13 10/19/10 Traversing binary trees OUTPUT: A C B F E H G D 8 When should we “visit” a node? •  In a “postorder” traversal 3 D B G –  Traverse left and right 5 A C 1 –  Then visit the node 7 2 void traverse(const Node* subtreeRoot) { E H F 6 4 if(subTreeRoot == NULL) return; What order will the traverse(subtreeRoot->leftChild); nodes be visited? traverse(subtreeRoot->rightChild); visit(subTreeRoot->item); } CPSC 223  ­ ­ Fall 2010 27 Homework 6: Pre, Post, and In Order Print These methods should each … –  Use the appropriate traversal (pre, post, inorder) –  Visit t...
View Full Document

Ask a homework question - tutors are online