码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA二叉树的创建以及各种功能的实现

时间:2015-11-24 22:52:10      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

直接上代码了,代码说得很清楚了

package BTree;

public class BTree {
	private Node root;

	private class Node {
		private Node lchild;
		private Node rchild;
		private int data;

		public Node(int data) {
			this.lchild = null;
			this.rchild = null;
			this.data = data;
		}
	}

	public BTree() {
		root = null;
	}

	public Node getNode() {
		return root;
	}

	public void createTree(Node node, int data) {
		if (root == null) {
			root = new Node(data);
		} else {
			if (data < node.data) {
				if (node.lchild == null) {
					node.lchild = new Node(data);
				} else {
					createTree(node.lchild, data);
				}
			} else {
				if (node.rchild == null) {
					node.rchild = new Node(data);
				} else {
					createTree(node.rchild, data);
				}
			}
		}
	}
	/*前序遍历*/
	public void PreOrder(Node node) {
		if (node != null) {
			System.out.print(node.data + " ");
			PreOrder(node.lchild);
			PreOrder(node.rchild);
		}
	}
	/*中序遍历*/
	public void InOrder(Node node) {
		if (node != null) {
			InOrder(node.lchild);
			System.out.print(node.data + " ");
			InOrder(node.rchild);
		}
	}
	/*后序遍历*/
	public void TailOrder(Node node) {
		if (node != null) {
			TailOrder(node.lchild);
			TailOrder(node.rchild);
			System.out.print(node.data + " ");
		}
	}
	/*二叉树高度*/
	public int Depth(Node node){
		int dl = 0,dr =0;
		if(node!=null){
			dl = Depth(node.lchild);
			dr = Depth(node.rchild);
			if(dl>dr) return dl+1;
			return dr+1;
		}
		return 0;
	}
	/*节点的个数*/
	public int Point(Node node){
		if(node!=null){
			return Point(node.lchild)+Point(node.rchild)+1;
		}
		return 0;
	}
	/*叶子结点的个数*/
	public int Leaf(Node node){
		if(node==null) return 0;
		boolean flag = (node.lchild==null&&node.rchild==null);
		if(flag) return Leaf(node.lchild)+Leaf(node.rchild)+1;
		return Leaf(node.lchild)+Leaf(node.rchild);
	}
	/*出度为一的节点个数*/
	public int oneDegree(Node node){
		if(node==null) return 0;
	    boolean flag = (node.lchild==null&&node.rchild!=null)||(node.lchild!=null&&node.rchild==null);
	    if(flag) return oneDegree(node.lchild)+oneDegree(node.rchild)+1;
	    return oneDegree(node.lchild)+oneDegree(node.rchild);
	}
	/*出度为二的节点个数*/
	public int twoDegree(Node node){
		if(node==null) return 0;
	    boolean flag = (node.lchild!=null&&node.rchild!=null);
	    if(flag) return twoDegree(node.lchild)+twoDegree(node.rchild)+1;
	    return twoDegree(node.lchild)+twoDegree(node.rchild);
	}
}

  

package BTree;

public class BTreeDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int [] a ={4,3,2,6,11,8,9,10,1,5};
		//int [] a = {1,2,3,4,5};
		BTree bt = new BTree();
		for(int i=0;i<a.length;i++){
			bt.createTree(bt.getNode(), a[i]);
		}
		System.out.print("前序遍历:");
		bt.PreOrder(bt.getNode());
		System.out.println();
		System.out.print("中序遍历:");
		bt.InOrder(bt.getNode());
		System.out.println();
		System.out.print("后序遍历:");
		bt.TailOrder(bt.getNode());
		System.out.println();
		System.out.println("二叉树的高度:"+bt.Depth(bt.getNode()));
		System.out.println("节点的个数:"+bt.Point(bt.getNode()));
		System.out.println("叶子节点个数:"+bt.Leaf(bt.getNode()));
		System.out.println("出度为一的节点个数:"+bt.oneDegree(bt.getNode()));
		System.out.println("出度为二的节点个数:"+bt.twoDegree(bt.getNode()));
	}

}
前序遍历:4 3 2 1 6 5 11 8 9 10 
中序遍历:1 2 3 4 5 6 8 9 10 11 
后序遍历:1 2 3 5 10 9 8 11 6 4 
二叉树的高度:6
节点的个数:10
叶子节点个数:3
出度为一的节点个数:5
出度为二的节点个数:2

  

  技术分享

JAVA二叉树的创建以及各种功能的实现

标签:

原文地址:http://www.cnblogs.com/liyinggang/p/4993060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!