SimpleTree: Difference between revisions
From charlesreid1
(Created page with "Can I just get a simple dang tree class goin up in here plz? KTNX Here's a functional tree node: <pre> /** Expression Tree Node. * * Could make this an <E>, * or di...") |
No edit summary |
||
| Line 53: | Line 53: | ||
Git you a nice tree there. | Git you a nice tree there. | ||
{{TreeFlag}} | |||
[[Category:Tree]] | |||
[[Category:Java]]] | |||
Revision as of 03:53, 14 June 2017
Can I just get a simple dang tree class goin up in here plz?
KTNX
Here's a functional tree node:
/** Expression Tree Node.
*
* Could make this an <E>,
* or distinguish internal/external,
* operator and number nodes,
* but will make these all expressions.
*
* Could make this process Strings,
* but will assume everything is single-digit,
* single chars only.
*/
class TreeNode {
Character element;
TreeNode left, right;
public TreeNode(Character e) {
this.element = e;
}
public int nChildren() {
int nc = 0;
if(left!=null) nc++;
if(right!=null) nc++;
return nc;
}
public String toString() { /* ? */ }
public Character getElement() { return this.element; }
public void setElement(Character v) { this.element = v; }
public void setLeft(TreeNode newl) { this.left = newl; }
public void setRight(TreeNode newr) { this.right = newr; }
}
Put this inside a Tree class:
public class Tree {
TreeNode root;
public ExpressionTree() {
root = null;
}
}
Git you a nice tree there.