JavaAlgorithms
Elementary and no so elementary Java algorithms
|
00001 00009 package treeAlgorithms; 00010 00011 import java.util.Iterator; 00012 00013 public class TreeNode<T extends Comparable<T>> implements Iterable<TreeNode<T>> { 00014 private Comparable<T> mValue; 00015 private TreeNode<T> mRight = null; 00016 private TreeNode<T> mLeft = null; 00017 00021 public TreeNode<T> getLeft() { 00022 return mLeft; 00023 } 00024 00029 public void setLeft(TreeNode<T> node) { 00030 this.mLeft = node; 00031 } 00032 00036 public TreeNode<T> getRight() { 00037 return mRight; 00038 } 00039 00044 public void setRight(TreeNode<T> node) { 00045 this.mRight = node; 00046 } 00047 00053 public TreeNode(Comparable<T> value) { 00054 this.mValue = value; 00055 } 00056 00060 public Comparable<T> getValue() { 00061 return mValue; 00062 } 00063 00068 public void setValue(Comparable<T> newVal) { 00069 this.mValue = newVal; 00070 } 00071 00080 protected void printTree(TreeNode<T> root, String indent) { 00081 if (root != null) { 00082 System.out.println(indent + root); 00083 String newIndent = indent + " "; 00084 for (TreeNode<T> elem : root) { 00085 printTree(elem, newIndent); 00086 } 00087 } 00088 } // printTree 00089 00095 public void printTree() { 00096 printTree(this, ""); 00097 } 00098 00099 @Override 00100 public Iterator<TreeNode<T>> iterator() { 00101 Iterator<TreeNode<T>> itr = new TreeNodeIterator<T>(this); 00102 return itr; 00103 } 00104 00105 @Override 00106 public String toString() { 00107 return this.getValue().toString(); 00108 } 00109 00110 }