I'm unsure if I am making my binary tree and node class properly or not. Are there better ways for the implementation of my BinaryTree and Node classes? I hope to be able to use these data structures to visually draw the tree in real time.
import javax.swing.JOptionPane;
public class Tree{
Node root = null;
Node leftNode = null;
Node rightNode = null;
// Returns the root node
public Node getRoot(){
return root;
}
// Sets the root node, if the tree is empty.
// Otherwise returns an error
public Node addRoot(String element, Node a){
if(root == null){
Node r = new Node(element, a);
root = r;
} else {
JOptionPane.showMessageDialog(null, this, "Error, root node is already present", 0);
}
return getRoot();
}
// Checks if there is a left node, if there isn't makes Node b the new left node with element
// as the value of the Node b. If there is a left node return an error
public Node insertLeft(Node b, String element){
// If there is no left node, make Node b the left node
if(b == null){
leftNode = new Node(element, b);
// If there is already a left node, return an error
} else {
JOptionPane.showMessageDialog(null, this, "Error, left node is already present", 0);
}
return leftNode;
}
// Checks if there is a right node, if there isn't makes Node c the new right node with element
// as the value of the Node c. If there is a right node return an error
public Node insertRight(Node c, String element){
// If there is no right node, make Node c the right node
if(c == null){
rightNode = new Node(element, c);
// If there is already a right node, return an error
} else {
JOptionPane.showMessageDialog(null, this, "Error, right node is already present", 0);
}
return rightNode;
}
// Removes Node d from the tree, if Node d has any children in the left
// or right branch of the subtree, replace the child node with Node d and
// return Node d
// Return an error if Node d has two child nodes.
public void removeNode(Node d){
if(d.getRight()==null&&d.getLeft()==null){
d = null;
} else if(d.getRight()!=null){
d = d.getRight();
} else if(d.getLeft()!=null){
d = d.getLeft();
} else if(d.getRight()!=null&&d.getLeft()!=null){
JOptionPane.showMessageDialog(null, this, "Error, cannot remove a node that has two child nodes", 0);
}
}
// Attaches subtree
public void attach(){
}
public class Node {
String element;
Node nextNode;
Node head, tail;
int size;
// Creates a node with the given element and next node
public Node(String s, Node n){
element = s;
nextNode = n;
}
// Returns the right node
public Node getRight(){
return rightNode;
}
// Returns the left node
public Node getLeft(){
return leftNode;
}
// Returns the element of the node
public String getElement(){
return element;
}
// Returns the next node of this node
public Node getNext(){
return nextNode;
}
// Sets the element of this node
public void setElement(String newElement){
element = newElement;
}
// Sets the next node of this node
public void setNext(Node newNext){
nextNode = newNext;
}
// Adds the node to the last node of the list
public void addFirst(Node first){
first.setNext(head); // Points to the previous head node
head = first; // Make the head node point to the new node
size = size + 1; // Increment the node count
}
// Adds the node to the last node of the list
public void addLast(Node last){
last.setNext(null); // Make the new node "last" point to null
tail.setNext(last); // Make the old tail node point to the new tail node
tail = last; // Make tail node point to new node
size = size + 1; // Increment the node count
}
// Removes the first node in the list. Before removing it points to the
// new head node then removes the old head node.
public void removeFirst(Node v){
if(head == null){
System.out.println("Error, cannot remove. The list is empty");
}
v = head;
head = head.getNext();
v.setNext(null);
size = size - 1;
}
}
}
Node
variables should be part of theNode
class. It looks like you are mixing a Binary Tree with a Doubly Linked List. Try moving theleftNode
andrightNode
into theNode
class, removenextNode
,head
,tail
, andsize
, then see where you get from there. Those variables I asked you to remove make no sense in a Binary Tree. – John McDonald Feb 22 at 16:49