//根据root递归生成最小表示法获得的字符串 public string GenerateMinusExp(Node root) { //如果是叶结点的话,则直接返回该结点的值 if (root.IsLeaf()) { return(root.Value); } else if (root.Value == "+" || root.Value == "×") { //对左子树进行递归,得到左子树的最小表示字符串 string LeftMinus = GenerateMinusExp(root.Left); //对右子树进行递归,得到右子树的最小表示字符串 string RightMinus = GenerateMinusExp(root.Right); //对左子树和右子树进行统一排序 if (string.Compare(LeftMinus, RightMinus) <= 0) { return(root.Value + LeftMinus + RightMinus); } else { return(root.Value + RightMinus + LeftMinus); } } //否则就按照正常次序进行最小字符串表示 else { return(root.Value + GenerateMinusExp(root.Left) + GenerateMinusExp(root.Right)); } }
//通过层次遍历的方法直接生成一颗二叉树 // public void GenerateBinaryTree(Fraction[] numbers,string[] operation) // { // //将两个数组混合成为一个队列,当队列为空时标明二叉树建立成功 // Queue<Node> Nodes = new Queue<Node>(); // foreach(string op in operation) // Nodes.Enqueue(new Node(op)); // foreach(Fraction num in numbers) // Nodes.Enqueue(new Node(num.ToString())); // //设立一个队列用于按层次构建二叉树 // Queue<Node> NodeQ = new Queue<Node>(); // //以Nodes[0]建立根结点 // //从待选取队列中除去头结点 // Root = Nodes.Dequeue(); // //根结点入队 // NodeQ.Enqueue(Root); // //当待选取队列不为空的时候要一直出队入队进行 // while (Nodes.Count()!=0) // { // //头结点出队 // Node Head = NodeQ.Dequeue(); // //如果没有左结点,则从Nodes中获取 // if (Head.Left == null) // Head.Left = Nodes.Dequeue(); // //由于该结点已经被选取了并加入了树中,所以加入NodeQ中 // NodeQ.Enqueue(Head.Left); // //如果没有右结点,则从Nodes中获取 // if (Head.Right == null) // Head.Right = Nodes.Dequeue(); // //由于右结点已经被选取了并加入了树中,所以加入NodeQ中 // NodeQ.Enqueue(Head.Right); // } // } //中序遍历得到二叉树对应中缀表达式 public string InOrderToInfixWithBrack(Node root) { //如果不是为叶结点或者不是根结点,则加括号 if (!root.IsLeaf() && root != Root) { return("( " + InOrderToInfixWithBrack(root.Left) + " " + root.Value + " " + InOrderToInfixWithBrack(root.Right) + " )"); } //如果不是叶结点却是根结点 else if (!root.IsLeaf()) { return(InOrderToInfixWithBrack(root.Left) + " " + root.Value + " " + InOrderToInfixWithBrack(root.Right)); } else { return(root.Value); } }
//通过层次遍历的方法直接生成一颗二叉树 // public void GenerateBinaryTree(Fraction[] numbers,string[] operation) // { // //将两个数组混合成为一个队列,当队列为空时标明二叉树建立成功 // Queue<Node> Nodes = new Queue<Node>(); // foreach(string op in operation) // Nodes.Enqueue(new Node(op)); // foreach(Fraction num in numbers) // Nodes.Enqueue(new Node(num.ToString())); // //设立一个队列用于按层次构建二叉树 // Queue<Node> NodeQ = new Queue<Node>(); // //以Nodes[0]建立根结点 // //从待选取队列中除去头结点 // Root = Nodes.Dequeue(); // //根结点入队 // NodeQ.Enqueue(Root); // //当待选取队列不为空的时候要一直出队入队进行 // while (Nodes.Count()!=0) // { // //头结点出队 // Node Head = NodeQ.Dequeue(); // //如果没有左结点,则从Nodes中获取 // if (Head.Left == null) // Head.Left = Nodes.Dequeue(); // //由于该结点已经被选取了并加入了树中,所以加入NodeQ中 // NodeQ.Enqueue(Head.Left); // //如果没有右结点,则从Nodes中获取 // if (Head.Right == null) // Head.Right = Nodes.Dequeue(); // //由于右结点已经被选取了并加入了树中,所以加入NodeQ中 // NodeQ.Enqueue(Head.Right); // } // } //中序遍历得到二叉树对应中缀表达式 public string InOrderToInfixWithBrack(Node root) { //如果不是为叶结点或者不是根结点,则加括号 if (!root.IsLeaf() && root != Root) return "( " + InOrderToInfixWithBrack(root.Left) + " " + root.Value + " " + InOrderToInfixWithBrack(root.Right) + " )"; //如果不是叶结点却是根结点 else if (!root.IsLeaf()) return InOrderToInfixWithBrack(root.Left) + " " + root.Value + " " + InOrderToInfixWithBrack(root.Right); else return root.Value; }
//根据root递归生成最小表示法获得的字符串 public string GenerateMinusExp(Node root) { //如果是叶结点的话,则直接返回该结点的值 if (root.IsLeaf()) return root.Value; else if (root.Value == "+" || root.Value == "×") { //对左子树进行递归,得到左子树的最小表示字符串 string LeftMinus = GenerateMinusExp(root.Left); //对右子树进行递归,得到右子树的最小表示字符串 string RightMinus = GenerateMinusExp(root.Right); //对左子树和右子树进行统一排序 if (string.Compare(LeftMinus, RightMinus) <= 0) return root.Value + LeftMinus + RightMinus; else return root.Value + RightMinus + LeftMinus; } //否则就按照正常次序进行最小字符串表示 else return root.Value + GenerateMinusExp(root.Left) + GenerateMinusExp(root.Right); }