Wednesday, December 1, 2010

Program 7

package program7;

/*********************
* Tree Class
*********************/

public class Tree
{

private Node root; // the only data field in Tree
int numRuns;

//constructor
public Tree()
{
numRuns = 1;
root = null;
}

//method insert
public void insert(int v)
{
/**************************************************
* This method will insert a node into the tree.
* It will not follow the standard binary tree rule due
* to the requirements of the project.
* 1, will be the root.
* 2, will be the left child
* 3, will be the right child
* 4, will be the left of the left child
* 5, will be the right of the left child
* 6, will be the left of the right child
* 7, will be the right of the right child
******************************************************/

Node newNode = new Node();
newNode.value = v;
Node current =root;

switch (numRuns)
{
case 1: root= newNode;
numRuns =2;
return;
case 2: current.left = newNode;
numRuns =3;
return;
case 3: current.right = newNode;
numRuns =4;
return;
case 4: current.left.left = newNode;
numRuns =5;
return;
case 5: current.left.right = newNode;
numRuns =6;
return;
case 6: current.right.left = newNode;
numRuns =7;
return;
case 7: current.right.right = newNode;
numRuns =8;
return;

default: System.out.println("Invalid");break;
}

} // end insert


//method traverse
public void traverse()
{

System.out.println();
preOrder(root);
System.out.println("");
inOrder(root);
System.out.println("");
postOrder(root);
System.out.println("");
}

//method preOrder
public void preOrder (Node localRoot)
{
//this method will traverse the tree and print the nodest in order
//An example of in order (infix):
//If the root is A, the left child is B, and the right child is C
//The infix order would be A,B,C


if(localRoot != null)
{
System.out.print(localRoot.value + " ");
inOrder(localRoot.left);
inOrder(localRoot.right);
}

}

//method inOrder
public void inOrder (Node localRoot)
{
//this method will traverse the tree and print the nodest in order
//An example of in order (infix):
//If the root is A, the left child is B, and the right child is C
//The infix order would be B,A,C


if(localRoot != null)
{
inOrder(localRoot.left);
System.out.print(localRoot.value + " ");
inOrder(localRoot.right);
}

}

//method postOrder
public void postOrder (Node localRoot)
{
//this method will traverse the tree and print the nodest in order
//An example of in order (infix):
//If the root is A, the left child is B, and the right child is C
//The infix order would be B,C,A


if(localRoot != null)
{
inOrder(localRoot.left);
inOrder(localRoot.right);
System.out.print(localRoot.value + " ");
}

}




//method find
public void find(int key)
{
//this method will be implemented for program # 9
}

//method delete
public void delete(int id)
{
//this method will be implemented for program #10
}

}


/***********************************
*
* Page 2
*
************************************/


package program7;


/*************************
* Node Class
************************/

public class Node
{
int value; //data used as key value
Node left; //his node's left child
Node right; //this node's right child


}


/***********************************
*
* Page 3
*
************************************/



package program7;

/*********************
* Tree Class
*********************/

public class Tree
{

private Node root; // the only data field in Tree
int numRuns;

//constructor
public Tree()
{
numRuns = 1;
root = null;
}

//method insert
public void insert(int v)
{
/**************************************************
* This method will insert a node into the tree.
* It will not follow the standard binary tree rule due
* to the requirements of the project.
* 1, will be the root.
* 2, will be the left child
* 3, will be the right child
* 4, will be the left of the left child
* 5, will be the right of the left child
* 6, will be the left of the right child
* 7, will be the right of the right child
******************************************************/

Node newNode = new Node();
newNode.value = v;
Node current =root;

switch (numRuns)
{
case 1: root= newNode;
numRuns =2;
return;
case 2: current.left = newNode;
numRuns =3;
return;
case 3: current.right = newNode;
numRuns =4;
return;
case 4: current.left.left = newNode;
numRuns =5;
return;
case 5: current.left.right = newNode;
numRuns =6;
return;
case 6: current.right.left = newNode;
numRuns =7;
return;
case 7: current.right.right = newNode;
numRuns =8;
return;

default: System.out.println("Invalid");break;
}

} // end insert


//method traverse
public void traverse()
{

System.out.println();
preOrder(root);
System.out.println("");
inOrder(root);
System.out.println("");
postOrder(root);
System.out.println("");
}

//method preOrder
public void preOrder (Node localRoot)
{
//this method will traverse the tree and print the nodest in order
//An example of in order (infix):
//If the root is A, the left child is B, and the right child is C
//The infix order would be A,B,C


if(localRoot != null)
{
System.out.print(localRoot.value + " ");
inOrder(localRoot.left);
inOrder(localRoot.right);
}

}

//method inOrder
public void inOrder (Node localRoot)
{
//this method will traverse the tree and print the nodest in order
//An example of in order (infix):
//If the root is A, the left child is B, and the right child is C
//The infix order would be B,A,C


if(localRoot != null)
{
inOrder(localRoot.left);
System.out.print(localRoot.value + " ");
inOrder(localRoot.right);
}

}

//method postOrder
public void postOrder (Node localRoot)
{
//this method will traverse the tree and print the nodest in order
//An example of in order (infix):
//If the root is A, the left child is B, and the right child is C
//The infix order would be B,C,A


if(localRoot != null)
{
inOrder(localRoot.left);
inOrder(localRoot.right);
System.out.print(localRoot.value + " ");
}

}




//method find
public void find(int key)
{
//this method will be implemented for program # 9
}

//method delete
public void delete(int id)
{
//this method will be implemented for program #10
}

}

No comments:

Post a Comment