Пример #1
0
        private void PreOrder(List <T> treeValues, KNode <T> root)
        {
            treeValues.Add(root.Value);

            if (root.Children != null)
            {
                foreach (var child in root.Children)
                {
                    PreOrder(treeValues, child);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// PreOrder traversal
        /// </summary>
        /// <returns> List of node values </returns>
        public List <T> PreOrder()
        {
            List <T>  treeValues = new List <T>();
            KNode <T> root       = Root;

            try
            {
                PreOrder(treeValues, root);
            }
            catch (ArgumentException e)
            {
                throw new NullReferenceException($"womp womp, {e}");
            }
            return(treeValues);
        }
Пример #3
0
        /// <summary>
        /// Breadth First traversal
        /// </summary>
        /// <returns> List of node values </returns>
        public List <T> BreadthFirst()
        {
            List <T>           treeValues = new List <T>();
            Queue <KNode <T> > holder     = new Queue <KNode <T> >();
            KNode <T>          node       = Root;

            holder.Enqueue(node);

            while (!holder.IsEmpty())
            {
                node = holder.Dequeue();
                treeValues.Add(node.Value);

                if (node.Children != null)
                {
                    foreach (var child in node.Children)
                    {
                        holder.Enqueue(child);
                    }
                }
            }
            return(treeValues);
        }