Printing data from a Binary Search Tree.

Printing Data from a comparable inside a binary search tree.

Page 1 of 1

2 Replies - 3559 Views - Last Post: 14 June 2009 - 03:23 PM Rate Topic: -----

#1 Outdor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-June 09

Printing data from a Binary Search Tree.

Posted 14 June 2009 - 11:28 AM

My project is a binary search tree that holds Member Names and ID numbers sorted by ID Number. I am trying to make the tree code as reusable as posible so I wrote it to accept a comparable as the value for the data and created a comparable class for member data. My member data class also has a print method to output the data. WHat I need to know is how do I enable the tree to run the print method as print is not part of Comparable.

-Outdor

My MemberData Class
public class ClubMember implements Comparable
{

	//Class Properties
	public int memberID;				//Member ID Number
	public String memberName;		//Member Name
	
	
	//Constructor
	public ClubMember(int id, String name)
	{
		memberID = id;
		memberName = name;
	}
	
	//CompareTo Method
	public int compareTo(Object o1)
	{
		if ((this.memberID) > ((ClubMember) o1).memberID)
		{
			return 1;
		}
		else if ((this.memberID) < ((ClubMember) o1).memberID)
		{
			return -1;
		}
		else
		{
			return 0;
		}
	}
	
	//Print Method
	public void Print()
	{
		System.out.print("Member ID: " + memberID);
		System.out.println(" Member Name: " + memberName);
	}

}



My Binary Search Tree(so far)
public class BinarySearchTree
{

	//Node Structure
	class BinaryNode
	{
	
		//Node Properties
		Comparable data;		//The data
		BinaryNode left;			//Left child
		BinaryNode right;			//Right child
		
		//Node Constructor
		BinaryNode(Comparable theData)
		{
			data = theData;
			left = right = null;
		}
			
	}

	//Tree Root
	protected BinaryNode root;

	//Constructor
	public BinarySearchTree()
	{
		root = null;
	}
	
	//Insert Initilizer
	public void insert(Comparable c1)
	{
		root = insert( c1, root);
	}
	
	//Insert Method for adding new nodes.
	protected BinaryNode insert(Comparable c1, BinaryNode bn1)
	{
		if (bn1 == null)
		{
			bn1 = new BinaryNode(c1);
		}
		else if (c1.compareTo(bn1.data) < 0)
		{
			bn1.left = insert(c1, bn1.left);
		}
		else if (c1.compareTo(bn1.data) > 0)
		{
			bn1.right = insert(c1, bn1.right);
		}
		return bn1;
	}
	
	//PrintTree Initilize
	public void PrintTree()
	{
		PrintTree(root);
	}
	
	//PrintTree method for printing entire tree.
	protected void PrintTree(BinaryNode bn1)
	{
		//Left
		if (bn1.left != null)
		{
			PrintTree(bn1.left);
		}
		
		//Print
		bn1.data.Print(); // <---- problem Lies here
		
		//Right
		if (bn1.right != null)
		{
			PrintTree(bn1.right);
		}
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Printing data from a Binary Search Tree.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: Printing data from a Binary Search Tree.

Posted 14 June 2009 - 12:16 PM

As far as I can tell here, there is no reason to be making your data private data member of type Comparable. Now if you want to make it of something like type "Object" fine. But you never need to call compareTo on data.

Now where you DO WANT the interface comparable is on your BinaryNode class. The idea is that your insert will take in an Object and wrap it in a BinaryNode class first thing (through the BinaryNode constructor). This BinaryNode will implement a compareTo which will allow you to then compare two BinaryNode objects.

In the compareTo method you will then compare one BinaryNode's data member against the other BinaryNode's data member for equality. Doing it this way will also allow you to either extend the comparable interface into a new interface which includes Print() or you can just create your own interface with print and put both interfaces on BinaryNode. It will also allow you to fetch the data member through a property instead of accessing the data member directly which is better encapsulation practice anyhow.

But the main thing you want to take away is that you don't use compareTo on the private member, you compare the nodes themselves.

Edit: Another way to do this would be just to call its toString method and pass that to System.out.println or something but that won't give you much control of what or how it is printed. Use caution.

Hope that makes sense. :)

This post has been edited by Martyr2: 14 June 2009 - 12:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Printing data from a Binary Search Tree.

Posted 14 June 2009 - 03:23 PM

Be lazy...
make it

public class ClubMember implements Comparable<ClubMember>

then your compareTo method becomes a piece of cake (1 line of code)

	//CompareTo Method
	public int compareTo(ClubMember o1)
	{
		return this.memberID - o1.memberID;
	 }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1