Hello,
we were taught some binary tree, I am thinking of creating my own...
though I am not sure if I am getting it right...
Node.java
BinaryTree.javaCode:
// This is a node class that should be used in a binary tree
public class Node
{
Object data; //stores the data in the node
Node parent; //stores the predecessor
Node left; // Left node
Node right; //right node
public Node()
{
data = null;
}
public Node(Object d)
{
setData(d);
}
public Node(Node l, Node p, Object d, Node r )
{
setLeft(l);
setRight(r);
setData(d);
setParent(p);
}
//sets the left node
public void setLeft(Node l)
{
left = l;
}
//returns the left node
public Node getLeft()
{
return left;
}
//sets the right node
public void setRight(Node r)
{
right = r;
}
//returns the right node
public Node getRight()
{
return right;
}
//sets the parent of the current node
public void setParent(Node p)
{
parent = p;
}
//returns the parent node
public Node getParent()
{
return parent;
}
//sets the content of the node
public void setData(Object d)
{
data= d;
}
//returns the content of the node
public Object getData()
{
return data;
}
//retuns true on false depending on whether the node has a left child
public boolean hasLeftChild()
{
if (left == null)
return false;
else
return true;
}
//returns true or false depending on whether the node has a right child
public boolean hasRightChild()
{
if(right == null)
return false;
else
return true;
}
//returns true or false depending on whether the node has a child
public boolean hasChild()
{
//if there is atleast one child, then it is ok
if(hasLeftChild() || hasRightChild() == true)
return true;
else
return false;
}
//returns true if the node has parent, else it returns false
public boolean hasParent()
{
if(parent == null)
return false;
else
return true;
}
//adds a left child
public void addLeft(Node l)
{
left = l;
}
//adds a right child
public void addRight(Node r)
{
right = r;
}
//adds a child to the tree, left by deafault
public void addChild(Node n)
{
//if the node already has a left child then add to the right else add to the left node
if(hasLeftChild())
{
addRight(n);
}else
{
addLeft(n);
}
}
//returns the node information
public String toString()
{
return String.format("* Node Data: %s", getData());
}
//Testing
public static void main(String args[])
{
Node me = new Node("Hello");
Node m = new Node("m");
Node e = new Node("e");
System.out.println(me.toString());
System.out.println(me.hasLeftChild());
me.addChild(m);
me.addChild(e);
System.out.println("me has a left child: "+ me.hasLeftChild());
System.out.println("me has a right child: " + me.hasRightChild());
if(me.hasLeftChild())
{
System.out.println(me.getLeft().toString());
}
if(me.hasRightChild())
{
System.out.println(me.getRight().toString());
}
}
}
Code:/**
* @(#)BinaryTree.java
*
*
* @author
* @version 1.00 2009/11/18
*/
import java.util.Vector;
public class BinaryTree<T> {
Vector<T> tree = new Vector<T>();
Node root; // The root node
Node parent;//parent of the current node
Node current; //current node
//Constructor
public BinaryTree(BinaryTree<T> preset)
{
setTree(preset);
}
//using vector
public BinaryTree(Vector<T> vals)
{
tree = vals;
}
//setting the tree
public void setTree(BinaryTree<T> t)
{
tree = t.tree;
}
//returns the current node
public Node getCurrentNode()
{
return current;
}
//moves to the next Node, This starts with the left child if it is a parent node
public void next()
{
if(isEmpty())
{
if (current == null) //if there is no current node
{
// start with parent node
current = root;
}else
{
if(current.hasChild()) // if there is a child, move to child
{
current = current.getLeft(); //pick left node
}else if(current.hasRightChild()) //else move to the right node
{
current = current.getRight();
}else//else if it has no child nor a right node move up
{
current = current.getParent();
}
}
}else
{
System.out.println("Tree is EMPTY");
}
}
//returns true if the tree is empty
public boolean isEmpty()
{
if(root == null)
return true;
else
return false;
}
//Moves to the right child of the current node
public void moveRight()
{
if(current.hasRightChild())
{
current = current.getRight();
}
}
//Moves to the Left Child of the current node
public void MoveLeft()
{
if(current.hasLeftChild())
{
current = current.getLeft();
}
}
//move to the parent node
public void moveUp()
{
if(current.hasParent())
{
current = current.getParent();
}
}
//searches for a node in the tree
public void searchNodes( BinaryTree<T> t, Node n )
{
}
//adds a node to the tree
public void addNode(BinaryTree<T> t, Node n)
{
}
//deletes a node from the tree
public void deleteNode(BinaryTree<T> t, Node n)
{
}
//TESTING
public static void main(String args[])
{
}
}
Thanks for the help..in Advance.!!! :)

