public class EncapsulatedAllDataProg {
public static void main (String [] args) {
try {
SaveData dat = new SaveData ();
int i;
for (i = 0; i < args.length; i++)
dat.setData (args[i]);
dat.printData ();
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("usage: java BinTreeProg arg1 ...");
}
}
public static class SaveData {
BinTree root = null;
LinkedLst lst = null;
SaveData (String v) {
BinTree root = new BinTree (v);
LinkedLst lst = new LinkedLst (v);
}
SaveData () {
this.root = null;
this.lst = null;
}
void setData (String v) {
if (this.root == null)
this.root = new BinTree (v);
else
this.root.insertBinTree (v);
if (this.lst == null)
this.lst = new LinkedLst (v);
else
this.lst.setFirst (v);
}
void printData () {
root.traverseBinTree ();
lst.printData ();
}
public static class BinTree {
public static class BinTreeNode {
BinTree l;
BinTree r;
String v;
BinTreeNode (BinTree l, BinTree r, String v) {
this.l = l;
this.r = r;
this.v = v;
}
}
BinTreeNode nod = null;
BinTree (String v) {
this.nod = new BinTreeNode (null, null, v);
}
void insertBinTree (String v) {
if (this.nod.v.compareTo (v) < 0) {
if (this.nod.l == null)
this.nod.l = new BinTree (v);
else
this.nod.l.insertBinTree (v);
}
else {
if (this.nod.r == null)
this.nod.r = new BinTree (v);
else
this.nod.r.insertBinTree (v);
}
}
void traverseBinTree () {
if (this.nod.r != null)
this.nod.r.traverseBinTree ();
System.out.println (this.nod.v);
if (this.nod.l != null)
this.nod.l.traverseBinTree ();
}
}
static public class ListVal {
public String val;
ListVal (String val) {
this.val = val;
}
String getVal () {
return this.val;
}
}
static public class LinkedLst {
public Entry head;
LinkedLst (String v) {
head = new Entry (new ListVal(v), null, null);
this.head.prev = head;
this.head.next = head;
}
ListVal getFirst () {
return this.head.val;
}
void setFirst (String v) {
Entry ptr = new Entry (new ListVal (v), head, null);
ptr.prev = ptr;
head.prev = ptr;
head = ptr;
}
void rmFirst () {
if ((head != null) && (head.next != null) && (head.next != head)) {
ListVal val = head.val;
head.next.prev = head.next;
head = head.next;
}
}
void printData () {
Entry ptr;
ptr = head;
while (ptr.next != ptr) {
System.out.println (ptr.toStr());
ptr = ptr.next;
}
System.out.println (ptr.toStr());
}
static public class Entry {
ListVal val;
Entry next;
Entry prev;
Entry (ListVal val, Entry next, Entry prev) {
this.val = val;
this.next = next;
this.prev = prev;
}
String toStr () {
return val.getVal();
}
}
}
}
}