Exemplo n.º 1
0
        public IList <int> Postorder(NTreeNode root)
        {
            List <int> res = new List <int>();

            if (root == null)
            {
                return(res);
            }

            Stack <NTreeNode> stk = new Stack <NTreeNode>();

            stk.Push(root);
            while (stk.Count > 0)
            {
                NTreeNode node = stk.Pop();
                res.Add(node.val);
                if (node.children != null)
                {
                    for (int i = 0; i < node.children.Count; i++)
                    {
                        stk.Push(node.children[i]);
                    }
                }
            }
            res.Reverse();
            return(res);
        }
Exemplo n.º 2
0
        /// <summary>
        /// N-ary Tree Preorder Traversal:
        /// Given the root of an n-ary tree, return the preorder traversal of its nodes' values.
        /// Nary-Tree input serialization is represented in their level order traversal.
        /// Each group of children is separated by the null value
        /// https://leetcode.com/problems/n-ary-tree-preorder-traversal/
        /// </summary>
        /// <param name="root"></param>
        /// <returns></returns>
        public IList <int> Preorder(NTreeNode root)
        {
            if (root == null)
            {
                return(new List <int>());
            }

            var resultList = new List <int>();
            var stack      = new Stack <NTreeNode>();

            stack.Push(root);

            while (stack.Count > 0)
            {
                root = stack.Pop();

                resultList.Add(root.val);

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

            return(resultList);
        }
Exemplo n.º 3
0
        void loadTree()
        {
            var tns = Hosts.LoadHosts();

            if (tns == null)
            {
                return;
            }
            transNode(tns, tv_ses.Nodes);

            void transNode(List <Tn> _tns, TreeNodeCollection tnc)
            {
                foreach (var t in _tns)
                {
                    NTreeNode tn;
                    if (t.Type == TnType.Group)
                    {
                        tn = new NTreeNode(t.Name);
                    }
                    else
                    {
                        tn = new NTreeNode(t.Host);
                    }
                    tn.Type = t.Type;
                    transNode(t.Child, tn.Nodes);
                    tnc.Add(tn);
                    tn.Expand();
                }
            }
        }
Exemplo n.º 4
0
        private void tsmi_tree_copy_Click(object sender, EventArgs e)
        {
            var selectNode = tv_ses.SelectedNode as NTreeNode;

            if (selectNode == null)
            {
                return;
            }

            if (selectNode.Type != TnType.Host)
            {
                return;
            }

            var he = new Host();

            he.SetHost(selectNode.Host);
            if (he.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            var newNode = new NTreeNode(he.host)
            {
                Type = TnType.Host
            };

            var nds = selectNode == null ? tv_ses.Nodes : selectNode.Parent.Nodes;

            nds.Add(newNode);

            selectNode?.Expand();

            saveTree();
        }
Exemplo n.º 5
0
Arquivo: NTree.cs Projeto: The5-1/Epos
 public void Traverse(NTreeNode <T> node, TreeVisitor <T> visitor)
 {
     visitor(node.data);
     foreach (NTreeNode <T> kid in node.children)
     {
         Traverse(kid, visitor);
     }
 }
Exemplo n.º 6
0
        private void tsb_new_sdir_Click(object sender, EventArgs e)
        {
            var newNode = new NTreeNode("会话组")
            {
                Type = TnType.Group
            };
            var selectNode = tv_ses.SelectedNode as NTreeNode;

            var nds = selectNode == null ? tv_ses.Nodes : selectNode.Nodes;

            nds.Add(newNode);

            selectNode?.Expand();
            saveTree();
        }
Exemplo n.º 7
0
        internal static void AddChildNodes(NTreeNodeCollection collection, int siblingCount, ref int currDepth, int depth)
        {
            NTreeNode node;

            for (int i = 0; i < siblingCount; i++)
            {
                node      = new NTreeNode();
                node.Text = "Sample Tree Node " + i + "; Depth: " + currDepth;
                collection.Add(node);

                if (currDepth < depth)
                {
                    currDepth++;
                    AddChildNodes(node.Nodes, siblingCount, ref currDepth, depth);
                    currDepth--;
                }
            }
        }
Exemplo n.º 8
0
        private void addChildBtn_Click(object sender, EventArgs e)
        {
            NTreeNode focused = nTreeViewEx1.FocusedItem as NTreeNode;

            if (focused == null)
            {
                return;
            }

            nTreeViewEx1.Suspend();

            NTreeNode child = new NTreeNode();

            child.Text = "Child Node " + focused.Nodes.Count;
            focused.Nodes.Add(child);
            child.Selected = true;

            nTreeViewEx1.Resume(true);
        }
Exemplo n.º 9
0
        public void NaryPreOrder()
        {
            Console.WriteLine($"--Executing: {Helper.WhosThere()}");
            var naryTree = new NTreeNode
            {
                val      = 1,
                children = new List <NTreeNode>()
                {
                    new NTreeNode()
                    {
                        val      = 3,
                        children = new List <NTreeNode>()
                        {
                            new NTreeNode()
                            {
                                val      = 5,
                                children = new List <NTreeNode>()
                            },
                            new NTreeNode()
                            {
                                val      = 6,
                                children = new List <NTreeNode>()
                            }
                        }
                    },
                    new NTreeNode()
                    {
                        val      = 2,
                        children = new List <NTreeNode>()
                    },
                    new NTreeNode()
                    {
                        val      = 4,
                        children = new List <NTreeNode>()
                    }
                }
            };

            Console.WriteLine($"--N-ary Preorder Traversal: {string.Join(",", Preorder(naryTree))}");
            Helper.InsertBlankSep();
        }
Exemplo n.º 10
0
        public int MaxDepth(NTreeNode root)
        {
            if (root == null)
            {
                return(0);
            }

            int maxSubDepth = 0;

            if (root.children != null)
            {
                foreach (var node in root.children)
                {
                    int nodeDepth = MaxDepth(node);
                    if (nodeDepth > maxSubDepth)
                    {
                        maxSubDepth = nodeDepth;
                    }
                }
            }
            return(maxSubDepth + 1);
        }
Exemplo n.º 11
0
        private void addSiblingBtn_Click(object sender, EventArgs e)
        {
            NTreeNode focused = nTreeViewEx1.FocusedItem as NTreeNode;

            if (focused == null)
            {
                return;
            }

            NTreeNodeCollection ownerNodes = focused.OwnerCollection as NTreeNodeCollection;

            if (ownerNodes == null)
            {
                return;
            }

            NTreeNode sibling = new NTreeNode();

            sibling.Text = "Sibling " + ownerNodes.Count;
            ownerNodes.Insert(focused.Index + 1, sibling);
            sibling.Selected = true;
        }
Exemplo n.º 12
0
        internal static void AddTestNodes(NTreeViewEx tree, int siblingCount, int depth)
        {
            tree.Suspend();
            tree.Nodes.Clear();

            NTreeNode           node;
            int                 currDepth      = 0;
            NTreeNodeCollection currCollection = tree.Nodes;

            for (int i = 0; i < siblingCount; i++)
            {
                node      = new NTreeNode();
                node.Text = "Sample Tree Node " + i + "; Depth: " + currDepth;

                currDepth++;
                AddChildNodes(node.Nodes, siblingCount, ref currDepth, depth);
                currDepth--;

                currCollection.Add(node);
            }

            tree.Resume(true);
        }
Exemplo n.º 13
0
        //广度优先算法。每次出队列时,把出列节点的子节点添加进队列。只是这次的子节点是链表
        public IList <IList <int> > LevelOrder(NTreeNode root)
        {
            if (root == null)
            {
                return(new List <IList <int> >());
            }

            Queue <IList <NTreeNode> > queue = new Queue <IList <NTreeNode> >();
            List <IList <int> >        res   = new List <IList <int> >();

            queue.Enqueue(new List <NTreeNode> {
                root
            });
            while (queue.Count > 0)
            {
                IList <NTreeNode> nodesList    = queue.Dequeue();
                IList <NTreeNode> childrenList = new List <NTreeNode>();
                List <int>        addList      = new List <int>();
                foreach (NTreeNode node in nodesList)
                {
                    addList.Add(node.val);
                    foreach (NTreeNode n in node.children)
                    {
                        childrenList.Add(n);
                    }
                }
                if (addList.Count > 0)
                {
                    res.Add(addList);
                }
                if (childrenList.Count > 0)
                {
                    queue.Enqueue(childrenList);
                }
            }
            return(res);
        }
Exemplo n.º 14
0
        private void settings1Btn_Click(object sender, EventArgs e)
        {
            nTreeViewEx1.Suspend();
            nTreeViewEx1.IndicatorStyle = TreeViewIndicatorStyle.OnLeft;

            nTreeViewEx1.NormalState.TextRenderingHint           = TextRenderingHint.ClearTypeGridFit;
            nTreeViewEx1.HotState.TextRenderingHint              = TextRenderingHint.ClearTypeGridFit;
            nTreeViewEx1.SelectedState.TextRenderingHint         = TextRenderingHint.ClearTypeGridFit;
            nTreeViewEx1.HotSelectedState.TextRenderingHint      = TextRenderingHint.ClearTypeGridFit;
            nTreeViewEx1.PressedState.TextRenderingHint          = TextRenderingHint.ClearTypeGridFit;
            nTreeViewEx1.InactiveSelectedState.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

            nTreeViewEx1.Nodes.Clear();
            nTreeViewEx1.ItemImageSize          = new NSize(32, 32);
            nTreeViewEx1.HotState.Image         = m_Images[3];
            nTreeViewEx1.SelectedState.Image    = m_Images[4];
            nTreeViewEx1.HotSelectedState.Image = m_Images[5];

            NTreeNode child1, child2, child3;
            string    richText = "<font size='10' face='Trebuchet MS' color='Navy'><b>Sample tree-node with rich text formatting</b></font><br/>The rich text however results in slower speed of the tree-view.<br/>You may specify rich text per desired node only to optimize performance.";

            NTooltipInfo info = new NTooltipInfo();

            info.CaptionText      = "<font face='Trebuchet MS' size='10'><b>Sample tooltip header.</b></font>";
            info.ContentImage     = m_Images[1];
            info.ContentImageSize = new NSize(32, 32);
            info.ContentText      = "This is sample tooltip content. It supports rich text and appearance settings per node.<br/>The individual tooltip is with higher priority than the internal one displayed for hidden items.";

            for (int i = 1; i < 21; i++)
            {
                child1                 = new NTreeNode();
                child1.TextAlign       = ContentAlignment.TopLeft;
                child1.Text            = richText;
                child1.Image           = m_Images[1];
                child1.TextProcessMode = ItemTextProcessMode.RichText;
                child1.CommonIndicator = (CommonIndicator)i;
                child1.TooltipInfo     = info;

                for (int j = 1; j < 21; j++)
                {
                    child2                 = new NTreeNode();
                    child2.Text            = "Simple Text Node";
                    child2.Image           = m_Images[1];
                    child2.CommonIndicator = (CommonIndicator)j;
                    child2.TooltipInfo     = info;

                    for (int k = 1; k < 21; k++)
                    {
                        child3                 = new NTreeNode();
                        child3.Text            = "Simple Text Node";
                        child3.Image           = m_Images[1];
                        child3.CommonIndicator = (CommonIndicator)k;
                        child3.TooltipInfo     = info;

                        child2.Nodes.Add(child3);
                    }

                    child1.Nodes.Add(child2);
                }

                nTreeViewEx1.Nodes.Add(child1);
            }

            nTreeViewEx1.Resume(true);
            this.nTreeViewEx1.Nodes[0].Expand();
        }
Exemplo n.º 15
0
        private void settings2Btn_Click(object sender, EventArgs e)
        {
            nTreeViewEx1.Suspend();
            nTreeViewEx1.Nodes.Clear();

            nTreeViewEx1.NormalState.TextRenderingHint           = TextRenderingHint.SystemDefault;
            nTreeViewEx1.HotState.TextRenderingHint              = TextRenderingHint.SystemDefault;
            nTreeViewEx1.SelectedState.TextRenderingHint         = TextRenderingHint.SystemDefault;
            nTreeViewEx1.HotSelectedState.TextRenderingHint      = TextRenderingHint.SystemDefault;
            nTreeViewEx1.PressedState.TextRenderingHint          = TextRenderingHint.SystemDefault;
            nTreeViewEx1.InactiveSelectedState.TextRenderingHint = TextRenderingHint.SystemDefault;

            NLightUIItemVisualState normalState = new NLightUIItemVisualState();

            normalState.FillInfo.Gradient1       = Color.Orange;
            normalState.FillInfo.Gradient2       = Color.Yellow;
            normalState.FillInfo.GradientStyle   = Nevron.UI.WinForm.Controls.GradientStyle.SigmaBell;
            normalState.StrokeInfo.SmoothingMode = SmoothingMode.AntiAlias;
            normalState.StrokeInfo.Rounding      = 3;
            normalState.StrokeInfo.Color         = Color.Black;
            normalState.TextFillInfo.FillStyle   = FillStyle.Solid;
            normalState.TextFillInfo.Color       = Color.Black;

            NLightUIItemVisualState hotState = new NLightUIItemVisualState();

            hotState.FillInfo.Gradient1       = Color.Yellow;
            hotState.FillInfo.Gradient2       = Color.Orange;
            hotState.FillInfo.GradientStyle   = Nevron.UI.WinForm.Controls.GradientStyle.Vista;
            hotState.StrokeInfo.SmoothingMode = SmoothingMode.AntiAlias;
            hotState.StrokeInfo.PenWidth      = 2;
            hotState.StrokeInfo.DashStyle     = DashStyle.Dash;
            hotState.StrokeInfo.Color         = Color.Black;
            hotState.TextFillInfo.Color       = Color.Black;

            NLightUIItemVisualState selectedState = new NLightUIItemVisualState();

            selectedState.FillInfo.Gradient1       = Color.Orange;
            selectedState.FillInfo.Gradient2       = Color.Black;
            selectedState.StrokeInfo.SmoothingMode = SmoothingMode.AntiAlias;
            selectedState.StrokeInfo.Rounding      = 3;
            selectedState.StrokeInfo.Color         = Color.Black;
            selectedState.TextFillInfo.Color       = Color.White;

            nTreeViewEx1.IndicatorStyle         = TreeViewIndicatorStyle.OnLeft;
            nTreeViewEx1.ItemImageSize          = new NSize(32, 32);
            nTreeViewEx1.HotState.Image         = m_Images[3];
            nTreeViewEx1.SelectedState.Image    = m_Images[4];
            nTreeViewEx1.HotSelectedState.Image = m_Images[5];
            nTreeViewEx1.TrackHotSelectedState  = false;

            NTreeNode child1, child2, child3;

            for (int i = 1; i < 21; i++)
            {
                child1                 = new NTreeNode();
                child1.Text            = "Tree node with local Hot visual state";
                child1.Image           = m_Images[1];
                child1.CommonIndicator = (CommonIndicator)i;
                child1.SetVisualState(hotState, ItemVisualState.Hot);

                for (int j = 1; j < 21; j++)
                {
                    child2                 = new NTreeNode();
                    child2.Text            = "Tree node with local Normal visual state";
                    child2.Image           = m_Images[1];
                    child2.CommonIndicator = (CommonIndicator)j;
                    child2.SetVisualState(normalState, ItemVisualState.Normal);

                    for (int k = 1; k < 21; k++)
                    {
                        child3                 = new NTreeNode();
                        child3.Text            = "Tree node with local Selected visual state";
                        child3.Image           = m_Images[1];
                        child3.CommonIndicator = (CommonIndicator)k;
                        child3.SetVisualState(selectedState, ItemVisualState.Selected);

                        child2.Nodes.Add(child3);
                    }

                    child1.Nodes.Add(child2);
                }

                nTreeViewEx1.Nodes.Add(child1);
            }

            this.nTreeViewEx1.Nodes[0].Expand();
            nTreeViewEx1.Resume(true);
        }