Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Inorder Traversal

Whereas inorder traversal of an N-ary tree is not defined for N>2, inorder traversal is defined for an M-way search tree: By definition, the inorder traversal of a search tree visits all the keys contained in the search tree in order.

Program gif is an implementation of the algorithm for depth-first traversal of an M-way search tree given in Section gif. The keys contained in a given node are visited (by calling the inVisit method of the visitor) in between the appropriate subtrees of that node. That is, key tex2html_wrap_inline63256 is visited in between subtrees tex2html_wrap_inline63254 and tex2html_wrap_inline62338.

   program20708
Program: MWayTree class depthFirstTraversal method.

In addition, the postVisit method is called on tex2html_wrap_inline64292 after subtree tex2html_wrap_inline63254 has been visited, and the preVisit method is called on tex2html_wrap_inline64296 before subtree tex2html_wrap_inline62338 is visited.

It is clear that the amount of work done at each node during the course of a depth-first traversal is proportional to the number of keys contained in that node. Therefore, the total running time for the depth-first traversal is tex2html_wrap_inline64300, where K is the number of keys contained in the search tree.


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.