473,421 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Binary search tree implementation problem

I'm doing an assignment for a Data Structures class, and I'm supposed to to implement a binary search tree with a client side method to create the tree. I've gotten most of it running I think, based on some debugging lines I added to track which part of the code has run. This is my class.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. public class BinarySearch {
  3.     public static void main(String[] args) {
  4.         ArrayList<String> dict = new ArrayList<String>();
  5.         BSTree<String> searchTree = new BSTree<String>();
  6.         FileInputStream fstream = null;
  7.         BufferedInputStream bis = null;
  8.         DataInputStream dis = null;
  9.         BufferedReader br = null;
  10.         StopWatch time = new StopWatch();
  11.         long wordCount = 0;
  12.         long wordsInDict = 0;
  13.  
  14.         try {
  15.             System.out.print("Enter the dictionary's name:");
  16.             br = new BufferedReader(new InputStreamReader(System.in));
  17.             String name = null;
  18.             name = br.readLine();
  19.             fstream = new FileInputStream(name);
  20.             bis = new BufferedInputStream(fstream);
  21.             dis = new DataInputStream(bis);
  22.             while (dis.available() != 0) {
  23.                 dict.add(dis.readLine());
  24.             }
  25.             System.out.println("Dict size " + dict.getLength());
  26.             createTree(dict, searchTree);
  27.             System.out.println("List has been successfully built!\n");
  28.  
  29.             System.out.print("Enter the file's name:");
  30.             br = new BufferedReader(new InputStreamReader(System.in));
  31.             name = br.readLine();
  32.             fstream = new FileInputStream(name);
  33.             bis = new BufferedInputStream(fstream);
  34.             dis = new DataInputStream(bis);
  35.             System.out.println("\nSearching for words in the file: " + name);
  36.             time.start();
  37.             while (dis.available() != 0) {
  38.                 String line = dis.readLine();
  39.                 String[] words = line.split(" ");
  40.                 wordCount += words.length;
  41.                 for (String w : words) {
  42.                     w.toLowerCase();
  43.                     if (searchTree.contains(w))
  44.                         wordsInDict++;
  45.                 }
  46.             }
  47.             time.stop();
  48.         } catch (FileNotFoundException e) {
  49.             System.out.println("File not found");
  50.         } catch (IllegalArgumentException e) {
  51.             System.out.println("Size is less than 0");
  52.         } catch (IOException e) {
  53.             System.out.println("Read error");
  54.         }
  55.  
  56.         System.out.println("Number of tokens = " + wordCount);
  57.         System.out.println("Number of tokens in dictionary = " + wordsInDict);
  58.         System.out.println(time);
  59.     }
  60.  
  61.     public static <T extends Comparable<? super T>> 
  62.        void createTree(ArrayList<T> list, BSTree<T> tree) {
  63.         tree.add(list.getEntry(list.getLength()/2));
  64.         System.out.println("Element added");
  65.         int leftSize = list.getLength()/2;
  66.         int rightSize = leftSize;
  67.         if ((list.getLength() % 2) == 0)
  68.             rightSize -= 1;
  69.         System.out.println("Sizes determined");
  70.         ArrayList<T> leftSub = new ArrayList<T>();
  71.         ArrayList<T> rightSub = new ArrayList<T>();
  72.         System.out.println("Sublists defined");
  73.         for (int i = 0; i < leftSize; i++) {
  74.             leftSub.add(list.getEntry(i));
  75.         }
  76.         System.out.println("Left sub filled");
  77.         for (int i = leftSize + 1; i < list.getLength(); i++) {
  78.             rightSub.add(list.getEntry(i));
  79.         }
  80.         System.out.println("Right sub filled");
  81.         createTree(leftSub, rightSub, tree);
  82.     }
  83.  
  84.     private static <T extends Comparable<? super T>>
  85.        void createTree(ArrayList<T> leftTree, ArrayList<T> rightTree, BSTree<T> tree) {
  86.         if ((leftTree.getLength() > 0) && (rightTree.getLength() > 0)) {
  87.            ArrayList<T> leftSub1 = new ArrayList<T>();
  88.            ArrayList<T> rightSub1 = new ArrayList<T>();
  89.            System.out.println("2L sublists defined");
  90.            ArrayList<T> leftSub2 = new ArrayList<T>();
  91.            ArrayList<T> rightSub2 = new ArrayList<T>();
  92.            System.out.println("2R sublists defined");
  93.            System.out.println("2L Element " + leftTree.getEntry(leftTree.getLength()/2));
  94.            tree.add(leftTree.getEntry(leftTree.getLength()/2));
  95.  
  96.            int leftSize1 = leftTree.getLength()/2;
  97.            int rightSize1 = leftSize1;
  98.            if ((leftTree.getLength() % 2 == 0))
  99.                rightSize1 -= 1;
  100.            System.out.println("2L Sizes determined");
  101.  
  102.  
  103.            for (int i = 0; i < leftSize1; i++) {
  104.                leftSub1.add(leftTree.getEntry(i));
  105.            }
  106.            System.out.println("2L leftSub size " + leftSub1.getLength());
  107.            for (int i = leftSize1 + 1; i < leftTree.getLength(); i++) {
  108.                rightSub1.add(leftTree.getEntry(i));
  109.            }
  110.            System.out.println("2L rightSub size " + rightSub1.getLength());
  111.  
  112.  
  113.            System.out.println("2R Element " + rightTree.getEntry(rightTree.getLength()/2));
  114.            tree.add(rightTree.getEntry(rightTree.getLength()/2));
  115.  
  116.            int leftSize2 = rightTree.getLength()/2;
  117.            int rightSize2 = leftSize2;
  118.            if ((rightTree.getLength() % 2 == 0))
  119.                rightSize2 -= 1;
  120.            System.out.println("2R Sizes determined");
  121.  
  122.  
  123.            for (int i = 0; i < leftSize2; i++) {
  124.                leftSub2.add(rightTree.getEntry(i));
  125.            }
  126.            System.out.println("2R leftSub size " + leftSub2.getLength());
  127.            for (int i = leftSize2 + 1; i < rightTree.getLength(); i++) {
  128.                rightSub2.add(rightTree.getEntry(i));
  129.            }
  130.            System.out.println("2R rightSub size " + rightSub2.getLength());
  131.  
  132.  
  133.            createTree(leftSub1, rightSub1, tree);
  134.            createTree(leftSub2, rightSub2, tree);
  135.         }
  136.     }
  137. }
  138.  
