コード例 #1
0
        /// <summary>
        /// Performs the action on all nodes of the subtree in a pre-order, right-to-left fashion.
        /// </summary>
        private static void PreOrderRightToLeftVisitor <TKey, TValue>(BinarySearchTreeMapNode <TKey, TValue> root, Action <BinarySearchTreeMapNode <TKey, TValue> > action)
        {
            if (root == null)
            {
                return;
            }

            action(root);
            PreOrderRightToLeftVisitor(root.Right, action);
            PreOrderRightToLeftVisitor(root.Left, action);
        }
コード例 #2
0
        /// <summary>
        /// Recursively visits all nodes of the given subtree in the specified order and applies the given action on them. Level-order traversal is iterative.
        /// </summary>
        /// <typeparam name="TKey">The data type of the key of the <see cref="BinarySearchTreeMapNode{TKey, TValue}"/>.</typeparam>
        /// <typeparam name="TValue">The data type of the value of the <see cref="BinarySearchTreeMapNode{TKey, TValue}"/>.</typeparam>
        /// <param name="root">The root of the subtree for traversal.</param>
        /// <param name="action">The action to perform on each node.</param>
        /// <param name="traversalMode">The way in which the subtree is traversed.</param>
        public static void ForEachRecursive <TKey, TValue>(this BinarySearchTreeMapNode <TKey, TValue> root, Action <BinarySearchTreeMapNode <TKey, TValue> > action, TraversalMode traversalMode = TraversalMode.InOrder)
        {
            if (root == null)
            {
                throw new ArgumentNullException("root");
            }
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            switch (traversalMode)
            {
            case TraversalMode.InOrder:
                InOrderVisitor(root, action);
                break;

            case TraversalMode.InOrderRightToLeft:
                InOrderRightToLeftVisitor(root, action);
                break;

            case TraversalMode.PreOrder:
                PreOrderVisitor(root, action);
                break;

            case TraversalMode.PreOrderRightToLeft:
                PreOrderRightToLeftVisitor(root, action);
                break;

            case TraversalMode.PostOrder:
                PostOrderVisitor(root, action);
                break;

            case TraversalMode.PostOrderRightToLeft:
                PostOrderRightToLeftVisitor(root, action);
                break;

            case TraversalMode.LevelOrder:
                LevelOrderVisitor(root, action);
                break;

            case TraversalMode.LevelOrderRightToLeft:
                LevelOrderRightToLeftVisitor(root, action);
                break;

            default:
                InOrderVisitor(root, action);
                break;
            }
        }
コード例 #3
0
        /// <summary>
        /// Performs the action on all nodes of the subtree in a level-order, right-to-left fashion.
        /// </summary>
        private static void LevelOrderRightToLeftVisitor <TKey, TValue>(BinarySearchTreeMapNode <TKey, TValue> root, Action <BinarySearchTreeMapNode <TKey, TValue> > action)
        {
            if (root == null)
            {
                return;
            }

            action(root);

            var queue = new Queue <BinarySearchTreeMapNode <TKey, TValue> >();

            if (root.Right != null)
            {
                action(root.Right);
                queue.Enqueue(root.Right);
            }
            if (root.Left != null)
            {
                action(root.Left);
                queue.Enqueue(root.Left);
            }


            while (queue.Count > 0)
            {
                var curNode = queue.Dequeue();

                if (curNode.Right != null)
                {
                    action(curNode.Right);
                    queue.Enqueue(curNode.Right);
                }
                if (curNode.Left != null)
                {
                    action(curNode.Left);
                    queue.Enqueue(curNode.Left);
                }
            }
        }