java二叉树建立二叉树_JAVA二叉树的创建以及各种功能的实现

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

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

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

2866d54953af099c0ec74072011bc2af.png

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值