所以我有一个程序可以创建一个用户输入数的素因子的二叉树,并将它们显示在treeView控件中:
现在我想创建一个类似于消息框中显示的字符串,除了指数(“256 = 2 ^ 8”,“1234567890 = 2 X 3 ^ 2 X 5 X 3607 X 3803”)
我目前的代码如下:
private void LabelDisplayCondensed(FactorTreeNode currentNode)
{
string result = Convert.ToString(root.Key) + " = "
+ Convert.ToString(currentNode.Left.Key);
FactorTreeNode prevNode = currentNode;
int exponent = 1;
while (currentNode.Right != null)
{
prevNode = currentNode;
currentNode = currentNode.Right;
if (currentNode.Left.Key == prevNode.Left.Key)
{
exponent += 1;
}
else
{
exponent = 1;
}
if ((exponent != 1) && (currentNode.Left.Key != prevNode.Left.Key))
{
result += " ^ " + exponent + " X " + currentNode.Left.Key;
}
}
MessageBox.Show(result);
}
这是我最近的绝望尝试。使用树的根调用该函数。我意识到这段代码完全有缺陷。我正在敲击的当前墙是currentNode到达树中最右边的子节点,在
中计算其.Left.Key的键。if (currentNode.Left.Key == prevNode.Left.Key)
并崩溃,因为.Left为空。
我实际上更接近了。我认为500点会被评估为“500 = 2 ^ 2 X 5 ^ 2 ^ 2”我认为(而不是理想的500 = 2 ^ 2 X 5 ^ 3)
以下是我的FactorTreeNode的代码:
class FactorTreeNode
{
// constants
// variables
private long data; // this is our only data, so also key
private FactorTreeNode leftPointer;
private FactorTreeNode rightPointer;
// these pointers point to the
// left and right children respectively
private FactorTreeNode parentPainter;
// pointer to the parent of the node
// constructors
public FactorTreeNode(long newValue)
{
Key = newValue;
Left = null;
Right = null;
Parent = null;
}
// Properties
public long Key
{
get
{
return data;
}
set
{
data = value;
}
}
public FactorTreeNode Left
{
get
{
return leftPointer;
}
set
{
leftPointer = value;
}
}
public FactorTreeNode Right
{
get
{
return rightPointer;
}
set
{
rightPointer = value;
}
}
public FactorTreeNode Parent
{
get
{
return parentPainter;
}
set
{
parentPainter = value;
}
}
}
我整天都在咀嚼这个。我感谢任何帮助。
答案 0 :(得分:1)
你会尝试这种方法
private void LabelDisplayCondensed(FactorTreeNode currentNode)
{
string result = Convert.ToString(root.Key) + " = " + Convert.ToString(currentNode.Left.Key);
FactorTreeNode prevNode = currentNode;
int exponent = 1;
while (currentNode.Right != null && currentNode.Left!= null)
{
prevNode = currentNode;
currentNode = currentNode.Right;
if ( currentNode.Left.Key == prevNode.Left.Key && currentNode.Right != null ) //updated *****************
{
exponent++;
continue;
}
else if (exponent != 1 )
{
result += " ^ " + exponent ;
exponent = 1;
}
result += " X " + currentNode.Left.Key;
}
MessageBox.Show(result);
}
答案 1 :(得分:1)
从看起来,你正在树下跑,并检查当前值是否等于前一个,以便计算一个因子出现的次数。
3x3x37
时会发生什么?你从3开始,下一次迭代你有3,所以你增加指数,下一次迭代你有37,所以你将指数设置为1 ......
你需要有一个像
这样的逻辑我假设您的节点已被订购,否则您将不得不在那里付出更多努力,但无论哪种方式,您都需要将x
的值添加到字符串中,即使它没有指数> 1,似乎你没有做ATM。
答案 2 :(得分:1)
假设您的FactorTreeNodeclass
是这样的:
class FactorTreeNode
{
public FactorTreeNode(long key) { this.Key = key; }
public FactorTreeNode Left { get; set; }
public FactorTreeNode Right { get; set; }
public long Key { get; private set; }
}
然后这将起作用:
private void LabelDisplayCondensed(FactorTreeNode root)
{
string result = Convert.ToString(root.Key) + " =";
long prevFactor = 0;
int exponent = 0;
bool firstFactor = true;
FactorTreeNode currentNode = root;
while (currentNode != null)
{
long nextFactor = currentNode.Left == null ?
currentNode.Key :
currentNode.Left.Key;
if (nextFactor != prevFactor && prevFactor != 0)
{
result += WriteFactor(prevFactor, exponent, ref firstFactor);
exponent = 1;
}
else
{
exponent++;
}
prevFactor = nextFactor;
currentNode = currentNode.Right;
}
result += WriteFactor(prevFactor, exponent, ref firstFactor);
MessageBox.Show(result);
}
private string WriteFactor(long factor, int exponent, ref bool firstFactor)
{
string result = firstFactor ? " " : " X ";
firstFactor = false;
if (exponent == 1)
{
result += factor.ToString();
}
else
{
result += factor.ToString() + " ^ " + exponent.ToString();
}
return result;
}
显然,这不包括检查树是否有效。
您可能还希望使用StringBuilder
来实际构建字符串,而不是执行所有这些追加。