- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在自下而上构建二叉树时遇到问题。树的输入将是树的内部节点,该节点的子节点是最终树的叶子。
所以最初如果树是空的,根将是第一个内部节点。
之后,下一个要添加的内部节点将是新根(NR),旧根(OR)是 NR 的子节点之一。等等。
我遇到的问题是,每当我添加 NR 时,OR 的子项似乎在我进行中序遍历时丢失。事实证明,当我执行 getSize() 调用时,它在 addNode(Tree,Node) 之前和之后返回相同数量的节点
感谢任何解决此问题的帮助
编辑包含节点类代码。树类和节点类都有 addChild 方法,因为我不太确定将它们放在哪里才能被使用。对此的任何评论也将不胜感激。
代码如下:
import java.util.*;
<p>public class Tree {</p>
<pre><code>Node root;
int size;
public Tree() {
root = null;
}
public Tree(Node root) {
this.root = root;
}
public static void setChild(Node parent, Node child, double weight) throws ItemNotFoundException {
if (parent.child1 != null && parent.child2 != null) {
throw new ItemNotFoundException("This Node already has 2 children");
} else if (parent.child1 != null) {
parent.child2 = child;
child.parent = parent;
parent.c2Weight = weight;
} else {
parent.child1 = child;
child.parent = parent;
parent.c1Weight = weight;
}
}
public static void setChild1(Node parent, Node child) {
parent.child1 = child;
child.parent = parent;
}
public static void setChild2(Node parent, Node child) {
parent.child2 = child;
child.parent = parent;
}
public static Tree addNode(Tree tree, Node node) throws ItemNotFoundException {
Tree tree1;
if (tree.root == null) {
tree.root = node;
} else if (tree.root.getSeq().equals(node.getChild1().getSeq()) ||
tree.root.getSeq().equals(node.getChild2().getSeq())) {
Node oldRoot = tree.root;
oldRoot.setParent(node);
tree.root = node;
} else { //form a disjoint tree and merge the 2 trees
tree1 = new Tree(node);
tree = mergeTree(tree, tree1);
}
System.out.print("addNode2 = ");
if(tree.root != null ) {
Tree.inOrder(tree.root);
}
System.out.println();
return tree;
}
public static Tree mergeTree(Tree tree, Tree tree1) {
String root = "root";
Node node = new Node(root);
tree.root.setParent(node);
tree1.root.setParent(node);
tree.root = node;
return tree;
}
public static int getSize(Node root) {
if (root != null) {
return 1 + getSize(root.child1) + getSize(root.child2);
} else {
return 0;
}
}
public static boolean isEmpty(Tree Tree) {
return Tree.root == null;
}
public static void inOrder(Node root) {
if (root != null) {
inOrder(root.child1);
System.out.print(root.sequence + " ");
inOrder(root.child2);
}
}
</code></pre>
<p>}</p>
公共(public)类节点{
Node child1;
Node child2;
Node parent;
double c1Weight;
double c2Weight;
String sequence;
boolean isInternal;
public Node(String seq) {
sequence = seq;
child1 = null;
c1Weight = 0;
child2 = null;
c2Weight = 0;
parent = null;
isInternal = false;
}
public boolean hasChild() {
if (this.child1 == null && this.child2 == null) {
this.isInternal = false;
return isInternal;
} else {
this.isInternal = true;
return isInternal;
}
}
public String getSeq() throws ItemNotFoundException {
if (this.sequence == null) {
throw new ItemNotFoundException("No such node");
} else {
return this.sequence;
}
}
public void setChild(Node child, double weight) throws ItemNotFoundException {
if (this.child1 != null && this.child2 != null) {
throw new ItemNotFoundException("This Node already has 2 children");
} else if (this.child1 != null) {
this.child2 = child;
this.c2Weight = weight;
} else {
this.child1 = child;
this.c1Weight = weight;
}
}
public static void setChild1(Node parent, Node child) {
parent.child1 = child;
child.parent = parent;
}
public static void setChild2(Node parent, Node child) {
parent.child2 = child;
child.parent = parent;
}
public void setParent(Node parent){
this.parent = parent;
}
public Node getParent() throws ItemNotFoundException {
if (this.parent == null) {
throw new ItemNotFoundException("This Node has no parent");
} else {
return this.parent;
}
}
public Node getChild1() throws ItemNotFoundException {
if (this.child1 == null) {
throw new ItemNotFoundException("There is no child1");
} else {
return this.child1;
}
}
public Node getChild2() throws ItemNotFoundException {
if (this.child2 == null) {
throw new ItemNotFoundException("There is no child2");
} else {
return this.child2;
}
}
最佳答案
只看附加的代码,找不到显式调用的 setChild
,只有 setParent
。如果您还附加了 setParent
的代码,那么我们可以确认该方法是否也创建了从父级到子级的反向链接。
我的猜测是 setParent
不会 setChild
,在这种情况下,到子项的链接并没有丢失:它们从一开始就没有创建。
//编辑后:
好的,现在可以确认 setParent
不会 setChild
,所以您一开始就没有创建这些链接。您应该增加以下形式的所有调用:
someNode.setParent(otherNode);
还有:
otherNode.setChild(someNode);
其他备注:
setChild
可以通过调用 setChild1
和 setChild2
来减少重复。setChild
中可以免费使用哪个链接的逻辑是正确的,但令人困惑,即“如果另一个不免费,则使用这个”,而不是更直接的“如果这个是免费的,就用它吧。”addNode
中的 tree1
可以在使用它的一个 block 内声明。事实上,它甚至可以完全从代码中写出来,例如tree = merge(tree, new Tree(node));
.node.getChild1().getSeq()
是否容易导致抛出 NullPointerException
?
ItemNotFoundException
。在任何一种情况下,您在 addNode
中的原始条件都是错误的,因为您不能保证两个子节点都存在(因为那样您将无法向其添加子节点)。Tree
中 Node.setChild*
的功能复制为 static
方法...然后两者都不使用?关于java - 自下而上构建树的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2597301/
看起来很简单,但我没有任何成功。 非常简单,使用 AHK,我想从下往上获取工作表中最后一行的编号,其中包含一个值。我不能自上而下,因为有些行是空白的,所以必须自下而上。 我的代码遍历选定文件夹中的所有
元素的合并排序过程步骤是什么:20 47 15 8 9 4 40 30 12 17 我遇到过这个...... Pass1: |20 47| |8 15| |4 9| |30 40| |12 17| P
我正在尝试将脚本添加到我网站上的一个页面,这是一种过渡效果,其中 div 在 View 中从下向上移动。我成功地将完全相同的脚本添加到另一个页面并且它有效,但由于某种原因,它在另一个页面上不起作用。我
我正在使用 WIC (Windows Imaging Component) 来解码图像文件并访问像素数据。我试图找出像素顺序(即自下而上或自上而下)。 我用 IWICImagingFactory::C
我想在 Reporting Services 的文本框中垂直自下而上地显示我的文本。我已经可以通过转到文本框的 WritingMode 属性并切换到 'tb-rl' 使其自上而下,但没有自下而上的选项
我是一名优秀的程序员,十分优秀!