예제 #1
0
        //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);
        }
예제 #2
0
        //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;
                }
            }
        }
예제 #3
0
 //Post Order traversal call private helper member function
 public void TraversePostOrder(TreeVisitFunctionDelegate visitFunction)
 {
     VisitPostOrder(ref root, visitFunction);
 }
예제 #4
0
 //Level Order traversal call private helper member function
 public void TraverseLevelOrder(TreeVisitFunctionDelegate visitFunction)
 {
     VisitLevelOrder(ref root, visitFunction);
 }
예제 #5
0
 //In Order traversal call private helper member function
 public void TraverseInOrder(TreeVisitFunctionDelegate visitFunction) //VisitFunction f)
 {
     VisitInOrder(ref root, visitFunction);                           //, f);
 }