public string Visualize() { TreeVisualizer vis = new TreeVisualizer(); Visualize(_root, vis); return(JsonConvert.SerializeObject(vis)); }
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 = node.Left.Color == NodeColor.RED ? "RED" : "BLACK" //,label = "left" }); Visualize(node.Left, vis); } 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 = node.Right.Color == NodeColor.RED ? "RED" : "BLACK" //,label = "right" }); Visualize(node.Right, vis); } 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" }); } }