From charlesreid1

No edit summary
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:


<pre>
<pre>
// public inorder_traversal method:
define inorder_traversal( tree ):
define inorder_traversal( tree ):
     inorder_traversal( tree, root, 0 )
     inorder_traversal( tree, root, 0 )
// protected/private recursive inorder_traversal method:
// (note, visit() function is the action being performed on the tree node)


define inorder_traversal( tree, position, depth ):
define inorder_traversal( tree, position, depth ):
     inorder_traversal( tree, position.left(), depth+1 );
     inorder_traversal( tree, position.left(), depth+1 );
     // perform visit action on position
     visit( position, depth )
     inorder_traversal(tree, position.right(), depth+1 );
     inorder_traversal(tree, position.right(), depth+1 );
</pre>
</pre>



Latest revision as of 15:54, 7 September 2017

Notes

The basis of in-order traversal is that the binary tree be sorted. This implements the convention of a sorted binary tree, a recursive definition:

The value of node k is greater than every node in the left subtree of node k. (Left goes first. Left to right precedence.)

The value of node k is less than every node in the right subtree of node k. (Right goes second. Left to right precedence.)


// public inorder_traversal method:

define inorder_traversal( tree ):
    inorder_traversal( tree, root, 0 )


// protected/private recursive inorder_traversal method:
// (note, visit() function is the action being performed on the tree node)

define inorder_traversal( tree, position, depth ):
    inorder_traversal( tree, position.left(), depth+1 );
    visit( position, depth )
    inorder_traversal(tree, position.right(), depth+1 );

Related Pages

Graphs:

Traversals on trees:

Breadth-first search and traversal on trees:

  • BFS - breadth first search
  • BFT - breadth first traversal

Depth-first search and traversal on trees:

  • DFS - depth first search
  • DFT - depth first traversal

OOP design patterns:

Category:Traversal

Flags