Skip to content

Instantly share code, notes, and snippets.

@A-gambit
Last active September 5, 2015 14:23
Show Gist options
  • Save A-gambit/62ca1ff5732e1ad5d595 to your computer and use it in GitHub Desktop.
Save A-gambit/62ca1ff5732e1ad5d595 to your computer and use it in GitHub Desktop.
package com.grisha.tree;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static Node createTmpTree() {
Node animal = new Node("Animal");
Node bear = new Node("Bear");
Node cat = new Node("Cat");
Node dog = new Node("Dog");
Node jaguar = new Node("Jaguar");
Node panther = new Node("Panther");
Node lynx = new Node("Lynx");
Node wolf = new Node("Wolf");
Node dingo = new Node("Dingo");
animal.addChild(cat);
animal.addChild(dog);
animal.addChild(bear);
cat.addChild(jaguar);
cat.addChild(panther);
cat.addChild(lynx);
dog.addChild(wolf);
dog.addChild(dingo);
return animal;
}
private static String treeToString(Node node) {
String strTree = node.getValue() + ">(";
List<Node> children = node.getChildren();
for (Node child : children) {
if (child.getChildren().size() > 0) {
strTree += treeToString(child);
}
else {
strTree += child.getValue();
}
strTree += children.indexOf(child) == children.size() - 1 ? ")" : ",";
}
return strTree;
}
public static String printTree(Node node) {
String strTree = treeToString(node);
System.out.println(strTree);
return strTree;
}
public static void writeTree(Node node, String dir, String filename) {
String strTree = printTree(node);
BufferedWriter output = null;
try {
File file = new File(dir, filename);
output = new BufferedWriter(new FileWriter(file));
output.write(strTree);
output.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
public static String reedTree(String dir, String filename) {
BufferedReader input = null;
String path = dir + "/" + filename;
String strTree = "";
try {
String line;
input = new BufferedReader(new FileReader(path));
strTree = input.readLine();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
return strTree;
}
private static List<Integer> getIndexOdChild(String childrenStr) {
List<Integer> indexArray = new ArrayList<>();
int open = 0;
int close = 0;
indexArray.add(0);
for (int i = 0; i < childrenStr.length(); i++) {
char cur = childrenStr.charAt(i);
if (cur == '(') {
open++;
}
else if (cur == ')'){
close++;
}
else if (cur == ',' && open == close){
open = 0;
close = 0;
indexArray.add(i);
}
}
indexArray.add(childrenStr.length());
return indexArray;
}
private static List<String> splitChildren(String childrenStr) {
List<String> childrenArray = new ArrayList<>();
if (childrenStr.indexOf('(') == -1) {
for (String part : childrenStr.split(",")) {
childrenArray.add(part);
}
}
else {
List<Integer> indexArray = getIndexOdChild(childrenStr);
for (int i = 0; i < indexArray.size() - 1; i++) {
String substring = childrenStr.substring(indexArray.get(i) + (i == 0 ? 0 : 1), indexArray.get(i + 1));
childrenArray.add(substring);
}
}
return childrenArray;
}
public static Node stringToTree(String strTree) {
String[] parts = strTree.split(">", 2);
Node node = new Node(parts[0]);
if (parts.length == 1) {
return node;
}
String childrenStr = parts[1].substring(1, parts[1].length() - 1);
List<String> childrenArray = splitChildren(childrenStr);
for (int i = 0; i < childrenArray.size(); i++) {
node.addChild(stringToTree(childrenArray.get(i)));
}
return node;
}
public static void main(String[] args) {
String dir = "/Users/Gregory/IdeaProjects/java.lessons/lesson1/src/main/java/com/grisha/tree";
String filename = "tmp.txt";
Node tree = createTmpTree();
writeTree(tree, dir, filename);
String strTree = reedTree(dir, filename);
Node newTree = stringToTree(strTree);
printTree(newTree);
}
}
package com.grisha.tree;
import java.util.ArrayList;
import java.util.List;
public class Node {
private String value;
private List<Node> children = new ArrayList<>();
public Node(String initValue) {
value = initValue;
}
public void setValue (String newValue) {
value = newValue;
}
public String getValue () {
return value;
}
public void setChildren(List newChildren) {
children = newChildren;
}
public List getChildren () {
return children;
}
public void addChild(Node child) {
children.add(child);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment