Java学习笔记之二叉树的节点数、深度
2014-12-30 09:06
411 查看
在上篇博文《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
老样子还是先注明这句话:【本文的代码请见原创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
相关文章推荐
- (Java学习笔记13)二叉树---删除二叉树节点
- 小蚂蚁学习数据结构(24)——求二叉树深度和度为2的节点个数代码实现
- 二叉树和递归的巩固--Java学习笔记(三)
- N个节点构成二叉树个数计算 -- JAVA 数据结构学习
- java事务学习笔记(九)--深度剖析JTA原理与实现
- [原创]java WEB学习笔记81:Hibernate学习之路--- 对象关系映射文件(.hbm.xml):hibernate-mapping 节点,class节点,id节点(主键生成策略),property节点,在hibernate 中 java类型 与sql类型之间的对应关系,Java 时间和日期类型的映射,Java 大对象类型 的 映射 (了解),映射组成关系
- java实现二叉树的建立,前中后序遍历,层次遍历,深度,节点个数等
- 数据结构与算法分析笔记与总结(java实现)--二叉树11:二叉树的深度
- 《剑指Offer》学习笔记--面试题39:二叉树的深度
- 二叉树和递归的巩固--Java学习笔记(三)
- 6Java学习笔记之数据结构——二叉树
- java学习笔记之创建二叉树
- 数据结构学习笔记(6)---二叉树的创建与求深度
- (Java学习笔记12)二叉树---(插入、查询、遍历)
- 二叉树和递归的巩固--Java学习笔记(三)
- java 二叉树(四)求二叉树的深度和节点总数
- 二叉树学习笔记-深度和宽度
- #数据结构与算法学习笔记#PTA10:层次遍历叶节点(JAVA)
- 深度学习Java类库deeplearning4j 学习笔记-MNIST手写数字分类问题
- java 学习笔记