Exemplo n.º 1
0
        public IEnumerable <T> Traverse(TraverseOrder order)
        {
            if (_root == null)
            {
                throw new InvalidOperationException();
            }

            var traversal = default(IEnumerable <T>);

            switch (order)
            {
            case TraverseOrder.InOrder:
                traversal = _root.TraverseInOrder();
                break;

            case TraverseOrder.PostOrder:
                traversal = _root.TraversePostOrder();
                break;

            case TraverseOrder.PreOrder:
                traversal = _root.TraversePreOrder();
                break;

            case TraverseOrder.LevelOrder:
                traversal = _root.TraverseLevelOrder();
                break;
            }

            return(traversal);
        }
Exemplo n.º 2
0
        private void PrintRecursive(ADSNode node, TraverseOrder order)
        {
            if (node == null)
            {
                return;
            }

            // used to output a tree to be loaded again without rebalancing
            if (order == TraverseOrder.PreOrder)
            {
                Console.Write(node.Key + " ");
            }

            PrintRecursive(node.Left, order);

            // used to print a tree for readability
            if (order == TraverseOrder.InOrder)
            {
                Console.Write(node.Key + " ");
            }

            PrintRecursive(node.Right, order);

            // used to print a tree to be deleted efficeintly
            if (order == TraverseOrder.PostOrder)
            {
                Console.Write(node.Key + " ");
            }
        }
Exemplo n.º 3
0
Arquivo: Tree.cs Projeto: lzz42/ZHello
        /// <summary>
        /// 10.4.2
        /// n结点的二叉树,使用O(n)的递归过程,遍历所有的节点的关键字
        /// </summary>
        /// <typeparam name="N"></typeparam>
        /// <typeparam name="K"></typeparam>
        /// <param name="tree"></param>
        /// <param name="node"></param>
        public static void PrintBTree <K>(BTree <K> tree, TraverseOrder order)
            where K : IComparable
        {
            if (tree == null)
            {
                return;
            }
            switch (order)
            {
            case TraverseOrder.PreOrder:
                Trace.WriteLine(tree.Key);
                PrintBTree(tree.Left, order);
                PrintBTree(tree.Right, order);
                break;

            case TraverseOrder.InOrder:
                PrintBTree(tree.Left, order);
                Trace.WriteLine(tree.Key);
                PrintBTree(tree.Right, order);
                break;

            case TraverseOrder.PostOrder:
                PrintBTree(tree.Left, order);
                PrintBTree(tree.Right, order);
                Trace.WriteLine(tree.Key);
                break;

            case TraverseOrder.LevelOrder:
                break;

            default:
                break;
            }
        }
Exemplo n.º 4
0
        public static void PrintTree(BinaryTree<int> binaryTree, TraverseOrder traverseOrder)
        {
            binaryTree.TraversalOrder = traverseOrder;

            Console.WriteLine("{0} traversal.", traverseOrder.ToString());

            foreach (var node in binaryTree)
            {
                Console.Write("{0} ", node);
            }

            Console.WriteLine();
        }
Exemplo n.º 5
0
        public void Traverse(TraverseOrder order = TraverseOrder.PreOrder)
        {
            switch (order)
            {
            case TraverseOrder.PreOrder:
                PreTraverse(_root);
                break;

            case TraverseOrder.PostOrder:
                PostTraverse(_root);
                break;
            }
        }
Exemplo n.º 6
0
        public void printTree(ADSNode node, TraverseOrder order)
        {
            if (node == null)
            {
                return;
            }

            printTree(node.left, order);
            if (order == TraverseOrder.InOrder)
            {
                Console.Write(node.key);
            }
            printTree(node.right, order);
        }
Exemplo n.º 7
0
 // Print the tree in a particular order
 public void TreePrinter(TraverseOrder traverse)
 {
     if (traverse == TraverseOrder.InOrder)
     {
         InOrderTraversal(root);
     }
     else if (traverse == TraverseOrder.PreOrder)
     {
         PreOrderTraversal(root);
     }
     else
     {
         PostOrderTraversal(root);
     }
 }
