二叉树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) ...
随机推荐
- php上传超大文件
1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...
- 过滤emoji表情的方法
public static function replaceEmoji($str) { $str = preg_replace_callback( '/./u', function (array $m ...
- Java如何获取ResultSet结果中的每一列的数据类型
示例代码片段: ResultSet resultSet = statement.executeQuery(sql); ResultSetMetaData metaData = resultSet.ge ...
- windows服务器入门 初始化数据盘
本人在寒假的时候自行搭建了一个服务器,在此分享一下我的方法.本人服务器的系统为Windows 2012R2 在后面的讲解中中文英文都会有 所以不用在意系统的语言问题 1)第一步 自然就是打开服 ...
- Python 面向对象总结
面向对象 类 class 类型 类变量 实例方法 init attack bite 类指针 - 指向父类 对象 对象指针 实例变量 self.name slef.age 组合 一个对象作为一个属性 s ...
- 20175316 盛茂淞 2018-2019-2 《Java程序设计》实验四 《Android程序设计》 实验报告
实验四 Android程序设计 实验要求 参考Android开发简易教程 完成云班课中的检查点,也可以先完成实验报告,直接提交.注意不能只有截图,要有知识点,原理,遇到的问题和解决过程等说明.实验报告 ...
- Snapshot Array
Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializ ...
- 【牛客网】Whalyzh's Problem
[牛客网]Whalyzh's Problem 每个\(b_{i,j}\)建一个点,认为选了\(b_{i,j}\)一定会选\(a_{i}\)和\(a_{j}\) 选了\(a_{i}\)的话会带了一个\( ...
- (七)mysql 记录长度
MySQL记录长度 MySQL中规定:任何一条记录最长不能超过 65535个字节: 这句话,就表明 varchar 永远也达不到理论值 : varchar 的实际存储长度能达到多少,是需要看具体的字符 ...
- Python class and object
# Python继承 class Person(object): """人""" def __init__(self, name, age) ...