package com.supermars.practice;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
/*
* (11,LL)(7,LLL)(8,R)(5,)(4,L)(13,RL)(2,LLR)(1,RRR)(4,RR)()
(3,L)(4,R)()
*/
public class 层次遍历 {
static PrintStream cout = System.out;
public static void main(String[] args) throws FileNotFoundException {
Scanner cin = new Scanner(
new File(
"E:\\jspWorkspace\\programArithmetic\\src\\com\\supermars\\practice\\in.txt"));
while (cin.hasNext()) {
String s = cin.nextLine();
s = s.substring(1, s.length() - 1);
String sa[] = s.split("\\)\\(");
// build tree
TNode root = new TNode();
for (int i = 0; i < sa.length; i++) {
String sp[] = partion(sa[i]);
if (sp.length == 1) {
int v = Integer.parseInt(sp[0]);
root.addNode(root, "", v);
} else {
int v = Integer.parseInt(sp[0]);
String dir = sp[1];
root.addNode(root, dir, v);
}
}
// printf root
bfs(root);
cout.print("\n");
}
}
private static int bfs(TNode root) {
TNode[] q = new TNode[256];
q[0] = root;
int[] ans = new int[256];
int n = 0;
int front = 0, rear = 1;
while (front < rear) {
TNode t = q[front++];
if (t.have_value == 0) {
System.out.print("-1");
return 0;
}
ans[n++] = t.v;
if (t.l != null)
q[rear++] = t.l;
if (t.r != null)
q[rear++] = t.r;
}
for (int i = 0; i < n; i++) {
cout.print(ans[i] + "\t");
}
return 1;
}
private static String[] partion(String sa) {
return sa.split(",");
}
}
class TNode {
int have_value;
int v;
TNode l, r;
public void addNode(TNode root, String dir, int v2) {
if ("".equals(dir)) {
if (root.have_value == 0) {
root.have_value = 1;
root.v = v2;
}
return; // root
}
int cnt = 0;
while (cnt < dir.length()) {
if ('L' == (dir.charAt(cnt++))) {
if (root.l == null) {
TNode t = new TNode();
root.l = t;
}
root = root.l;
} else {
if (root.r == null) {
TNode t = new TNode();
root.r = t;
}
root = root.r;
}
}
root.have_value = 1;
root.v = v2;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
/*
* (11,LL)(7,LLL)(8,R)(5,)(4,L)(13,RL)(2,LLR)(1,RRR)(4,RR)()
(3,L)(4,R)()
*/
public class 层次遍历 {
static PrintStream cout = System.out;
public static void main(String[] args) throws FileNotFoundException {
Scanner cin = new Scanner(
new File(
"E:\\jspWorkspace\\programArithmetic\\src\\com\\supermars\\practice\\in.txt"));
while (cin.hasNext()) {
String s = cin.nextLine();
s = s.substring(1, s.length() - 1);
String sa[] = s.split("\\)\\(");
// build tree
TNode root = new TNode();
for (int i = 0; i < sa.length; i++) {
String sp[] = partion(sa[i]);
if (sp.length == 1) {
int v = Integer.parseInt(sp[0]);
root.addNode(root, "", v);
} else {
int v = Integer.parseInt(sp[0]);
String dir = sp[1];
root.addNode(root, dir, v);
}
}
// printf root
bfs(root);
cout.print("\n");
}
}
private static int bfs(TNode root) {
TNode[] q = new TNode[256];
q[0] = root;
int[] ans = new int[256];
int n = 0;
int front = 0, rear = 1;
while (front < rear) {
TNode t = q[front++];
if (t.have_value == 0) {
System.out.print("-1");
return 0;
}
ans[n++] = t.v;
if (t.l != null)
q[rear++] = t.l;
if (t.r != null)
q[rear++] = t.r;
}
for (int i = 0; i < n; i++) {
cout.print(ans[i] + "\t");
}
return 1;
}
private static String[] partion(String sa) {
return sa.split(",");
}
}
class TNode {
int have_value;
int v;
TNode l, r;
public void addNode(TNode root, String dir, int v2) {
if ("".equals(dir)) {
if (root.have_value == 0) {
root.have_value = 1;
root.v = v2;
}
return; // root
}
int cnt = 0;
while (cnt < dir.length()) {
if ('L' == (dir.charAt(cnt++))) {
if (root.l == null) {
TNode t = new TNode();
root.l = t;
}
root = root.l;
} else {
if (root.r == null) {
TNode t = new TNode();
root.r = t;
}
root = root.r;
}
}
root.have_value = 1;
root.v = v2;
}
}