1 Replies - 415 Views - Last Post: 20 July 2009 - 09:13 AM Rate Topic: -----

#1 Potato Sauce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-July 09

small java problem

Post icon  Posted 20 July 2009 - 01:22 AM

hi guys, just wondering if anyone could help me with this little problem? I've got most of the code working but can't find why my program doesn't like my BinarySortTree declaration.

The program is meant to take input string from the command line, and sort the input into alphabetical order (not case dependant). Its a modification from my homework code for uni where it sorts input numbers into ascending and descending order which worked as specified.

here is the code I have so far

import java.io.*;
import java.util.*;

public class BinaryTree {
	public static String main(String[] args) throws IOException{
		String str = null;
		boolean root = false;
		BinarySortTree tree = new BinarySortTree();
		
		if(str != null){
			try {
				
				System.out.println("In ascending order: ");
				tree.printAscending(tree);
				System.out.println("In descending order: ");
				tree.printDescending(tree);
				System.out.println("Sort completed successfully \n Exit");
				System.exit(1);
			}
			return str;
		}
		
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		do {
			str = br.readLine();
			if(root == false){
				root = true;
				tree.element = str;
			} else {
				tree.insert(str, tree);
			}
		} while (str.length() != -1);
		  return str;
	}
	
	class BinarySortTree {
		public String element;
		BinarySortTree left;
		BinarySortTree right;
		
		public  BinarySortTree(String theElement){
			element = theElement;
			left = right = null;
		}
	public void printAscending(BinarySortTree tree){
		if(tree.left != null){
			printAscending(tree.left);
		}
		System.out.println(tree.element);
		if(tree.right != null){
			printAscending(tree.right);
		}
	}
	public void printDescending(BinarySortTree tree){
		if(tree.right != null) {
			printDescending(tree.right);
		}
		System.out.println(tree.element);
		if(tree.left != null) {
			printDescending(tree.left);
		}
	}
	public void insert(String str, BinarySortTree tree){
		if(str.compareToIgnoreCase(tree.element) <= 0 && tree.left != null)
			insert(str, tree.left);
		else if(str.compareToIgnoreCase(tree.element) > 0 && tree.right != null)
			insert(str, tree.right);
		else if(str.compareToIgnoreCase(tree.element) <= 0 && tree.left == null)
			tree.left = new BinarySortTree(str);
		else if(str.compareToIgnoreCase(tree.element) > 0 && tree.right == null)
			tree.right = new BinarySortTree(str);
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: small java problem

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: small java problem

Posted 20 July 2009 - 09:13 AM

BinarySortTree tree = new BinarySortTree();
this wont work because BinarySortTree is looking for 1 arg (String theElement) in its constructor
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1