二叉树BinaryTree构建测试(无序)
此测试仅用于二叉树基本的性质测试,不包含插入、删除测试(此类一般属于有序树基本操作)。
//二叉树树类
public class BinaryTree { public TreeNode root; //有一个根节点
public static int index; public TreeNode CreateBTree(int[] a) {
TreeNode root = null;
if (a[index] != '#') {
root = new TreeNode(a[index]);
index++;
root.setLChild(CreateBTree(a));
index++;
root.setRChild(CreateBTree(a));
}
return root; } //先序遍历
public void prevOrder(TreeNode root) {
if (root == null) {
return;
}
System.out.print(root.getData() + ",");
prevOrder(root.getLChild());
prevOrder(root.getRChild());
} // 中序遍历
public void midOrder(TreeNode root) {
if (root == null) {
return;
}
midOrder(root.getLChild());
System.out.print(root.getData() + ",");
midOrder(root.getRChild());
} // 后序遍历
public void postOrder(TreeNode root) {
if (root == null) {
return;
}
postOrder(root.getLChild());
postOrder(root.getRChild());
System.out.print(root.getData() + ",");
} // 获取树大小
private int getSize(TreeNode node) {
if (node == null) {
return 0;
} else {
return 1 + getSize(node.leftChild) + getSize(node.rightChild);
}
} /*求二叉树的高*/
public int getHeight() {
return getHeight(this.root);
} private int getHeight(TreeNode node) {
if (node != null) { //左子树和右子树中谁大返回谁
int i = getHeight(node.leftChild);
int j = getHeight(node.rightChild);
return (i > j) ? i + 1 : j + 1;
} else {
return 0;
}
} //获得叶子数
public int getLeaf(TreeNode node) {
if (node == null) {
return 0;
}
if (node.leftChild == null && node.rightChild == null) {
System.out.println("Leaf node: " + node.getData());
return 1;
} else {
return getLeaf(node.leftChild) + getLeaf(node.rightChild);
} } //获得第K层节点数
public int getNodeKNum(TreeNode node, int k) {
if (k == 1) {
if (node == null)
return 0;
System.out.println("K Node:" + node.getData());
return 1;
}
return getNodeKNum(node.getLChild(), k - 1) + getNodeKNum(node.getRChild(), k - 1);
} //查找某个节点
public TreeNode findNode(int data) {
return findNode(this.root, data);
} public TreeNode findNode(TreeNode node, int data) {
if (node == null) {
return null;
} else if (node.getData() == data) {
return node;
}
TreeNode leftNode = findNode(node.getLChild(), data);
if (null != leftNode)
return leftNode;
TreeNode rightNode = findNode(node.getRChild(), data);
if (null != rightNode)
return rightNode;
return null;
} //返回某节点的父节点
public TreeNode getParent(int data) {
return getParent(this.root, data);
} public TreeNode getParent(TreeNode node, int data) {
if (node == null)
return null;
TreeNode childL = node.getLChild();
TreeNode childR = node.getRChild();
if ((childL != null && childL.getData() == data) || childR != null && childR.getData() == data)
return node;
TreeNode parentL = getParent(node.getLChild(), data);
if (parentL != null)
return parentL;
TreeNode parentR = getParent(node.getRChild(), data);
if (parentR != null)
return parentR;
return null;
} //层次遍历,用到队列
public void BTreeLevelOrder() {
TreeNode root = this.root;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
LinkedList<TreeNode> list = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode pre = queue.poll();
list.add(pre);
if (pre.getLChild() != null)
queue.offer(pre.getLChild());
if (pre.getRChild() != null)
queue.offer(pre.getRChild());
}
Iterator<TreeNode> it = list.iterator();
while (it.hasNext()) {
TreeNode cur = it.next();
System.out.print(cur.getData() + ", ");
}
} //判断一棵树是否是完全二叉树(层次遍历的变形)
public boolean isCompleteBTree() {
TreeNode root = this.root;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root); while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node == null)
break;
queue.offer(node.getLChild());
queue.offer(node.getRChild()); }
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if (cur != null)
return false;
}
return true; } class TreeNode {
private TreeNode leftChild;
private TreeNode rightChild;
private int data; public TreeNode(int data) {
this.data = data;
} public void setLChild(TreeNode left) {
this.leftChild = left;
} public void setRChild(TreeNode right) {
this.rightChild = right;
} public void setData(int data) {
this.data = data;
} public int getData() {
return this.data;
} public TreeNode getLChild() {
return this.leftChild;
} public TreeNode getRChild() {
return this.rightChild;
}
} public static void main(String[] agrs) {
BinaryTree tree = new BinaryTree();
int[] a = new int[]{1, 2, 3, '#', '#', 4, '#', '#', 5, 6, '#', '#', '#'};
// int[] a = new int[]{1, 2, '#', 3, '#', '#', 4, '#', 5, 6, '#', '#', '#'};
// 1
// / \
// 2 5
// / \ / \
// 3 4 6 #
// / \ / \ / \
// # # # # # #
tree.root = tree.CreateBTree(a);
System.out.print("先序遍历:");
tree.prevOrder(tree.root);
System.out.print("\n中序遍历:");
tree.midOrder(tree.root);
System.out.print("\n后序遍历:");
tree.postOrder(tree.root);
System.out.println(); System.out.println("Tree size Num: " + tree.getSize(tree.root));
System.out.println("Tree Leaf Num: " + tree.getLeaf(tree.root));
System.out.println("K=2 num: " + tree.getNodeKNum(tree.root, 2));
System.out.println("Tree height: " + tree.getHeight()); System.out.println("3 find: " + tree.findNode(3).getData());
System.out.println("1 find: " + tree.findNode(1).getData());
System.out.println("6 find: " + tree.findNode(6).getData());
System.out.println("7 find: " + tree.findNode(7)); System.out.println("6 parent node is : " + tree.getParent(6).getData());
System.out.println("3 paren node is : " + tree.getParent(3).getData());
System.out.println("5 paren node is : " + tree.getParent(5).getData());
System.out.println("1 paren node is : " + tree.getParent(1));
System.out.print("层序遍历:");
tree.BTreeLevelOrder();
System.out.println(); System.out.println("the tree is complete? " + tree.isCompleteBTree()); }
}
二叉树BinaryTree构建测试(无序)的更多相关文章
- 软件测试之构建测试---BVT
1. 构建的基本流程: a. 开发人员在他们的个人计算机上编写源代码文件 b. 他们将编写好的文件存放在一个统一集中的地方,构建组将所有的源代码编译成可以在计算机上运行的二进制文件,且用安装工具把各种 ...
- java实现二叉树的构建以及3种遍历方法
转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...
- docker构建测试环境
构建测试环境首先要根据自己的需求,构建出适合自己项目的image,有了自己的image,就可以快速的搭建出来一套测试环境了. 下边就说一下构建image的两种方式. 1.DOCKFILE创建文件夹:m ...
- Java 实现二叉树的构建以及3种遍历方法
转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...
- java实现二叉树的构建以及3种遍历方法(转)
转 原地址:http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历 ...
- Maven创建Web工程并执行构建/测试/打包/部署
创建工程基本参考上一篇Java Application工程,不同的是命令参数变了,创建Web工程的命令如下: mvn archetype:generate -DgroupId=com.jsoft.te ...
- Maven的构建/测试/打包
继上一篇http://www.cnblogs.com/EasonJim/p/6809882.html使用Maven创建工程后,接下来是使用Maven进行构建/测试/打包. 在打包之前,先熟悉一下Mav ...
- 数据结构之二叉树的构建C++版
二叉树的构建要注意与链式表的区别,二叉树这里的构建十分低级,每个树只是构建了一个单一的二叉树节点,总体来看是有下向上构建的.用户需要手动去构建自己需要的树,而不是直接去插入数据就到二叉树中了,因为不是 ...
- @vue/cli3+配置build命令构建测试包&正式包
上一篇博客介绍了vue-cli2.x配置build命令构建测试包和正式包,但现在前端开发vue项目大多数使用新版@vue/cli脚手架搭建vue项目(vue create project-name) ...
随机推荐
- json简单案例
1.Group类 import java.util.ArrayList; import java.util.List; class Group{ private int id; private Str ...
- Jsoup-简单爬取知乎推荐页面(附:get_agent())
总览 今天我们就来小用一下Jsoup,从一个整体的角度来看一看爬虫 一个基本的爬虫框架包括: [x] 解析网页 [x] 失败重试 [x] 抓取内容保存至本地 [x] 多线程抓取 *** 分模块讲解 将 ...
- hdfs中删除文件、文件夹、抓取内容
删除文件 bin/hdfs dfs -rm output2/* 删除文件夹 bin/hdfs dfs -rm -r output2 抓取内容 bin/hdfs dfs -cat /us ...
- END使用
[root@bogon ~]# cat d.sh #!/bin/bash#. /etc/init.d/functionscat <<END+------------------------ ...
- ubuntu18和windows10双系统时间不同步问题(Ubuntu)
1.安装并校准时间 sudo apt install ntpdate sudo ntpdate time.windows.com 2.写入硬件配置 sudo hwclock --localtime - ...
- k8s集群监控 cadvisor/exporter+prometheus+grafana
### k8s监控处理 ### 1.cadvisor/exporter+prometheus+grafana 安装#### 1.1 配置nfs安装```shellubuntu: nfs 服务器 apt ...
- GB2312、GBK、GB18030 这几种字符集的主要区别
1 GB2312-80 GB 2312 或 GB 2312-80 是中国国家标准简体中文字符集,全称<信息交换用汉字编码字符集·基本集>,又称 GB 0,由中国国家标准总局发布,1981 ...
- 20191011-构建我们公司自己的自动化接口测试框架-ProVar模块
ProVar模块主要定义测试数据所在目录,以及定义变量和测试数据excel里面的column对应这样后续在进行excel操作的时候直接使用变量即可进行操作,后期excel的column有增删的时候,修 ...
- Linux驱动函数解读
一.kmalloc().kzalloc()和vmalloc() 这三个函数都可以分配连续的虚拟内存 除此之外,这三个函数的区别有: 1. kmalloc()和kzalloc()函数分配的物理内存也是连 ...
- SAS学习笔记13 SAS数据清洗和加工(续)
查找缺失值 cha[*]和num[*]是建立数组cha和num,但不指定数组中的元素数 自动变量_character_表示数据集中的所有字符型变量 自动变量_numeric_表示数据集中的所有数值型变 ...