Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

91
LINES

< > BotCompany Repo | #1003407 // class OccTree2 - with element in node, fixed element class

JavaX fragment (include)

sclass OccTree2 {
  OccTree2 parent;
  E e;
  new L<OccTree2> next;
  int count; // This isn't accurate after replaceWith and stuff
  
  *() {}
  *(E *e) {}
  
  void addScript(L<E> script) {
    OccTree2 node = this;
    ++count;
    for (E c : script)
      (node = node.getOrMakeFollowUp(c)).count++;
  }
  
  OccTree2 getOrMakeFollowUp(E e) {
    OccTree2 node = followUp(e);
    if (node == null)
      next.add(node = newChild(e));
    ret node;
  }
  
  OccTree2 followUp(E e) {
    for (OccTree2 t : next)
      if (eq(t.e, e))
        ret t;
    null;
  }
  
  L<E> followUpKeys() {
    ret collect(next, "e");
  }
  
  OccTree2 newChild(E e) {
    OccTree2 child = new OccTree2(e);
    child.parent = this;
    ret child;
  }
  
  // size including all nodes
  int size() {
    int n = 1;
    for (OccTree2 t : next)
      n += t.size();
    ret n;
  }
  
  L<OccTree2> allLeaves() { new L l; collectLeaves(l); ret l; }
  
  // does not include root node as it has no input line associated
  L<OccTree2> allNodes() { new L l; collectNodes(l); ret l; }
  
  void collectLeaves(L<OccTree2> l) {
    if (isLeaf())
      l.add(this);
    else
      for (OccTree2 t : next)
        t.collectLeaves(l);
  }
  
  void collectNodes(L<OccTree2> l) {
    if (!isRoot())
      l.add(this);
    for (OccTree2 t : next)
      t.collectNodes(l);
  }
  
  bool isRoot() { ret e == null; }
  
  bool isLeaf() {
    ret !isRoot() && isEmpty(next);
  }
  
  int level() {
    int n = 0;
    OccTree2 node = this;
    while (node.parent != null) {
      ++n;
      node = node.parent;
    }
    ret n;
  }
  
  void replaceWith(OccTree2 n) {
    int i = parent.next.indexOf(this);
    parent.next.set(i, n);
    n.parent = parent;
    parent = null;
  }
}

Author comment

Began life as a copy of #1003331

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv

No comments. add comment

Snippet ID: #1003407
Snippet name: class OccTree2 - with element in node, fixed element class
Eternal ID of this version: #1003407/2
Text MD5: 5ce6d734daf3c2889bd45d808f409315
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-09-07 18:07:05
Source code size: 1836 bytes / 91 lines
Pitched / IR pitched: No / No
Views / Downloads: 696 / 3595
Version history: 1 change(s)
Referenced in: [show references]