Пример #1
0
        public string Visualize()
        {
            TreeVisualizer vis = new TreeVisualizer();

            Visualize(_root, vis);
            return(JsonConvert.SerializeObject(vis));
        }
Пример #2
0
        private void Visualize(TreeNode <TKey, TValue> node, TreeVisualizer vis)
        {
            if (node == null)
            {
                return;
            }
            vis.nodes.Add(new TreeVisualizer.Node()
            {
                id = node.Key.ToString(), label = node.Key.ToString()
            });
            if (node.Left != null)
            {
                vis.edges.Add(new TreeVisualizer.Edge()
                {
                    from = node.Key.ToString(),
                    to   = node.Left.Key.ToString()
                           //, label = "left"
                });
            }
            else
            {
                vis.nodes.Add(new TreeVisualizer.Node()
                {
                    id    = $"{node.Key.ToString()}-left",
                    label = $"null"
                });
                vis.edges.Add(new TreeVisualizer.Edge()
                {
                    from = node.Key.ToString(),
                    to   = $"{node.Key.ToString()}-left"
                           //, label = "left"
                });
            }
            if (node.Right != null)
            {
                vis.edges.Add(new TreeVisualizer.Edge()
                {
                    from = node.Key.ToString(),
                    to   = node.Right.Key.ToString()
                           //, label = "right"
                });
            }
            else
            {
                vis.nodes.Add(new TreeVisualizer.Node()
                {
                    id = $"{node.Key.ToString()}-right", label = $"null"
                });
                vis.edges.Add(new TreeVisualizer.Edge()
                {
                    from = node.Key.ToString(),
                    to   = $"{node.Key.ToString()}-right"
                           //, label = "right"
                });
            }

            Visualize(node.Left, vis);
            Visualize(node.Right, vis);
        }