001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.compilers.opt.util;
014
015import java.util.Enumeration;
016
017
018/**
019 *  This class is a node in a tree.  Both up and down pointers are used.
020 */
021public class TreeNode {
022
023  /**
024   *  The parent of this node
025   */
026  private TreeNode parent;
027
028  /**
029   *  The first (leftmost) child
030   */
031  private TreeNode leftChild;
032
033  /**
034   *  The next node on the child list that I am on
035   */
036  private TreeNode rightSibling;
037
038  /**
039   * Constructor
040   */
041  public TreeNode() {
042    parent = null;
043    leftChild = null;
044    rightSibling = null;
045  }
046
047  /**
048   * return the parent of this node
049   * @return my parent
050   */
051  public TreeNode getParent() {
052    return parent;
053  }
054
055  /**
056   * returns the first child of this node
057   * @return the first child of this node
058   */
059  public TreeNode getLeftChild() {
060    return leftChild;
061  }
062
063  /**
064   * returns the next node with the same parent as me
065   * @return the next node with the same parent as me
066   */
067  public TreeNode getRightSibling() {
068    return rightSibling;
069  }
070
071  /**
072   *  adds a child to this node
073   *  @param node the new child
074   */
075  public void addChild(TreeNode node) {
076    if (leftChild == null) {
077      leftChild = node;
078    } else {
079      // get to the last sibling
080      TreeNode siblingNode = leftChild;
081      while (siblingNode.rightSibling != null) {
082        siblingNode = siblingNode.rightSibling;
083      }
084      siblingNode.rightSibling = node;
085    }
086    node.parent = this;
087  }
088
089  /**
090   *  Sets all tree pointers to null
091   */
092  public void clear() {
093    leftChild = null;
094    rightSibling = null;
095    parent = null;
096  }
097
098  public Enumeration<TreeNode> getChildren() {
099    return new TreeNodeChildrenEnumerator(this);
100  }
101
102}