public class BinTreeEncapsulatedAllDataProg {
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 (new ListVal(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 (new ListVal(v));
else
this.lst.setFirst (ListVal (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 (ListVal p) {
head = new Entry (p, null, null);
this.head.prev = head;
this.head.next = head;ś
}
ListVal getFirst () {
return this.head.val;
}
void setFirst (ListVal p) {
Entry ptr = new Entry (p, 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;
Entry prev_ptr;
prev_ptr = ptr;
while (ptr.next != ptr) {
System.out.println (ptr.toString());
prev_ptr = ptr;
ptr = ptr.next;
}
System.out.println (prev_ptr.toString());
}
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;
}
toString () {
return val;
}
}
}
}
}