Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-30-2008, 06:13 PM
Member
 
Join Date: Apr 2008
Posts: 2
bigdoggy is on a distinguished road
tree sort
Hi guys,

I need to use tree sort in order to sort through an ordered binary tree. I can't find any useful info on it and was looking for some pointers.

Thanks in advance
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-30-2008, 10:06 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
I am not sure what you mean... ordered means sorted.
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-30-2008, 10:40 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
by sort do you mean search.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-30-2008, 11:43 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Here are a couple of possible resources:
Scroll down to Section 4 on Binary Trees.
Section 11.4 Binary Trees.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-01-2008, 02:11 AM
Member
 
Join Date: Apr 2008
Posts: 2
bigdoggy is on a distinguished road
Hi guys

Thanks for the replies. I have a class
Code:
import java.util.*; public class OBTComparable <T extends Comparable<T>> { private T value; private OBTComparable left; private OBTComparable right; private boolean empty; //create an empty tree public OBTComparable() { setEmpty(); } //make this tree into an empty tree private void setEmpty() { empty = true; value = null; left = null; right = null; } //see if this is an empty (sub)tree public boolean isEmpty() { return empty; } //get the value which is here public T getValue() { return value; } //get the left sub tree public OBTComparable getLeft() { return left; } //get the right sub tree public OBTComparable getRight() { return right; } //store a value in this position in the tree private void setValue(T requiredValue) { if(empty) { empty = false; left = new OBTComparable(); //makes new empty tree right = new OBTComparable(); //makes new empty tree } value = requiredValue; } //insert a value, allowing multiple instances public void insert(T insertValue) { if(empty) setValue(insertValue); else if(value.compareTo(insertValue)==1) left.insert(insertValue); else right.insert(insertValue); } public boolean find(T findValue) { if(empty) return false; else if(findValue==value) return true; else if(findValue.compareTo(value)==-1) return left.find(findValue); else return right.find(findValue); } public int getSize() { if(empty) return 0; else return (left.getSize() + 1 + right.getSize()); } public int getDepth() { if(empty) return 0; else { int leftDepth = left.getDepth(); int rightDepth = right.getDepth(); if(leftDepth > rightDepth) return leftDepth +1; else return rightDepth +1; } } public Iterator elementsAscending() { ArrayList<T> elementsList = new ArrayList<T>(); addElementsAscending(elementsList); return elementsList.iterator(); } private void addElementsAscending(List elementsList) { if(!empty) { left.addElementsAscending(elementsList); elementsList.add(value); right.addElementsAscending(elementsList); } } }
and now what I need to do is write a separate class which takes a list of strings from standard input and produce an ascending,sorted version of the list using tree sort, and I don't know how to use my class above combined with the tree sort...?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-01-2008, 07:52 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.util.*; public class BTTest { public static void main(String[] args) { String[] strs = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; OBTComparable root = new OBTComparable(); for(int i = 0; i < strs.length; i++) { root.insert(strs[i]); } Iterator elements = root.elementsAscending(); while(elements.hasNext()) { System.out.print(elements.next() + " "); } System.out.println(); } } class OBTComparable <T extends Comparable<T>> { private T value; private OBTComparable left; private OBTComparable right; private boolean empty; //create an empty tree public OBTComparable() { setEmpty(); } //make this tree into an empty tree private void setEmpty() { empty = true; value = null; left = null; right = null; } //see if this is an empty (sub)tree public boolean isEmpty() { return empty; } //get the value which is here public T getValue() { return value; } //get the left sub tree public OBTComparable getLeft() { return left; } //get the right sub tree public OBTComparable getRight() { return right; } //store a value in this position in the tree private void setValue(T requiredValue) { if(empty) { empty = false; left = new OBTComparable(); //makes new empty tree right = new OBTComparable(); //makes new empty tree } value = requiredValue; } //insert a value, allowing multiple instances public void insert(T insertValue) { insert(this, insertValue); /* if(empty) setValue(insertValue); else if(value.compareTo(insertValue)==1) left.insert(insertValue); else right.insert(insertValue); */ } private void insert(OBTComparable<T> node, T insertValue) { if(node.isEmpty()) { node.setValue(insertValue); } else { if(insertValue.compareTo(node.getValue()) < 0) insert(node.left, insertValue); else insert(node.right, insertValue); } } public boolean find(T findValue) { if(empty) return false; else if(findValue==value) return true; else if(findValue.compareTo(value)==-1) return left.find(findValue); else return right.find(findValue); } public int getSize() { if(empty) return 0; else return (left.getSize() + 1 + right.getSize()); } public int getDepth() { if(empty) return 0; else { int leftDepth = left.getDepth(); int rightDepth = right.getDepth(); if(leftDepth > rightDepth) return leftDepth +1; else return rightDepth +1; } } public Iterator elementsAscending() { List<T> elementsList = new ArrayList<T>(); addElementsAscending(elementsList); return elementsList.iterator(); } private void addElementsAscending(List elementsList) { if(!empty) { left.addElementsAscending(elementsList); elementsList.add(value); right.addElementsAscending(elementsList); } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort a list using Bubble sort algorithm Java Tip Algorithms 3 04-29-2008 10:04 PM
Need Help With Tree Adt avi New To Java 0 03-20-2008 05:11 AM
Pakage tree Thewhitelynx New To Java 1 03-17-2008 04:53 AM
Creating a tree Preethi AWT / Swing 0 01-07-2008 03:04 PM
Tree controls using Swing kabir AWT / Swing 1 01-05-2008 11:48 AM


All times are GMT +3. The time now is 05:43 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org