Results 1 to 13 of 13
  1. #1
    itman95 is offline Member
    Join Date
    Nov 2014
    Posts
    7
    Rep Power
    0

    Default Importing values from a text file into a Binary Search Tree

    Hi all I need help importing values from a text file into a Binary Search Tree. Currently my tree is hard coded w/ values in the BTreePrinterTest Class. Please see below.

    BTreePrinter Class:
    Java Code:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    class BTreePrinter {
    
        public static <T extends Comparable<?>> void printNode(Node<T> root) {
            int maxLevel = BTreePrinter.maxLevel(root);
    
            printNodeInternal(Collections.singletonList(root), 1, maxLevel);
        }
    
        private static <T extends Comparable<?>> void printNodeInternal(List<Node<T>> nodes, int level, int maxLevel) {
            if (nodes.isEmpty() || BTreePrinter.isAllElementsNull(nodes))
                return;
    
            int floor = maxLevel - level;
            int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
            int firstSpaces = (int) Math.pow(2, (floor)) - 1;
            int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;
    
            BTreePrinter.printWhitespaces(firstSpaces);
    
            List<Node<T>> newNodes = new ArrayList<Node<T>>();
            for (Node<T> node : nodes) {
                if (node != null) {
                    System.out.print(node.data);
                    newNodes.add(node.left);
                    newNodes.add(node.right);
                } else {
                    newNodes.add(null);
                    newNodes.add(null);
                    System.out.print(" ");
                }
    
                BTreePrinter.printWhitespaces(betweenSpaces);
            }
            System.out.println("");
    
            for (int i = 1; i <= endgeLines; i++) {
                for (int j = 0; j < nodes.size(); j++) {
                    BTreePrinter.printWhitespaces(firstSpaces - i);
                    if (nodes.get(j) == null) {
                        BTreePrinter.printWhitespaces(endgeLines + endgeLines + i + 1);
                        continue;
                    }
    
                    if (nodes.get(j).left != null)
                        System.out.print("/");
                    else
                        BTreePrinter.printWhitespaces(1);
    
                    BTreePrinter.printWhitespaces(i + i - 1);
    
                    if (nodes.get(j).right != null)
                        System.out.print("\\");
                    else
                        BTreePrinter.printWhitespaces(1);
    
                    BTreePrinter.printWhitespaces(endgeLines + endgeLines - i);
                }
    
                System.out.println("");
            }
    
            printNodeInternal(newNodes, level + 1, maxLevel);
        }
    
        private static void printWhitespaces(int count) {
            for (int i = 0; i < count; i++)
                System.out.print(" ");
        }
    
        private static <T extends Comparable<?>> int maxLevel(Node<T> node) {
            if (node == null)
                return 0;
    
            return Math.max(BTreePrinter.maxLevel(node.left), BTreePrinter.maxLevel(node.right)) + 1;
        }
    
        private static <T> boolean isAllElementsNull(List<T> list) {
            for (Object object : list) {
                if (object != null)
                    return false;
            }
    
            return true;
        }
    
    }
    Node Class
    Java Code:
    class Node<T extends Comparable<?>> {
        Node<T> left, right;
        T data;
    
        public Node(T data) {
            this.data = data;
        }
    }
    BTreePrinterTest Class
    Java Code:
    import java.io.*;
    
    public class BTreePrinterTest {
    
        private static Node<Integer> test1() {
            Node<Integer> root = new Node<Integer>(2);
            Node<Integer> n11 = new Node<Integer>(7);
            Node<Integer> n12 = new Node<Integer>(5);
            Node<Integer> n21 = new Node<Integer>(2);
            Node<Integer> n22 = new Node<Integer>(6);
            Node<Integer> n23 = new Node<Integer>(3);
            Node<Integer> n24 = new Node<Integer>(6);
            Node<Integer> n31 = new Node<Integer>(5);
            Node<Integer> n32 = new Node<Integer>(8);
            Node<Integer> n33 = new Node<Integer>(4);
            Node<Integer> n34 = new Node<Integer>(5);
            Node<Integer> n35 = new Node<Integer>(8);
            Node<Integer> n36 = new Node<Integer>(4);
            Node<Integer> n37 = new Node<Integer>(5);
            Node<Integer> n38 = new Node<Integer>(8);
            
            root.left = n11;
            root.right = n12;
    
            n11.left = n21;
            n11.right = n22;
            n12.left = n23;
            n12.right = n24;
    
            n21.left = n31;
            n21.right = n32;
            n22.left = n33;
            n22.right = n34;
            n23.left = n35;
            n23.right = n36;
            n24.left = n37;
            n24.right = n38;
    
            return root;
        }
       
        public static void main(String[] args) throws FileNotFoundException {
        	PrintStream console = System.out;
        	File file = new File("out.txt");
        	FileOutputStream fos=new FileOutputStream(file);
        	PrintStream ps = new PrintStream(fos);
        	System.setOut(ps);
        	BTreePrinter.printNode(test1());
            System.setOut(console);
        }
    }
    My input text file will be in the following format, each row is 1 tree, each empty line between the rows means a new tree.
    Java Code:
    80 45 14 1 2 3 4 51
    
    2 4 7 8 9 6 3 4
    
    1 2 3 4 5 6 7 9

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Importing values from a text file into a Binary Search Tree

    The Scanner class has methods that can read the file line by line.
    The String class's split method could separate out the values on each line.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    itman95 is offline Member
    Join Date
    Nov 2014
    Posts
    7
    Rep Power
    0

    Default Re: Importing values from a text file into a Binary Search Tree

    I see, i dont know how to implement this in my code however..

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Importing values from a text file into a Binary Search Tree

    Read the API doc for the Scanner class. It has several examples of how to use its methods.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    itman95 is offline Member
    Join Date
    Nov 2014
    Posts
    7
    Rep Power
    0

    Default Re: Importing values from a text file into a Binary Search Tree

    I have created the scanner class...all i have it doing is just reading the text file. How can i take the that and insert into my binary tree?

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    
    public class test{
      public static void main(String[] args) {
        try {
          BufferedReader br = new BufferedReader(new FileReader("test.txt"));
          String file;
          int lineNum = 1;
          while ((file = br.readLine()) != null) {
            System.out.print( "(" + lineNum++ + ") ");
            System.out.println(file.toLowerCase());
    
            String line = br.readLine();
            //String is split or removes the spaces
            String[] words = line.split(" ");
    
          }
        }
        catch (IOException e){
          System.out.println("Error: " + e.getMessage());
        }
      }
    }

  6. #6
    itman95 is offline Member
    Join Date
    Nov 2014
    Posts
    7
    Rep Power
    0

    Default Re: Importing values from a text file into a Binary Search Tree

    I believe line 18 in my scanner class can be used to start a new binary tree drawing in my output. Again i dont know how to get the values that are read via this class into my binary tree.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Importing values from a text file into a Binary Search Tree

    how to get the values that are read via this class into my binary tree.
    The binary tree class should have an add() method to call to add nodes with the values contained in the words array.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    itman95 is offline Member
    Join Date
    Nov 2014
    Posts
    7
    Rep Power
    0

    Default Re: Importing values from a text file into a Binary Search Tree

    Can you please show me an example

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Importing values from a text file into a Binary Search Tree

    Something like:
    refToBT.add(words[index]); // add a value from the words array to the BT
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    itman95 is offline Member
    Join Date
    Nov 2014
    Posts
    7
    Rep Power
    0

    Default Re: Importing values from a text file into a Binary Search Tree

    I dont follow you

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Importing values from a text file into a Binary Search Tree

    Does the binary tree class have an add() method?
    If not, you need to write one.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    itman95 is offline Member
    Join Date
    Nov 2014
    Posts
    7
    Rep Power
    0

    Default Re: Importing values from a text file into a Binary Search Tree

    Lines 5-40 in BTreePrinterTest class are setting the node values, cant i use that or i still need an add method?

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Importing values from a text file into a Binary Search Tree

    That code is manually building a tree with hardcoded values and is no use if the values are changed.
    There needs to be an add() method that knows how to add a node to the tree.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Turning text file into Binary Search Tree
    By Wnt2bsleepin in forum New To Java
    Replies: 13
    Last Post: 05-08-2012, 04:11 PM
  2. Replies: 2
    Last Post: 04-20-2011, 05:39 PM
  3. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  4. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 01:42 AM
  5. Binary Search Tree
    By michael_mke in forum New To Java
    Replies: 3
    Last Post: 12-04-2008, 02:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •