public class Node { //node class, the base of tree
int data;
int index;
Node leftChild;
Node rightChild;
}
import java.util.Scanner;
/**
* @author NEU 灏忓畤
* @version 1.1
*/
public class TreeDemo
{
public Node root; //root node
/**********************************************************/
public TreeDemo() //initialize root node
{
root = null;
}
/**********************************************************/
public void deleteTree() // delete
{
root = root.leftChild = root.rightChild = null;
}
/**********************************************************/
public Node find(Node localNode, int n) //find the node
{
Node p;
if(localNode == null) //if root node is null
return null;
else if (localNode.data == n)
return localNode;
else
{
p = find(localNode.leftChild,n);
if(p!=null)
return p;
else
return find(localNode.rightChild,n);
}
}
/***********************************************************/
public void insert() // insert a new node
{
System.out.println("new node's data:");
Scanner scanner = new Scanner(System.in);
Node newNode = new Node(); //initialize new node
newNode.data = scanner.nextInt();
if(root == null) //if the tree is null
root = newNode;
else //if the tree is not true
{
System.out.println("input father node's num");
int fNum = scanner.nextInt();
Node fatherNode;
fatherNode = find(root,fNum);
if(fatherNode == null)
{
System.out.println("there isn't the node");
return;
}
System.out.println("left or right? 1/2");
int n = scanner.nextInt();
if(n == 1) //if insert to left child node
{
fatherNode.leftChild = newNode;
}
else //if insert to right child node
{
fatherNode.rightChild = newNode;
}
}
}
/***********************************************************/
public void inOrder(Node localNode) //inorder traverse
{
if(localNode != null)
{
inOrder(localNode.leftChild);
System.out.print(localNode.data + " ");
inOrder(localNode.rightChild);
}
}
/************************************************************/
public void preOrder(Node localNode) //preorder traverse
{
if(localNode != null)
{
System.out.print(localNode.data + " ");
preOrder(localNode.leftChild);
preOrder(localNode.rightChild);
}
}
/**************************************************************/
public void postOrder(Node localNode) //postorder traverse
{
if(localNode != null)
{
postOrder(localNode.leftChild);
postOrder(localNode.rightChild);
System.out.print(localNode.data + " ");
}
}
/***************************************************************/
public void levelOrder(Node localNode) //level order traverse
{
Node[] array = new Node[100];
int front,rear; //the queue's head and tail
front = rear = 0;
array[0] = localNode;
rear ++;
while(rear != front)
{
System.out.print(array[front].data + " ");
int i = front;
front++;
if(array[i].leftChild != null)
{
array[rear] = array[i].leftChild;
rear++;
}
if(array[i].rightChild != null)
{
array[rear] = array[i].rightChild;
rear++;
}
}
}
/***********************************************************/
public int getHeight(Node localNode) //Get the height of the tree
{
if(localNode == null)
return 0;
else
{
int leftHeight = getHeight(localNode.leftChild);
int rightHeight = getHeight(localNode.rightChild);
return (leftHeight > rightHeight)?(leftHeight + 1):(rightHeight + 1);
}
}
/*************************************************************/
}
import java.util.Scanner;
/**This praogran is to enforce a
* Tree's basical operation
* @author xiaoYu
* @version 1.1
*/
public class TreeApp {
/**
* @param args
*/
public static boolean flag; //exist the operation by this
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeDemo tree = new TreeDemo();
flag = true;
while(flag)
{
treeOperation(tree);
}
System.out.print("bye bye,my friend");
}
/**********************************************************/
public static void treeOperation(TreeDemo localTree) //use this to operate tree
{
Scanner scanner = new Scanner(System.in);
System.out.println("what funtion do you need now? 1.insert node 2.preorder traverse 3.postorder traverse 4.inorder traverse");
System.out.println(" 5.level order 6.get height 7 delete the tree 10.exist");
int select = scanner.nextInt();
switch(select)
{
case 1:
localTree.insert();
break;
case 2:
localTree.preOrder(localTree.root);
break;
case 3:
localTree.postOrder(localTree.root);
break;
case 4:
localTree.inOrder(localTree.root);
break;
case 5:
localTree.levelOrder(localTree.root);
break;
case 6:
int h = localTree.getHeight(localTree.root);
System.out.println("The height is" + h);
break;
case 7:
localTree.deleteTree();
break;
case 10:
flag = false;
break;
default:
break;
}
}
}