Java学习笔记之二叉树的节点数、深度

在上篇博文《Java学习笔记之创建二叉树》后,我们现在来求增加二叉树的节点数、二叉树的深度的函数,以下代码中黄色背景是增加实现的代码,由于注释较多,我用绿色字体将自己解读的注解区分。

老样子还是先注明这句话:【本文的代码请见原创http://blog.csdn.net/wuwenxiang91322/article/details/12231657】


我是结合他的代码一行行通过注释解读自己的理解,从中温习java的语法结构及数据结构、算法的思想。如有错误,望批评指正~

package tree;
import java.util.Stack;

/**
 * 二叉树的链式存储
 * @author WWX
 */

public class BinaryTree{
//二叉树通常用树结点结构存储,有时也包含指向唯一父节点的指针
private TreeNode root = null;

//BinTree()该方法与类名字相同,所以是构造方法,被默认强制为void
public  BinaryTree(){
root = new TreeNode(1,"A");
}

/*创建二叉树,树由结点构成
* <pre>
*           A
*     B          C
*  D     E            F
*  </pre>
* @param root
* @author WWX
*/

public void createBinTree(TreeNode root){
TreeNode newNodeB = new TreeNode(2,"B");
TreeNode newNodeC = new TreeNode(3,"C");
TreeNode newNodeD = new TreeNode(4,"D");
TreeNode newNodeE = new TreeNode(5,"E");
TreeNode newNodeF = new TreeNode(6,"F");
root.leftChild = newNodeB;
root.rightChild = newNodeC;
root.leftChild.leftChild = newNodeD;
root.leftChild.rightChild = newNodeE;
root.rightChild.rightChild = newNodeF;

}

//节点个数

//二叉树的递归思想,先定义根节点root

public int size(){
return size(root);
}

//二叉树递归思想:定义根节点root后,再用subtree实现递归
private int size(TreeNode subtree){
if (subtree == null)
return 0;
else
{
return 1+size(subtree.leftChild)+size(subtree.rightChild);
}
}


//树的高度
//二叉树递归思想,先定义root

public int height(){
return height(root);

}

//二叉树递归思想,定义root后,再用subtree实现递归
private int height(TreeNode subtree){
if (subtree == null)
return 0;
else {
int i = height(subtree.leftChild);
int j = height(subtree.rightChild);
return (i<j)?(j+1):(i+1);
//三目运算符知识,如果(i<j)是真的,则返回j+1,否则返回i+1
}

}


/**
* 二叉树的节点数据结构
* @author WWX
*/

private class TreeNode{
private int key = 0; //key 为层序编码
private String data = null; //data 为数据域
private boolean isVisted = false;

/*     树的每一个节点的数据结构都是TreeNode类型,
createBinTree里定义的root为TreeNode类型,所以左右孩子也为TreeNode类型,
加上二叉树的递归思想,所以所有节点都是TreeNode类型
        */ 

private TreeNode leftChild = null;
private TreeNode rightChild = null;



/*TreeNode(int key,String data) 方法名与类名相同,所以为构造方法

 为什么要用到this?
 因为在TreeNode()函数中需要该函数所属类的当前对象

//这里符合必须使用this的情况:
* 构造方法将外部传入的参数赋值给类的成员变量
* 构造方法的形式参数名称与类的成员变量名一致
*/

//构造方法的形式参数名称与类的成员变量名一致

public TreeNode(int key,String data){

this.key = key;//构造方法将外部传入的参数赋值给类的成员变量
this.data = null;
this.isVisted = false;
this.leftChild = null;
this.rightChild = null;

}
}


//测试
public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        //TreeNode root = new TreeNode(0, "R"); 
        
      /*这里方法里需要bt.root是由于root变量在BinaryTree类里定义的,

       *根据作用域定义,root只在BinaryTree类里起作用,其它地方调用root,都需要  bt.root
       * 比如:如果写成bt.createBinTree(root),就变成root就不属于BinarTree类了
      */

        bt.createBinTree(bt.root);
        System.out.println("the size of the tree is " + bt.size());
       System.out.println("the height of the tree is " + bt.height());

        
    }
}
 


打印的结果如下:

the size of the tree is 6
the height of the tree is 3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值