SimpleTree
From charlesreid1
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.
| Trees Part of Computer Science Notes
Series on Data Structures Abstract data type: Trees/ADT Concrete implementations: Trees/LinkedTree · Trees/ArrayTree · SimpleTree
Tree Traversal Preorder traversal: Trees/Preorder Postorder traversal: Trees/Postorder In-Order traversal: Binary Trees/Inorder Breadth-First Search: BFS Breadth-First Traversal: BFT Depth-First Search: DFS Depth-First Traversal: DFT OOP Principles for Traversal: Tree Traversal/OOP · Tree Traversal/Traversal Method Template Tree operations: Trees/Operations Performance · Trees/Removal
Tree Applications Finding Minimum in Log N Time: Tree/LogN Min Search
Abstract data type: Binary Trees/ADT Concrete implementations: Binary Trees/LinkedBinTree · Binary Trees/ArrayBinTree Binary Trees/Cheat Sheet · Binary Trees/OOP · Binary Trees/Implementation Notes
|