Back to index

wims  3.65+svn20090927
Tree.java
Go to the documentation of this file.
00001 package rene.util.list;
00002 
00007 public class Tree
00008 {      ListClass Children; // list of children, each with Tree as content
00009        Object Content; // content
00010        ListElement Le; // the listelement containing the tree
00011        Tree Parent; // the parent tree
00012 
00014        public Tree (Object o)
00015        {      Content=o;
00016               Children=new ListClass();
00017               Le=null; Parent=null;
00018        }
00019 
00021        public void addchild (Tree t)
00022        {      ListElement p=new ListElement(t);
00023               Children.append(p);
00024               t.Le=p; t.Parent=this;
00025        }
00026 
00028        public void insertchild (Tree t)
00029        {      if (!haschildren()) // simple case
00030               {      addchild(t); return;
00031               }
00032               // give t my children
00033               t.Children=Children;
00034               // make t my only child
00035               Children=new ListClass();
00036               ListElement p=new ListElement(t);
00037               Children.append(p);
00038               t.Le=p; t.Parent=this;
00039               // fix the parents of all grandchildren
00040               ListElement le=t.Children.first();
00041               while (le!=null)
00042               {      Tree h=(Tree)(le.content());
00043                      h.Parent=t;
00044                      le=le.next();
00045               }
00046        }
00047 
00049        public void remove (Tree t)
00050        {      if (t.parent()!=this) return;
00051               Children.remove(t.Le);
00052        }
00053 
00055        public void removeall ()
00056        {      Children.removeall();
00057        }
00058 
00059        // Access Methods:
00060        public boolean haschildren () { return Children.first()!=null; }
00061        public Tree firstchild () { return (Tree)Children.first().content(); }
00062        public Tree lastchild () { return (Tree)Children.last().content(); }
00063        public Tree parent () { return Parent; }
00064        public ListClass children () { return Children; }
00065        public Object content () { return Content; }
00066        public void content (Object o) { Content=o; }
00067        public ListElement listelement () { return Le; }
00068 }
00069