Nice TWiki > Doc > CodeExamples > BinarySearchTreeExample (r1.1) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
<Comparable T,U>
class IntTreeNode <T,U> {
  private !T m_key;
  private U m_value;
  private ?IntTreeNode<T,U> left = null;
  private ?IntTreeNode<T,U> right = null;

  public void add (!T key, U value) {
    if (key == m_key)
    { 
      m_value = value;
    }
    else if (key < m_key)
    { 
      ?IntTreeNode<T,U> theleft = left;
      if (theleft == null) {
        theleft = new IntTreeNode (m_key: key, m_value: value);
      } else {
        theleft.add (key, value);
      }
    }
    else
    { 
      ?IntTreeNode<T,U> theright = right;
      if (theright == null) {
        theright = new IntTreeNode (m_key: key, m_value: value);
      } else {
        theright.add (key, value);
      }
    }
  }

  public ?U getValue(T key) {
    if (key == m_key) {
      return m_value;
    } else if (key < m_key) {
      ?IntTreeNode<T,U> myleft = left;
      return myleft != null ? myleft.getValue(key) : null;
    } else {
      ?IntTreeNode<T,U> myright = right;
      return myright != null ? myright.getValue(key) : null;
    }
  }
}

<Comparable T,U>
class IntTree <T,U> {
  ?IntTreeNode<T,U> content = null;

  public void add (!T key, !U value) {
    ?IntTreeNode<T,U> mycontent = content;
    if (mycontent == null) {
      mycontent = new IntTreeNode (m_key: key, m_value: value);
    } else {
      mycontent.add (key, value);
    }
  }

  public ?U getValue(!T key)
  {
    ?IntTreeNode<T,U> mycontent = content;
    return mycontent != null ? mycontent.getValue(key) : null;
  }
}

-- Raboof - 11 Apr 2005

Topic BinarySearchTreeExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.2 | > | r1.1 | More }
Revision r1.1 - 11 Apr 2005 - 16:37 GMT - TWikiGuest
Parents: WebHome > CodeExamples
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.