Thursday, December 27, 2012
Tuesday, December 4, 2012
CSC438 - DATA STRUCTURE - LAB BINARY TREE
package LAB_9_TREE;
class BinaryTreeNode {
int data;
BinaryTreeNode right;
BinaryTreeNode left;
BinaryTreeNode(int a)
{
this.data = a;
}
}
public class BinaryTree {
protected BinaryTreeNode root;
// DEFAULT CONSTRUCTOR
public BinaryTree()
{
root = null;
}
// DESTROY THE BINARY TREE
public void destroyTree ()
{
root = null;
}
//DETERMINE WHETER THE BINARY TREE IS EMPTY
public boolean isEmpty()
{
return ( root == null );
}
// DETERMINE THE HEIGHT OF THE BINARY TREE
public int treeHeight()
{
return height(root);
}
// A RECURSIVE FUNCTION TO GET THE HEIGHT OF THE BINARY TREE
public int height( BinaryTreeNode p )
{
if ( p == null )
return 0;
else
return 1 + max(height(p.left),height(p.right) );
}
// COMPARE THE TWO VALUE FROM THE LEFT AND THE RIGHT NODE
public int max( int x,int y)
{
if ( x >= y )
return x;
else
return y;
}
//INSERT A NEW NODE INTO THE BINARY TREE
public void insertNode(int item)
{
BinaryTreeNode current;
BinaryTreeNode trailCurrent = null;
BinaryTreeNode newNode;
newNode = new BinaryTreeNode(0);
newNode.data = item;
newNode.left = null;
newNode.right = null;
if(root == null)
root = newNode;
else
{
current = root;
while(current != null)
{
trailCurrent = current;
if(current.data == item)
{
System.err.print("The insert item is already in the list. Duplicate item are not allowed");
return;
}
else
if(current.data < item)
current = current.left;
else
current = current.right;
}
if(trailCurrent.data < item)
trailCurrent.left = newNode;
else
trailCurrent.right = newNode;
}
}
}
============================================================
package LAB_9_TREE;
import javax.swing.*;
public class TestBinaryTree
{
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
int value;
System.out.println("Inserting the following values : ");
for ( int i = 0; i < 10 ; i++)
{
value = (int) (Math.random()*100);
System.out.print(value + " ");
tree.insertNode(value);
}
}
}
/*
The output :
Inserting the following values :
45 60 63 72 67 20 1 28 99 30
*/
prefix and postfix the easy ways
Find the prefix and postfix notation for the
following infix expression:
|
|
|
|
(a + b – c) * (e / f) – ( g – h/i)
There is a quick way to
convert from one notation to another. You start by inserting all the implicit brackets/parentheses
that determine the order of evaluation, regardless of what notation the
expression is already in. So, taking the expression above and adding these
implicit brackets gives:
Now, in order to
convert from one notation to another using the bracketed form, you would move
the operator in each bracketed expression. Where you move the operator
depends on the notation that you want to convert to.
|
Postfix vs. Prefix Notation
If you want to convert to
postfix notation, you would move the operator to the end of the bracketed
expression, right before the closing brace. To convert to prefix notation, you
would move the operator to the beginning of the bracketed expression, right after
the opening brace. So, (h/i) in postfix notation would look like (h i /), and
in prefix notation would look like (/ h i ). Do this for every operator in a
bracket. So, converting the expression above to prefix notation will give you:
Moving all the operators to the beginning of the
bracketed expression for prefix notation gives us:
( - ( * ( - ( + a b) c) ( / e f)) ( - g ( / h i ) ) )
|
And finally, removing all the
parentheses gives us our final prefix notation:
- * - + a b c / e f - g / h i
|
Try figuring out how to get
the postfix notation on your own using the rules given above. You should get
this as your answer:
a b + c - e f / * g h i / - -
|
Subscribe to:
Posts (Atom)