Exemple #1
0
        public List <int> PreOrderLoop(MultiTreeNode root)
        {
            List <int> vals = new List <int>();

            Stack <MultiTreeNode> stack = new Stack <MultiTreeNode>();

            stack.Push(root);
            while (stack.Count > 0)
            {
                MultiTreeNode n = stack.Pop();
                vals.Add(n.Val);

                List <MultiTreeNode> nodes = new List <MultiTreeNode>();
                MultiTreeNode        child = n.LeftChild;
                while (child != null)
                {
                    nodes.Add(child);
                    child = child.RightSibling;
                }

                for (int i = nodes.Count - 1; i >= 0; i--)
                {
                    stack.Push(nodes[i]);
                }
            }

            return(vals);
        }
Exemple #2
0
        public List <int> PostOrder(MultiTreeNode root)
        {
            List <int> vals = new List <int>();

            PostTraverse(root, vals);
            return(vals);
        }
Exemple #3
0
        private void PostTraverse(MultiTreeNode n, List <int> vals)
        {
            MultiTreeNode child = n.LeftChild;

            while (child != null)
            {
                PostTraverse(child, vals);
                child = child.RightSibling;
            }
            vals.Add(n.Val);
        }
Exemple #4
0
        private static void MultiTreeTest()
        {
            MultiTreeNode root = new MultiTreeNode()
            {
                Val = 1
            };

            root.LeftChild = new MultiTreeNode()
            {
                Val = 2
            };
            root.LeftChild.RightSibling = new MultiTreeNode()
            {
                Val = 3
            };
            root.LeftChild.LeftChild = new MultiTreeNode()
            {
                Val = 4
            };
            root.LeftChild.LeftChild.RightSibling = new MultiTreeNode()
            {
                Val = 5
            };
            root.LeftChild.RightSibling.LeftChild = new MultiTreeNode()
            {
                Val = 6
            };
            root.LeftChild.RightSibling.LeftChild.RightSibling = new MultiTreeNode()
            {
                Val = 7
            };

            MultiTree tree = new MultiTree();
            var       vals = tree.PreOrderLoop(root);

            Console.WriteLine("Pre Order Loop: " + string.Join(',', vals));
            vals = tree.PostOrderLoop(root);
            Console.WriteLine("Post Order Loop: " + string.Join(',', vals));
            vals = tree.PreOrder(root);
            Console.WriteLine("Pre Order: " + string.Join(',', vals));
            vals = tree.PostOrder(root);
            Console.WriteLine("Post Order: " + string.Join(',', vals));
        }
Exemple #5
0
        public List <int> PostOrderLoop(MultiTreeNode root)
        {
            List <int> vals = new List <int>();

            Stack <MultiTreeNode> stack = new Stack <MultiTreeNode>();
            MultiTreeNode         cur   = root;

            while (stack.Count > 0 || cur != null)
            {
                while (cur != null)
                {
                    stack.Push(cur);
                    cur = cur.LeftChild;
                }
                cur = stack.Pop();
                vals.Add(cur.Val);
                cur = cur.RightSibling;
            }

            return(vals);
        }