//Traverse post order and use functor private void VisitPostOrder(ref TreeNode <K, T> node, TreeVisitFunctionDelegate visitFunction) { //If empty tree return NULL if (node == null) { return; } //Visit left VisitPostOrder(ref node.LeftNode, visitFunction); //Visit right VisitPostOrder(ref node.RightNode, visitFunction); //Return visitFunction(ref node); }
//Traverse level order and use functor private void VisitLevelOrder(ref TreeNode <K, T> node, TreeVisitFunctionDelegate visitFunction) { //Declare queue of nodes //Purpose: store node sequence Queue <TreeNode <K, T> > nodeQueue = new Queue <TreeNode <K, T> >(); //Declare current node TreeNode <K, T> currentNode = node; //Iterate until NULL while (currentNode != null) { //Call functor visitFunction(ref currentNode); //Visit left branch if (currentNode.LeftNode != null) { //Enqueue node nodeQueue.Enqueue(currentNode.LeftNode); } //Visit right branch if (currentNode.RightNode != null) { //Enqueue node nodeQueue.Enqueue(currentNode.RightNode); } //If queue is empty if (nodeQueue.Count != 0) { //Get head of queue currentNode = nodeQueue.Peek(); //Dequeue nodeQueue.Dequeue(); } else { //If end of queue set current node to null currentNode = null; } } }
//Post Order traversal call private helper member function public void TraversePostOrder(TreeVisitFunctionDelegate visitFunction) { VisitPostOrder(ref root, visitFunction); }
//Level Order traversal call private helper member function public void TraverseLevelOrder(TreeVisitFunctionDelegate visitFunction) { VisitLevelOrder(ref root, visitFunction); }
//In Order traversal call private helper member function public void TraverseInOrder(TreeVisitFunctionDelegate visitFunction) //VisitFunction f) { VisitInOrder(ref root, visitFunction); //, f); }