This is the output I get from the console.

Expand|Select|Wrap|Line Numbers
  1. Enter the dictionary's name:dict8.txt
  2. Dict size 17272
  3. Element added
  4. Sizes determined
  5. Sublists defined
  6. Left sub filled
  7. Right sub filled
  8. 2L sublists defined
  9. 2R sublists defined
  10. 2L Element descend
  11. 2L Sizes determined
  12. 2L leftSub size 4318
  13. 2L rightSub size 4317
  14. 2R Element roland
  15. 2R Sizes determined
  16. 2R leftSub size 4317
  17. 2R rightSub size 4317
  18. 2L sublists defined
  19. 2R sublists defined
  20. 2L Element brenda
  21.  
It looks like it managed to complete the first run through the createTree method, but then it just stopped at the tree.add statement the second time through. The method has been defined by my instructor so I cannot change anything there. What may be the problem here?
Oct 14 '10 #1
0 1686

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Henry Jordon | last post by:
I have everything pretty much done but I need to fix something in my coding. I want to be able to enter strings such as "love", "hate", "the", etc. but am unable to figure how to do this. I have...
0
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a...
3
by: tsunami | last post by:
hi all; I have an array and want to insert all the elements from this array to a binary search tree.That array is an object of the class of a stringtype which includes overloaded "< > = =="...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
1
by: mathon | last post by:
hi, now i facing a problem which i do not know how to solve it...:( My binary search tree structures stores a double number in every node, whereby a higher number is appended as right child...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
5
gekko3558
by: gekko3558 | last post by:
I am writing a simple binary search tree (nodes are int nodes) with a BSTNode class and a BST class. I have followed the instructions from my C++ book, and now I am trying to get a remove method...
11
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
36
Ganon11
by: Ganon11 | last post by:
OK, first of all, thanks to everyone who helped me out with my Isomorphism problem - it finally works. Now, the other part of my homework I'm having trouble with is this: I don't want to post...
1
by: yogi_bear_79 | last post by:
I am enrolled in distance learning class, this amounts to self taught. I have a book and that is about it. below is my assingment. The book doesn't prove useful for examples, and I haven't had...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.