2

I am trying to make a generic binary tree that is created from a text file. I am looking for the best way and correctly do so and already tried. In my first method is BinaryTree(Scanner) is what creates the tree from a Scanner tied to the file. Then leftInsert() inserts left and rightInsert() inserts right and I hope those are correct too. But I am not sure how to make the text file correctly in order for the tree to be made as well.

Text File:

    A
    B
    C
    D 
    E
    F        

Generic Binary Tree Class:

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

import org.w3c.dom.Node;

private class Nodes<E> {

    public E value;
    public Nodes<E> leftChild;
    public Nodes<E> rightChild;

    public Nodes(E value) {
        leftChild = null;
        rightChild = null;
        this.value = value;
    }
}

public class BinaryTree<E> {

private Node root;


// biggest issue here
public BinaryTree(Scanner){
// reads a file to create a tree
}

public void leftInsert(Nodes<E> Node, E value) {

    if ((value).compareTo(Node.value) < 0) {

        if (Node.leftChild == null) {

            Node.leftChild = new Nodes<E>(value);

        } else {

            leftInsert(Node.leftChild, value);

        }
    }

}

public void rightInsert(Nodes<E> Node, E value) {

    if ((value).compareTo(Node.value) >= 0) {

        if (Node.rightChild == null) {

            Node.rightChild = new Nodes<E>(value);

        } else {

            rightInsert(Node.rightChild, value);

        }
    }

}
}
  • As in, you're wanting to be able to read in that literal text, and generate a tree from it? – Luke Jun 25 '15 at 04:39
  • If that's the only way to make a tree from a text file, then yes. Or is there a more simpler way to do it? –  Jun 25 '15 at 04:53
  • Okay.. working on something :) this is good fun! – Luke Jun 25 '15 at 05:05
  • Didn't do generics or a whole lot of Java-style get/set encapsulation, sue me! – Luke Jun 25 '15 at 05:16

1 Answers1

2

Yay! :D

import java.util.ArrayList;
import java.util.Scanner;
import java.util.HashMap;
import java.util.regex.*;
public class Tree {
    public static void main(String[] args) {
        (new Tree()).build();
    }
    private ArrayList<Node> nodes;
    private ArrayList<Link> links;
    public void build() {
        nodes = new ArrayList<Node>();
        links = new ArrayList<Link>();
        // Read in nodes and links
/*
    A
   / \
  B   C
 / \
D   E
   / \
  F   G
*/
        String input = "    A\n   / \\\n  B   C\n / \\\nD   E\n   / \\\n  F   G";
        int y = 0;
        for (String line : input.split("\n")) {
            Matcher matcher = Pattern.compile("[A-Z]").matcher(line);
            while (matcher.find()) {
                Node node = new Node();
                node.name = matcher.group();
                node.x = matcher.start();
                node.y = y;
                nodes.add(node);
            }
            matcher = Pattern.compile("[/\\\\]").matcher(line);
            while (matcher.find()) {
                Link link = new Link();
                link.x = matcher.start();
                link.y = y;
                links.add(link);
            }
            ++y;
        }
        // Join the nodes
        for (Node node : nodes) {
            Node left = getNodeAt(node.x - 2, node.y + 2);
            if (left != null && hasLinkAt(node.x - 1, node.y + 1))
                node.left = left;
            Node right = getNodeAt(node.x + 2, node.y + 2);
            if (right != null && hasLinkAt(node.x + 1, node.y + 1))
                node.right = right;
        }
        // Great success!
        nodes.get(0).print(0);
    }
    private Node getNodeAt(int x, int y) {
        for (Node node : nodes)
            if (node.x == x && node.y == y)
                return node;
        return null;
    }
    private boolean hasLinkAt(int x, int y) {
        for (Link link : links)
            if (link.x == x && link.y == y)
                return true;
        return false;
    }
    private class Node {
        private int x, y;
        private String name;
        private Node left, right;
        public void print(int indent) {
            String indentString = "";
            for (int i = 0; i < indent; ++i, indentString += "    ") {}
            System.out.println(indentString + name + ": {");
            if (left != null)
                left.print(indent + 1);
            if (right != null)
                right.print(indent + 1);
            System.out.println(indentString + "}");
        }
    }
    private class Link {
        private int x, y;
    }
}

Output to prove great success:

A: {
    B: {
        D: {
        }
        E: {
            F: {
            }
            G: {
            }
        }
    }
    C: {
    }
}
Luke
  • 1,724
  • 1
  • 12
  • 17
  • This looks great it really does! But its wrong. The BinaryTree(Scanner) is to create a tree from a Scanner tied to a file. I created defined data file (the text file) format to specify the binary tree and create at least one such input file. –  Jun 25 '15 at 05:40
  • Okay, so change the part where it uses a literal string! Get a scanner, and get each line? – Luke Jun 25 '15 at 06:10
  • Yes, Just create a tree from the text file and if changing the text file to do so is necessary then that's okay. It just reads the file and creates the tree from the text file for example comma separated values, spaces between values, or one value per line. But I am wondering if I have to make the file the way I did –  Jun 25 '15 at 06:21
  • What's the original data format...? I thought you were trying to build from that file? Also, do you mind reexplaining your previous comment there, please? :S – Luke Jun 25 '15 at 06:28
  • I am sorry to confuse you, my fault. Simply the text file looks like `A B C D E F G` and you have to create a binary tree from that. Yes just get a scanner and get each line and create the tree. But I am not sure how to make the tree using the Scanner. Forget the last comment. –  Jun 25 '15 at 06:45
  • does that make batter sense? –  Jun 25 '15 at 12:34
  • No D: like `"A B C D E F G"` or like `" A\n / \\\n B C\n / \\\nD E\n / \\\n F G"`? If the latter, you can use a scanner or whatever for my algo, you're just trying to determine the x,y positions of the nodes and edges. This page offers some other ways of reading from files: http://stackoverflow.com/questions/3849692/whole-text-file-to-a-string-in-java – Luke Jun 25 '15 at 15:04
  • yes like `"A B C D E F G"` and I just saw the the link. hmm using string huh? –  Jun 26 '15 at 04:45
  • I'm not I understand, then... that simple string "A B C D E F G" doesn't imply any links, so how do you know where to put the nodes...? – Luke Jun 26 '15 at 05:08