2 Replies - 596 Views - Last Post: 03 April 2012 - 12:19 PM Rate Topic: -----

#1 cr9r   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-March 12

i don't know how to start my program .. plz every body

Posted 03 April 2012 - 12:02 PM

hello every body ..
i have a problem that i don't know how to build a binary search tree from a word from txt file ..

for example


cat dog
boy girl
man women


it will be like this ..

-----------cat dog---------
-----boy girl------man women-------

every two word in one node ..

iam a beginer in java .. plz help me
Is This A Good Question/Topic? 0
  • +

Replies To: i don't know how to start my program .. plz every body

#2 eikkaj   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 119
  • Joined: 20-December 08

Re: i don't know how to start my program .. plz every body

Posted 03 April 2012 - 12:09 PM

So what code do you have so far?
Was This Post Helpful? 0
  • +
  • -

#3 cr9r   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-March 12

Re: i don't know how to start my program .. plz every body

Posted 03 April 2012 - 12:19 PM

View Posteikkaj, on 03 April 2012 - 12:09 PM, said:

So what code do you have so far?


this code to read from a file ..

import java.io.*;
class FileRead 
{
   public static void main(String args[])
	{
      try{
	
		FileInputStream fstream = new FileInputStream("textfile.txt");
		
		DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
	
		while ((strLine = br.readLine()) != null) 	{
		
			
			System.out.println (strLine);
		}
	
		in.close();
		}catch (Exception e){
			System.err.println("Error: " + e.getMessage());
		}
	}
}







and this one for the binary search tree ..

ublic class BinaryTreeExample {

  public static void main(String[] args)
    {
  new BinaryTreeExample().run();
  }

  static class Node 

   {

  Node left;
  Node right;
  int value;

  public Node(int value) {
  this.value = value;
  }
  }

  public void run() {
  Node rootnode = new Node(25);
  System.out.println("Building tree with rootvalue 
" + rootnode.value);
  System.out.println("=======================
  ==========");
  insert(rootnode, 11);
  insert(rootnode, 15);
  insert(rootnode, 16);
  insert(rootnode, 23);
  insert(rootnode, 79);
  System.out.println("Traversing tree in order");
  System.out.println("========================
  =========");
  printInOrder(rootnode);

  }

  public void insert(Node node, int value) {
  if (value < node.value) {
  if (node.left != null) {
  insert(node.left, value);
  } else {
  System.out.println("  Inserted " + value + 
    " to left of node " + node.value);
  node.left = new Node(value);
  }
  } else if (value > node.value) {
  if (node.right != null) {
  insert(node.right, value);
  } else {
  System.out.println("  Inserted " + value + " 
  to right of node " + node.value);
  node.right = new Node(value);
  }
  }
  }

  public void printInOrder(Node node) {
  if (node != null) {
  printInOrder(node.left);
  System.out.println("  Traversed " + node.value);
  printInOrder(node.right);
  }
  }
}




i don't know how to rewrite the 2 codes to do the work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1