二叉树基本操作--java实现

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;
		}
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值