Building a binary tree with a node sharing the same information
I wanted to build a binary tree in java where some nodes should be the same as in the other part.
For example, I have a tree on one side and a node on the other side (Set class). On the other side of the node is the node which will also be in the tree. And that a node in the tree can be multiple or single.
So basically, we have a node communicating with a tree. When I change the content of this node, it should also change the content in the tree! So I decided to write something as a proof of concept and couldn't do it ...
This is my code: Main class
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());
}
}
Install class
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();
}
}
Node Class
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();
}
}
And the result of my output from my main class is as follows:
x | ((x < 0 )&& (x >= 0 ))
(2 + 3 ) | ((x < 0 )&& (x >= 0 ))
Usually every "x" that appears in my function should be replaced with "2 + 3". Which is not replaced as shown in the picture ... What's wrong with my code? Should I be using a different architecture? Thank.
source to share
When you set the id, you forgot to set the head as well. The ID prints out correctly, and so does the head in the toStringSort function:
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();
}
The head is initialized with left and right, being a link to the nodes:
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);
This is what is printed in toStringSort, as these links are never touched.
One more thing about your implementation. I think you are making it too difficult. Why don't you build a tree with just nodes? You can easily build a tree in such a way that each node contains references to its father and his children. Now, when you change a node, every occurrence of that node changes, and since this is only confirmation, the node changes in each of them happens.
source to share