//重新写结构 public void WriteStructure() { StreamReader sr = new StreamReader("C:\\Users\\dell\\Desktop\\暑假\\实验数据\\1.txt", Encoding.Default); StreamWriter sw = new StreamWriter("C:\\Users\\dell\\Desktop\\暑假\\实验数据\\2.txt"); String read = sr.ReadLine(); while (read != null) { //开始层次遍历 Dictionary <int, List <String> > dic = new Dictionary <int, List <string> >(); BinaryTreeNode bnode = new BinaryTreeNode(); FinalNode1 root = new FinalNode1(); FinalNode1 tempNode = new FinalNode1(); root = bnode.getNode(read); root.xuhao = 1; tempNode = root;//临时指向根结点 Queue <FinalNode1> q = new Queue <FinalNode1>(); q.Enqueue(root); while (q.Count != 0) { //指向队列第一个字符 tempNode = q.First(); if (dic.Count == 0) { List <String> list = new List <string>(); list.Add(tempNode.zifu + "1" + "1"); dic.Add(tempNode.BTreeLevel, list); } else { if (dic.ContainsKey(tempNode.BTreeLevel)) { if (isOperator(tempNode.zifu)) { dic[tempNode.BTreeLevel].Add(tempNode.zifu + tempNode.BTreeLevel + tempNode.xuhao); } else { dic[tempNode.BTreeLevel].Add("$" + tempNode.BTreeLevel + tempNode.xuhao); } } else { if (isOperator(tempNode.zifu)) { List <String> list = new List <string>(); list.Add(tempNode.zifu + tempNode.BTreeLevel + tempNode.xuhao); dic.Add(tempNode.BTreeLevel, list); } else { List <String> list = new List <string>(); list.Add("$" + tempNode.BTreeLevel + tempNode.xuhao); dic.Add(tempNode.BTreeLevel, list); } } } //第一个节点出队 q.Dequeue(); if (tempNode.left != null) { tempNode.left.xuhao = tempNode.xuhao * 2; //Console.WriteLine("++++++++++++++left" + tempNode.left.xuhao); q.Enqueue(tempNode.left); } if (tempNode.right != null) { tempNode.right.xuhao = tempNode.xuhao * 2 + 1; //Console.WriteLine("++++++++++++++" + tempNode.right.xuhao); q.Enqueue(tempNode.right); } } //开始写了 String str = null; foreach (var it in dic) { String text = ""; foreach (var itt in it.Value) { text = text + itt; } str = str + text + "#"; } str = str + read; Console.WriteLine("结果为:" + str); sw.WriteLine(str); read = sr.ReadLine(); }//读取内容 sw.Flush(); }