Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Alice

TruthTableQuiz


TreeNode.java

public class TreeNode {
   private char key;
   private int index; // Index is useful for traversals, etc.
   private TreeNode left;
   private TreeNode right;
   private TreeNode parent;

// Default constructor.  Set the key to be ' ', and the children to null.
  public TreeNode() {
       setData(' ',-1);
       setRelatives(null,null,null);
  }
  public TreeNode(char info) {
       setData(info,-1);
       setRelatives(null,null,null);
  }
// A constructor
  public TreeNode(char info,int ind) {
       setData(info,ind);
       setRelatives(null,null,null);
  }
// A constructor
  public TreeNode(char info,TreeNode L,TreeNode R) {
       setData(info,-1);
       setRelatives(null,L,R);
  }
// A constructor
  public TreeNode(char info,int ind,TreeNode L,TreeNode R) {
       setData(info,ind);
       setRelatives(null,L,R);
  }
//----------------------------------------------------------------------
  public void setData(char info,int ind) {
       key=info;
       index=ind;
       }
  public void setRelatives(TreeNode p, TreeNode l, TreeNode r) {
       parent=p;
       left=l;
       right=r;
       }
//----------------------------------------------------------------------
  public char getKey() { return key; }
  public int getIndex() { return index; }
  public TreeNode getLeft() { return left; }
  public TreeNode getRight() { return right; }
  public TreeNode getParent() { return parent; }

  public void setKey(char info) { key=info; }
  public void setIndex(int ind) { index=ind; }
  public void setParent(TreeNode T) { parent=T; }
//----------------
  public void setLeft(TreeNode T) {
      left=T;
      if(T!=null) T.parent=this;
  }
  public void setLeft(char info) {
         setLeft(new TreeNode(info));
  }
  public void setLeft(char info,int ind) {
         setLeft(new TreeNode(info,ind));
  }
//----------------
  public void setRight(TreeNode T) {
      right=T;
      if(T!=null) T.parent=this;
  }
  public void setRight(char info) {
         setRight(new TreeNode(info));
  }
  public void setRight(char info,int ind) {
         setRight(new TreeNode(info,ind));
  }
//----------------
  public void removeRight() {
      if(right!=null)
        right.setParent(null);
      right=null;
  }
  public void removeLeft() {
      if(left!=null)
        left.setParent(null);
      left=null;
  }
//----------------
  public void blankLeft() {
         setLeft(new TreeNode(' '));
  }
  public void blankRight() {
         setRight(new TreeNode(' '));
  }
//----------------
  public boolean isLeaf() {
     if(right==null && left==null)
       return true;
     else
       return false;
  }
//----------------
public void swapChildren() {
       TreeNode temp=left;
       left=right;
       right=temp;
}
//----------------
}