Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

binary tree ADT

Write a Java program to implement binary tree Abstract Data Type (ADT), by means of java.util.Vector class.
I do not know where to start!
:SICK:
[153 byte] By [Kinda Electroni] at [2007-11-11 7:54:56]
# 1 Re: binary tree ADT
I do not know where to start!
:SICK:

Start here:
http://java.sun.com/j2se/1.3/docs/api/java/util/Vector.html
prometheuzz at 2007-11-11 22:37:04 >
# 2 Re: binary tree ADT
First, thank you very much for the useful information that I really like.
I admit that I am a slow learner, I have to work hard at my JAVA, and I learn by examples more than lecture.
I was unable to make it works.

public class Vector (int a, int b){
int a = 10; int b = 5;

}// The End of Vector.java
Kinda Electroni at 2007-11-11 22:37:58 >
# 3 Re: binary tree ADT
public class Vector (int a, int b){
int a = 10; int b = 5;

}// The End of Vector.java

What are you doing here?
Vector is a java class from the java.util-package, you don't need to write it yourself.
When you write a class, you can pass parameters through it's methods or constructors. Something like this:
import java.util.Vector;
public class MyClass { // no (...) here!

Vector vector;

public MyClass() {
vector = new Vector();
}

publis void addAnIntToMyVector(int i) {
// you can't add primitive's to a Vector
// so wrap you int in an Integer
Ineteger temp = new Integer(i);
vector.add(temp);
}
}
prometheuzz at 2007-11-11 22:39:08 >
# 4 Re: binary tree ADT
binary tree and vector ( which is a 1-dimensional array ) are different structures.
maybe you need a special situation to handle ,
http://en.wikipedia.org/wiki/Binary_tree
look at the "Methods for storing binary trees" part ..
its simple to implement in case "complete binary tree",
otherwise it may be hard .
mr1yh1 at 2007-11-11 22:40:07 >
# 5 Re: binary tree ADT
I have done this using Linked Lists..

If you wanna look at it PM me or something...
Code_Nerd at 2007-11-11 22:41:00 >
# 6 Re: binary tree ADT
=Code_Nerd

If you wanna look at it PM me or something...
What do you mean?
I :confused: 100%
Kinda Electroni at 2007-11-11 22:42:11 >
# 7 Re: binary tree ADT
I have implemented a Binary Tree ADT using Linked Lists and not Vectors...
Code_Nerd at 2007-11-11 22:43:09 >
# 8 Re: binary tree ADT
Would you please email me a copy. I might be able to replcate your steps.
Kinda Electroni at 2007-11-11 22:44:12 >
# 9 Re: binary tree ADT
Would you please email me a copy. I might be able to replcate your steps.

PM sent as you havent put your email anywhere ;)
*Edit* PM is too long, will post it here..
public class BTNode
{
private Object data;
private BTNode left, right;
public BTNode(Object initialData, BTNode initialLeft, BTNode initialRight)
{
data = initialData;
left = initialLeft;
right = initialRight;
}
public BTNode()
{ this(null, null, null); }

public BTNode(Object newdata)
{ this(newdata, null, null); }

//NODE methods
public Object getData() //return and set data in node
{return data;}

public void setData(Object data)
{this.data = data; }

public BTNode getLeft() //return and set left child
{return left; }

public void setLeft(BTNode left)
{this.left = left; }

public BTNode getRight() //return and set right child
{return right; }

public void setRight(BTNode right)
{this.right = right; }

public boolean isLeaf() //return true if this is leaf node
{return (left == null)&&(right == null); }

// TREE Methods - for tree starting at this node
// get specific data from a tree

public Object getLeftmostData()
{ if(left == null)
return data;
else
return left.getLeftmostData(); }

public Object getRightmostData()
{ if(right == null)
return data;
else
return right.getRightmostData(); }
// print data in a tree
public void inorderPrint()
{
if (left != null)
left.inorderPrint( );
System.out.print(data+"\t");
if (right != null)
right.inorderPrint( ); }

public void preorderPrint()
{
System.out.print(data+"\t");
if (left != null)
left.preorderPrint( );
if (right != null)
right.preorderPrint( ); }

public void postorderPrint()
{
if (left != null)
left.postorderPrint( );
if (right != null)
right.postorderPrint( );
System.out.print(data+"\t"); }

// pretty print a tree
//public void print(int depth);

//Interface for Nodes in Binary Tree (3)
//removing nodes from a tree
//returns reference to tree after removal of node
public BTNode removeLeftmost()
{
if (left == null) // we are as deep as we can get
return right; // remove this node by returning right subt ree
else
{ // keep going down recursively
left = left.removeLeftmost( );
// when done, return node that act ivated this
// method as root of t ree
return this; }}

public BTNode removeRightmost()
{
if (right == null) // we are as deep as we can get
return left; // remove this node by returning left subt ree
else
{ // keep going down recursively
right = right.removeRightmost( );
// when done, return node that activated this
// method as root of tree
return this; }}
//returns number of nodes in tree
public static long treeHeight(BTNode root)
{
if (root == null)
return -1;
else
return 1 + Math.max(treeHeight(root.left),
treeHeight(root.right));
}

public static long treeSize(BTNode root)
{
if (root == null)
return 0;
else
return 1 + treeSize(root.left) + treeSize(root.right);
}
//copying a tree: returns reference to root of copy

// public BTNode treeCopy(BTNode root);

public static BTNode insertNode(BTNode root, int key){
if (root == null) // null tree, so create node at root
return new BTNode(new Integer(key));
Integer data_element = (Integer) root.data;
if (key <= data_element.intValue())
root.setLeft(insertNode(root.left, key));
else root.setRight(insertNode(root.right, key));
return root; }

public static BTNode findNode(BTNode root,int key){
if (root == null)
return null; // null tree
Integer data_element = (Integer) root.data;
if (key == data_element.intValue())
return root;
else
if (key <= data_element.intValue())
return findNode(root.left, key);
else return findNode(root.right, key); }

public static BTNode removeNode(BTNode root, int key){
if (root == null) return null;
Integer data_element = (Integer) root.data;
if (key < data_element.intValue())
root.setLeft(removeNode(root.left, key));
else if (key > data_element.intValue())
root.setRight(removeNode(root.right,key));
else { // found it
if (root.right == null) root = root.left;
else if (root.left == null) root = root.right;
else { //two children
Integer temp = (Integer)
root.left.getRightmostData();
root.setData(temp);
root.setLeft(root.left.removeRightmost());
}
}
return root; }

public static BTNode copyTree(BTNode bt){
if (bt==null) return null;
BTNode left=copyTree(bt.left);
BTNode right=copyTree(bt.right);
return new BTNode(bt.data,left,right); }

}

Im going away for 3 weeks tomorrow, so I cant help you till I get back if you need it..

Cheers
Code_Nerd at 2007-11-11 22:45:14 >
# 10 Re: binary tree ADT
Thank you.
Kinda Electroni at 2007-11-11 22:46:09 >