static void Make(Node BeginNode,List<Edge> result) { if (BeginNode.IsVisited) return; foreach (var node in BeginNode.Children) { result.Add(new Edge { Begin = BeginNode.ID, End = node.ID }); Make(node, result); node.IsVisited = true; } }
static void Main(string[] args) { var n1 = new Node { ID = 1 }; var n2 = new Node { ID = 2 }; var n3 = new Node { ID = 3 }; var n4 = new Node { ID = 4 }; var n5 = new Node { ID = 5 }; var n6 = new Node { ID = 6 }; var n7 = new Node { ID = 7 }; var n8 = new Node { ID = 8 }; var n9 = new Node { ID = 9 }; var n10 = new Node { ID = 10 }; n1.Children.Add(n2); n1.Children.Add(n3); n1.Children.Add(n4); n2.Children.Add(n5); n3.Children.Add(n6); n5.Children.Add(n7); n6.Children.Add(n7); n4.Children.Add(n7); n4.Children.Add(n9); n7.Children.Add(n8); n7.Children.Add(n9); n8.Children.Add(n10); n9.Children.Add(n10); var result = new List<Edge>(); Make(n1, result); result.ForEach(r => Console.WriteLine("begin={0},end={1}", r.Begin, r.End)); Console.WriteLine(ToString(result)); Console.WriteLine("/////////////////////////////////"); var buffer = ToBytes(result); Dictionary<int, Node> restore; Restore(buffer,out restore); var result2 = new List<Edge>(); Make(restore.First().Value, result2); result2.ForEach(r => Console.WriteLine("begin={0},end={1}", r.Begin, r.End)); Console.WriteLine(ToString(result2)); Console.Read(); }
static void Restore(byte[] value, out Dictionary<int,Node> nodeList) { nodeList = new Dictionary<int, Node>(); using (var ms=new MemoryStream(value)) { var br = new BinaryReader(ms); var n = br.ReadInt32(); for (int i = 0; i < n; i++) { var begin = br.ReadInt32(); var end = br.ReadInt32(); Node beginNode ; if(nodeList.TryGetValue(begin,out beginNode)==false) { beginNode = new Node { ID = begin }; nodeList[begin] = beginNode; } Node endNode ; if (nodeList.TryGetValue(end, out endNode) == false) { endNode = new Node { ID = end }; nodeList[end] = endNode; } beginNode.Children.Add(endNode); } } }
/// <summary> /// 流程驳回,驳回到指定节点 /// </summary> /// <remarks></remarks> public void Refuse(Node toNode) { throw new System.NotImplementedException(); }