Java 构造节点共享相同信息的二叉树
我想用java构建一个二叉树,其中一些节点必须与另一部分中的节点相同 例如,我的树在一边,节点在另一边(Set类)。另一侧的节点是一个节点,该节点也将位于树上。树中的节点可以是多个或单个 所以,基本上,我们有一个与树共享信息的节点。 当我要更改该节点的内容时,它还必须更改树中的内容! 所以我决定写点东西作为概念的证明,但没能做到 这是我的代码: 主类Java 构造节点共享相同信息的二叉树,java,Java,我想用java构建一个二叉树,其中一些节点必须与另一部分中的节点相同 例如,我的树在一边,节点在另一边(Set类)。另一侧的节点是一个节点,该节点也将位于树上。树中的节点可以是多个或单个 所以,基本上,我们有一个与树共享信息的节点。 当我要更改该节点的内容时,它还必须更改树中的内容! 所以我决定写点东西作为概念的证明,但没能做到 这是我的代码: 主类 public class Main { public static void main(String [] args) {
public class Main {
public static void main(String [] args) {
Set s = new Set();
System.out.println(s.toString());
Node n1 = new Node(new String("2"),null,null);
Node n2 = new Node(new String("3"),null,null);
Node n3 = new Node(new String("+"),n1,n2);
s.setIdentifier(n3);
System.out.println(s.toString());
}
}
public class Set {
private Node identifier; // This is the node where is apart of the tree
private Node head; // This is the head of the tree
// Pre-Condição : Cria o conjunto de universo
public Set() {
Node id = new Node(new String("x"), null, null);
this.identifier = id;
Node left = new Node(new String("<"), this.identifier, new Node(new String("0"), null, null));
Node right = new Node(new String(">="), this.identifier, new Node(new String("0"), null, null));
this.head = new Node(new String("&&"), left, right);
}
// Pre-Condicao : Cria o conjunto vazio
public Set(String identifier) {
Node id = new Node(identifier);
this.identifier = id;
this.head = null;
}
// Pre-Condicao: Cria o conjunto dado
public Set(String identifier, Node h) {
Node id = new Node(identifier);
this.identifier = id;
this.head = h;
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public void setIdentifier(Node identifier) {
if (this.identifier != null) {
this.identifier = identifier;
}
}
public Node getIdentifier() {
return this.identifier;
}
public void isntASet() {
this.identifier = null;
}
public String toStringSort(String type) {
StringBuilder s = new StringBuilder();
if (this.identifier != null && this.head != null) {
s.append(this.identifier.toStringSort(type));
s.append(" | ");
s.append(this.head.toStringSort(type));
}
return s.toString();
}
public String toString() {
StringBuilder s = new StringBuilder();
if (this.identifier != null && this.head != null) {
s.append(this.toStringSort("infix"));
} else if (this.identifier != null && this.head == null) {
s.append("{");
s.append(this.identifier.toStringSort("infix"));
s.append(" | ");
s.append(" }");
} else {
s.append("Problem detected with the expression of the Set (not a boolean Expression).");
}
return s.toString();
}
}
public class Node {
private String data;
private Node left;
private Node right;
public Node(String data) {
this.data = data;
this.left = null;
this.right = null;
}
public Node(String data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
public Node(Node n) {
this.data = n.getData();
this.left = n.getLeft();
this.right = n.getRight();
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public boolean doesLeft() {
return (this.left != null) ? true : false;
}
public boolean doesRight() {
return (this.right != null) ? true : false;
}
public void setRight(Node right) {
this.right = right;
}
public Node clone() {
return new Node(this);
}
public String toStringSort(String type) {
StringBuilder s = new StringBuilder();
if (type.equals("prefix")) {
s.append(this.data.toString());
if (this.left != null) {
s.append(this.left.toStringSort("prefix"));
}
if (this.right != null) {
s.append(this.right.toStringSort("prefix"));
}
} else if (type.equals("infix")) {
if (this.left != null) {
s.append("(" + this.left.toStringSort("infix"));
}
s.append(this.data.toString() + " ");
if (this.right != null) {
s.append(this.right.toStringSort("infix") + ")");
}
} else if (type.equals("postfix")) {
if (this.left != null) {
s.append(this.left.toStringSort("postfix"));
}
if (this.right != null) {
s.append(this.right.toStringSort("postfix"));
}
s.append(this.data.toString());
}
return s.toString();
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(this.data.toString());
return s.toString();
}
}
设置类
public class Main {
public static void main(String [] args) {
Set s = new Set();
System.out.println(s.toString());
Node n1 = new Node(new String("2"),null,null);
Node n2 = new Node(new String("3"),null,null);
Node n3 = new Node(new String("+"),n1,n2);
s.setIdentifier(n3);
System.out.println(s.toString());
}
}
public class Set {
private Node identifier; // This is the node where is apart of the tree
private Node head; // This is the head of the tree
// Pre-Condição : Cria o conjunto de universo
public Set() {
Node id = new Node(new String("x"), null, null);
this.identifier = id;
Node left = new Node(new String("<"), this.identifier, new Node(new String("0"), null, null));
Node right = new Node(new String(">="), this.identifier, new Node(new String("0"), null, null));
this.head = new Node(new String("&&"), left, right);
}
// Pre-Condicao : Cria o conjunto vazio
public Set(String identifier) {
Node id = new Node(identifier);
this.identifier = id;
this.head = null;
}
// Pre-Condicao: Cria o conjunto dado
public Set(String identifier, Node h) {
Node id = new Node(identifier);
this.identifier = id;
this.head = h;
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public void setIdentifier(Node identifier) {
if (this.identifier != null) {
this.identifier = identifier;
}
}
public Node getIdentifier() {
return this.identifier;
}
public void isntASet() {
this.identifier = null;
}
public String toStringSort(String type) {
StringBuilder s = new StringBuilder();
if (this.identifier != null && this.head != null) {
s.append(this.identifier.toStringSort(type));
s.append(" | ");
s.append(this.head.toStringSort(type));
}
return s.toString();
}
public String toString() {
StringBuilder s = new StringBuilder();
if (this.identifier != null && this.head != null) {
s.append(this.toStringSort("infix"));
} else if (this.identifier != null && this.head == null) {
s.append("{");
s.append(this.identifier.toStringSort("infix"));
s.append(" | ");
s.append(" }");
} else {
s.append("Problem detected with the expression of the Set (not a boolean Expression).");
}
return s.toString();
}
}
public class Node {
private String data;
private Node left;
private Node right;
public Node(String data) {
this.data = data;
this.left = null;
this.right = null;
}
public Node(String data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
public Node(Node n) {
this.data = n.getData();
this.left = n.getLeft();
this.right = n.getRight();
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public boolean doesLeft() {
return (this.left != null) ? true : false;
}
public boolean doesRight() {
return (this.right != null) ? true : false;
}
public void setRight(Node right) {
this.right = right;
}
public Node clone() {
return new Node(this);
}
public String toStringSort(String type) {
StringBuilder s = new StringBuilder();
if (type.equals("prefix")) {
s.append(this.data.toString());
if (this.left != null) {
s.append(this.left.toStringSort("prefix"));
}
if (this.right != null) {
s.append(this.right.toStringSort("prefix"));
}
} else if (type.equals("infix")) {
if (this.left != null) {
s.append("(" + this.left.toStringSort("infix"));
}
s.append(this.data.toString() + " ");
if (this.right != null) {
s.append(this.right.toStringSort("infix") + ")");
}
} else if (type.equals("postfix")) {
if (this.left != null) {
s.append(this.left.toStringSort("postfix"));
}
if (this.right != null) {
s.append(this.right.toStringSort("postfix"));
}
s.append(this.data.toString());
}
return s.toString();
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(this.data.toString());
return s.toString();
}
}
我的主类输出的结果是:
x | ((x < 0 )&& (x >= 0 ))
(2 + 3 ) | ((x < 0 )&& (x >= 0 ))
x|((x<0)和&(x>=0))
(2+3)|((x<0)和&(x>=0))
通常,“我的函数”中出现的每个“x”都应替换为“2+3”。如图所示,未对其进行更换。。。
我的代码怎么了?我应该使用另一种架构吗?
谢谢。当您设置标识符时,您也忘了设置头部。正确打印出标识符,ToString排序函数中的标题也正确打印:
public String toStringSort(String type){
StringBuilder s = new StringBuilder();
if(this.identifier != null && this.head != null) {
s.append(this.identifier.toStringSort(type));
s.append(" | ");
s.append(this.head.toStringSort(type));
}
return s.toString();
}
头部初始化时,左侧和右侧是对节点的引用:
Node left = new Node(new String("<"),this.identifier, new Node(new String("0"),null,null));
Node right = new Node(new String(">="),this.identifier, new Node(new String("0"),null,null));
this.head = new Node(new String("&&"), left,right);
Node left=新节点(新字符串(“=”),this.identifier,新节点(新字符串(“0”),null,null));
this.head=新节点(新字符串(&&&),左,右);
这是toStringSort中打印出来的内容,因为这些引用从未接触过
关于您的实现还有一件事。我觉得你把事情弄得太复杂了。为什么不直接用节点来构建树呢?您可以通过每个节点都包含对其父节点和子节点的引用的方式轻松构建树。
现在,当您更改一个节点时,该节点的每一次出现都会更改,并且由于它只是一个引用,所以该节点在每一次出现时都会更改。出于好奇:为什么要使用
String.(String)
来复制文本?你可以使用Node.(String,Node,Node)
在这里你可以简单地使用Node.(String)
…你在说什么?哪一类?它在Main
和Set
类中整体存在,不需要注意的是StringBuilder
中有更多奇怪的情况,请解释一下。。。如果我做错了StringBuilder,请告诉我出了什么问题。。。关于新字符串(“x”)
,没有问题。。。我当然可以省略。。。但这不是问题:)我不明白。。。你能给我举个例子吗?