A tree is a linked structure in which each node may have more than

advertisement
A tree is a linked structure in which each node may have more than one successor. The
one structural rule which prevents a total randomness from occurring is – each node may
only have one parent.
A tree has one node which has no parents - the root node. In fact, a tree may be
considered a ‘recursive structure’, as each node is a ‘root’ of it’s own ‘subtree’.
This leads us to see that recursive code is quite effective in dealing with a tree.
Binary trees are trees in which each node may have no more that two children.
There is no order imposed on the data stored in a binary tree, so a search must potentially
traverse the entire tree, a O(n) operation. However it may be noted that no node in a
binary tree containing N nodes is more that log2N paths away from the root, if the tree is
full (each level but the last is filled).
Binary trees can be used to store a variety of types of data, ranging from a mathematical
expression to a business corporate structure. Insertion, deletion and other operations
exist based on the type of data being stored in the tree. An example class follows:
An expression can be stored in a form which incorporates precedence, as follows:
(5+2) * (33-9)
A tree structure which contains this sort of information is referred to as an expression tree.
A binary tree class which creates and evaluates a tree containing a mathematical
expression from a prefix expression follows:
NOTE: printout methods can be used with ANY type of binary tree or binary search tree
public class ExTree{
private class TreeNode{
Object info;
TreeNode left;
TreeNode right;
}
TreeNode root;
String s;
// root of tree
// string containing prefix mathematical expression
public ExTree () {
root = null;
}
//expression tree built describing prefix expression
// contain in String s
public void buildIt(String x){
s = x;
TreeNode temp = build();
root = temp;
}
//expression tree built describing prefix expression
// contain in String s
public TreeNode build(){
String ops = "*+-/";
String op = s.substring(0,1);
s = s.substring(1, s.length());
TreeNode temp = new TreeNode();//create node w/ data
temp.info = op;
if ( ops.indexOf(op) >= 0) {
temp.left = build();
temp.right = build();
}
return temp;
// is operator – has children
}
public int eval(){
return rec_eval(root);
}
private int rec_eval(TreeNode node) {
if (node.left == null && node.right == null){
String ival = (String)node.info;
return Integer.parseInt(ival);
}
else {
int left = rec_eval(node.left);
int right = rec_eval(node.right);
String coperator = (String)node.info;
char operator = coperator.charAt(0);
switch(operator) {
case '+':
return left + right;
case '-':
return left - right;
case '*':
return left * right;
}
case '/':
return left / right;
default:
return 0;
}
} // method
//INORDER PRINT METHOD
public void printit(){
out(root,25);
}
private void out (TreeNode node, int width) {
if (node.left != null)
out(node.left,width-2);
outnode (node.info, width);
if (node.right != null)
out(node.right,width-2);
}
private void outnode(Object info, int width){
for (int i = 1; i < width ; i++)
System.out.print (" ");
System.out.println(info);
}
//PREORDER PRINT METHOD
public void pprintit(){
pout(root,35);
}
private void pout (TreeNode node, int width) {
if (node != null){
poutnode (node.info, width);
pout(node.right,width+3);
pout(node.left,width+3);
}
}
private void poutnode(Object info, int width){
for (int i = 1; i < width ; i++)
System.out.print (" ");
System.out.println(info);
}
} //class
A binary search tree is a binary tree in which data is ordered such that
data
contained in
the left child
<
data
contained in
parent node
data
<= contained in
the right child
This adds the capability to directly ‘find’ the location of any data element stored in the
tree. Hence, any search will take a maximum of log2N +1 comparisons to achieve it’s
result. Thus, searching in a full, balanced binary search tree is a O(lgN) operation`
The text presents a fine implementation of a generic binary search tree which can be used
to store any type of Comparable data.
Download