Exemplo n.º 8
0
 public void Traverse(TraverseOrder traverseOrder)
 {
     if (traverseOrder == TraverseOrder.PreOrder)
     {
         TraversePreOrder(root);
     }
     else if (traverseOrder == TraverseOrder.InOrder)
     {
         TraverseInOrder(root);
     }
     else if (traverseOrder == TraverseOrder.PostOrder)
     {
         TraversePostOrder(root);
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// traverse every item in the tree node recursively.
        /// <para>Use this when <paramref name="treeNode"/>'s structure is flat.</para>
        /// </summary>
        /// <param name="treeNode"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public static IEnumerable <ITreeNode> Traverse(this ITreeNode treeNode, TraverseOrder order)
        {
            switch (order)
            {
            case TraverseOrder.Pre:
                foreach (var item in PreorderTraverse(treeNode))
                {
                    yield return(item);
                }
                break;

            case TraverseOrder.Post:
                foreach (var item in PostorderTraverse(treeNode))
                {
                    yield return(item);
                }
                break;

            default:
                break;
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// enumerate the subtree with the root <paramref name="partialroot"/>, provided with certain information (of type <typeparamref name="TLevel"/>) of the node
        /// 遍历以<paramref name="partialroot"/>为根的子树,并且在过程中提供结点有关信息(<typeparamref name="TLevel"/>类型的)
        /// </summary>
        /// <typeparam name="TLevel">提供的每层结点额外信息的类型</typeparam>
        /// <param name="partialroot">需要遍历的子树的根</param>
        /// <param name="seed">附加参数的初始值</param>
        /// <param name="leftlevelfunc">遍历到某一层结点时将附加参数传给下一层左孩子时需要进行的操作</param>
        /// <param name="rightlevelfunc">遍历到某一层结点时将附加参数传给下一层右孩子时需要进行的操作</param>
        /// <param name="order">二叉树遍历方式</param>
        /// <return>遍历得到的结点与附加参数的集合</return>
        public static IEnumerable <Tuple <IBinaryTreeNode, TLevel> > TraverseSubtree <TLevel>(
            this IBinaryTreeNode partialroot,
            TLevel seed,
            Func <TLevel, IBinaryTreeNode, TLevel> leftlevelfunc,
            Func <TLevel, IBinaryTreeNode, TLevel> rightlevelfunc,
            TraverseOrder order = TraverseOrder.InOrder)
        {
            Contract.Requires <ArgumentNullException>(partialroot != null);
            Contract.Ensures(Contract.Result <IEnumerable <Tuple <IBinaryTreeNode, TLevel> > >() != null);

            var current    = new Tuple <IBinaryTreeNode, TLevel>[] { new Tuple <IBinaryTreeNode, TLevel>(partialroot, seed) };
            var leftlevel  = leftlevelfunc == null ? seed : leftlevelfunc(seed, partialroot);
            var rightlevel = rightlevelfunc == null ? seed : rightlevelfunc(seed, partialroot);

            switch (order)
            {
            case TraverseOrder.InOrder:
                return(partialroot.LeftChild.TraverseSubtree(leftlevel, leftlevelfunc, rightlevelfunc, order)
                       .Concat(current)
                       .Concat(partialroot.RightChild.TraverseSubtree(rightlevel, leftlevelfunc, rightlevelfunc, order)));

            case TraverseOrder.PreOrder:
                return(current
                       .Concat(partialroot.LeftChild.TraverseSubtree(leftlevel, leftlevelfunc, rightlevelfunc, order))
                       .Concat(partialroot.RightChild.TraverseSubtree(rightlevel, leftlevelfunc, rightlevelfunc, order)));

            case TraverseOrder.PostOrder:
                return(partialroot.LeftChild.TraverseSubtree(leftlevel, leftlevelfunc, rightlevelfunc, order)
                       .Concat(partialroot.RightChild.TraverseSubtree(rightlevel, leftlevelfunc, rightlevelfunc, order))
                       .Concat(current));

            case TraverseOrder.LevelOrder:
                return(LevelOrderTraverse(partialroot, seed, leftlevelfunc, rightlevelfunc));

            default:
                return(null);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// enumerate the subtree with the root <paramref name="partialroot"/>, provided with certain information (of type <typeparamref name="TLevel"/>) of the node
        /// 遍历以<paramref name="partialroot"/>为根的子树,并且在过程中提供结点有关信息(<typeparamref name="TLevel"/>类型的)
        /// </summary>
        /// <typeparam name="TLevel">提供的每层结点额外信息的类型</typeparam>
        /// <param name="partialroot">需要遍历的子树的根</param>
        /// <param name="seed">附加参数的初始值</param>
        /// <param name="levelfunc">遍历到某一层结点时将附加参数传给下一层孩子时需要进行的操作。第一个参数是该层的附加参数,
        /// 第二个参数是遍历到的结点,第三个是遍历到的结点在上层结点孩子结点中的编号</param>
        /// <param name="order">多叉树遍历方式</param>
        /// <returns>遍历得到的结点与附加参数的集合</returns>
        public static IEnumerable <Tuple <IMultiwayTreeNode, TLevel> > TraverseSubtree <TLevel>(
            this IMultiwayTreeNode partialroot,
            TLevel seed,
            Func <TLevel, IMultiwayTreeNode, int, TLevel> levelfunc,
            TraverseOrder order = TraverseOrder.LevelOrder)
        {
            Contract.Requires <ArgumentNullException>(partialroot != null);
            Contract.Requires <NotSupportedException>(order != TraverseOrder.InOrder, "多叉树不支持中序遍历");
            Contract.Ensures(Contract.Result <IEnumerable <Tuple <IMultiwayTreeNode, TLevel> > >() != null);

            var current = new Tuple <IMultiwayTreeNode, TLevel>[] { new Tuple <IMultiwayTreeNode, TLevel>(partialroot, seed) };

            switch (order)
            {
            case TraverseOrder.PreOrder:
                return(current.Concat(partialroot.Children?
                                      .SelectMany((node, index) => node?.TraverseSubtree(
                                                      levelfunc == null ? seed : levelfunc(seed, partialroot, index), levelfunc, order)
                                                  ?? Enumerable.Empty <Tuple <IMultiwayTreeNode, TLevel> >())
                                      ?? Enumerable.Empty <Tuple <IMultiwayTreeNode, TLevel> >()));

            case TraverseOrder.PostOrder:
                return((partialroot.Children?
                        .SelectMany((node, index) => node?.TraverseSubtree(
                                        levelfunc == null ? seed : levelfunc(seed, partialroot, index), levelfunc, order)
                                    ?? Enumerable.Empty <Tuple <IMultiwayTreeNode, TLevel> >())
                        ?? Enumerable.Empty <Tuple <IMultiwayTreeNode, TLevel> >())
                       .Reverse().Concat(current));

            case TraverseOrder.LevelOrder:
                return(LevelOrderTraverse(partialroot, seed, levelfunc));

            default:
                return(null);
            }
        }
Exemplo n.º 12
0
        public void Traverse(TraverseOrder traverseOrder)
        {
            switch (traverseOrder)
            {
            case TraverseOrder.PreOrder:     // прямой
                PreOrderPrint(_root);
                break;

            case TraverseOrder.InOrder:
                InOrderPrint(_root);
                break;

            case TraverseOrder.InOrderReverse:
                InOrderPrintReverse(_root);
                break;

            case TraverseOrder.PostOrder:
                PostOrderPrint(_root);
                break;

            default:
                throw new ArgumentOutOfRangeException("traverseOrder");
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Visits all items in the given order
        /// </summary>
        /// <param name="item">The item to visit</param>
        /// <param name="start">The state to use</param>
        /// <returns>All items, including the start item, in the given order</returns>
        private static IEnumerable <VisitedItem> All(ParsedItem item, VisitedItem start, TraverseOrder order)
        {
            start = start ?? new VisitedItem();

            if (order == TraverseOrder.DepthFirstPostOrder)
            {
                start.Parents.Add(item);

                foreach (var n in item.Children)
                {
                    foreach (var c in All(n, start, order))
                    {
                        yield return(c);
                    }
                }

                start.Parents.RemoveAt(start.Parents.Count - 1);

                start.Current = item;
                yield return(start);
            }
            else //if (order == TraverseOrder.DepthFirstPreOrder)
            {
                start.Current = item;
                yield return(start);

                start.Parents.Add(item);

                foreach (var n in item.Children)
                {
                    foreach (var c in All(n, start, order))
                    {
                        yield return(c);
                    }
                }

                start.Parents.RemoveAt(start.Parents.Count - 1);
            }
        }
Exemplo n.º 14
0
 public IEnumerator <TNode> GetEnumerator(TraverseOrder order) => Root.GetSubtreeEnumerator(order);
Exemplo n.º 15
0
 // Print the tree in a particular order
 public void printTree(TraverseOrder order)
 {
     breathPrint(root);
 }
Exemplo n.º 16
0
 // Print the tree in a particular order
 public void printTree(TraverseOrder order)
 {
 }
Exemplo n.º 17
0
 // Print the tree in a particular order
 public void printTree(TraverseOrder order)
 {
     printTree(root, order);
 }
Exemplo n.º 18
0
        public static IEnumerable <IBinaryTreeNode> TraverseSubtree(this IBinaryTreeNode partialroot, TraverseOrder order = TraverseOrder.InOrder)
        {
            Contract.Requires <ArgumentNullException>(partialroot != null);
            Contract.Ensures(Contract.Result <IEnumerable <IBinaryTreeNode> >() != null);

            return(partialroot.TraverseSubtree <object>(null, null, null, order).Select(res => res.Item1));
        }
Exemplo n.º 19
0
Arquivo: Tree.cs Projeto: lzz42/ZHello
        /// <summary>
        /// 10.4.3
        /// n结点的二叉树,使用O(n)的非递归过程,遍历所有的节点的关键字
        /// </summary>
        /// <typeparam name="N"></typeparam>
        /// <typeparam name="K"></typeparam>
        /// <param name="tree"></param>
        /// <param name="node"></param>
        public static void PrintBTree <N, K>(BTree <K> tree, TraverseOrder order)
            where K : IComparable
        {
            if (tree == null)
            {
                return;
            }
            var stack = new Stack <BTree <K> >(1024);

            switch (order)
            {
            case TraverseOrder.PreOrder:
                //前序遍历
                while (tree != null || !stack.IsEmpty())
                {
                    while (tree != null)
                    {
                        Trace.WriteLine(tree.Key);
                        stack.Push(tree);
                        tree = tree.Left;
                    }
                    if (!stack.IsEmpty())
                    {
                        tree = stack.Pop();
                        tree = tree.Right;
                    }
                }
                break;

            case TraverseOrder.InOrder:
                //中序遍历
                while (tree != null || !stack.IsEmpty())
                {
                    while (tree != null)
                    {
                        stack.Push(tree);
                        tree = tree.Left;
                    }
                    if (!stack.IsEmpty())
                    {
                        tree = stack.Pop();
                        Trace.WriteLine(tree.Key);
                        tree = tree.Right;
                    }
                }
                break;

            case TraverseOrder.PostOrder:
                //后序遍历
                var stack2 = new Stack <BTree <K> >(1024);
                while (tree != null || !stack.IsEmpty())
                {
                    while (tree != null)
                    {
                        stack.Push(tree);
                        stack2.Push(tree);
                        tree = tree.Right;
                    }
                    if (!stack.IsEmpty())
                    {
                        tree = stack.Pop();
                        tree = tree.Left;
                    }
                }
                while (!stack2.IsEmpty())
                {
                    tree = stack2.Pop();
                    Trace.WriteLine(tree.Key);
                }
                break;

            case TraverseOrder.LevelOrder:
                break;

            default:
                break;
            }
        }
Exemplo n.º 20
0
 // Print the tree in a particular order
 public void PrintTree(TraverseOrder order)
 {
     PrintRecursive(Root, order);
     Console.WriteLine();
 }
Exemplo n.º 21
0
 /// <summary>
 /// Visits all items in the given order
 /// </summary>
 /// <param name="item">The item to visit</param>
 /// <param name="order">The order to visit in</param>
 /// <returns>All items, including the start item, in the given order</returns>
 public static IEnumerable <VisitedItem> All(this ParsedItem item, TraverseOrder order = TraverseOrder.DepthFirstPreOrder)
 {
     return(All(item, new VisitedItem(), order));
 }
Exemplo n.º 22
0
        public static IEnumerable <IMultiwayTreeNode> TraverseSubtree(this IMultiwayTreeNode partialroot, TraverseOrder order = TraverseOrder.LevelOrder)
        {
            Contract.Requires <ArgumentNullException>(partialroot != null);
            Contract.Requires <NotSupportedException>(order != TraverseOrder.InOrder, "多叉树不支持中序遍历");
            Contract.Ensures(Contract.Result <IEnumerable <IMultiwayTreeNode> >() != null);

            return(partialroot.TraverseSubtree <object>(null, null, order).Select(res => res.Item1));
        }
Exemplo n.º 23
0
 /// <summary>
 /// Visits all items in depth first pre-order
 /// </summary>
 /// <param name="item"></param>
 /// <param name="order">The order to visit in</param>
 /// <returns>All items, including the start item, in the given order</returns>
 public static IEnumerable <VisitedItem> All(this IEnumerable <ParsedItem> item, TraverseOrder order = TraverseOrder.DepthFirstPreOrder)
 {
     foreach (var s in item)
     {
         foreach (var n in All(s, order))
         {
             yield return(n);
         }
     }
 }
Exemplo n.º 24
0
        /// <summary>
        /// Get the enumerator of subtree, root of which is <paramref name="partialroot"/>
        /// 获取以<paramref name="partialroot"/>为根的子树的遍历器
        /// </summary>
        /// <param name="order">二叉树遍历方式</param>
        /// <param name="partialroot">需要遍历的子树的根</param>
        public static IEnumerator <TNode> GetSubtreeEnumerator <TNode>(this TNode partialroot, TraverseOrder order = TraverseOrder.InOrder)
            where TNode : class, IBinaryTreeNode, IPersistent
        {
            Contract.Requires <ArgumentNullException>(partialroot != null);
            Contract.Ensures(Contract.Result <IEnumerator <TNode> >() != null);

            switch (order)
            {
            case TraverseOrder.InOrder:
                return(new BinaryTreeEnumerator <TNode> .InOrderEnumerator(partialroot));

            case TraverseOrder.PreOrder:
                return(new BinaryTreeEnumerator <TNode> .PreOrderEnumerator(partialroot));

            case TraverseOrder.PostOrder:
                return(new BinaryTreeEnumerator <TNode> .PostOrderEnumerator(partialroot));

            case TraverseOrder.LevelOrder:
                return(new BinaryTreeEnumerator <TNode> .LevelOrderEnumerator(partialroot));

            default:
                return(null);
            }
        }