此测试仅用于二叉树基本的性质测试,不包含插入、删除测试(此类一般属于有序树基本操作)。

  1. //二叉树树类
  2. public class BinaryTree {
  3.  
  4. public TreeNode root; //有一个根节点
  5. public static int index;
  6.  
  7. public TreeNode CreateBTree(int[] a) {
  8. TreeNode root = null;
  9. if (a[index] != '#') {
  10. root = new TreeNode(a[index]);
  11. index++;
  12. root.setLChild(CreateBTree(a));
  13. index++;
  14. root.setRChild(CreateBTree(a));
  15. }
  16. return root;
  17.  
  18. }
  19.  
  20. //先序遍历
  21. public void prevOrder(TreeNode root) {
  22. if (root == null) {
  23. return;
  24. }
  25. System.out.print(root.getData() + ",");
  26. prevOrder(root.getLChild());
  27. prevOrder(root.getRChild());
  28. }
  29.  
  30. // 中序遍历
  31. public void midOrder(TreeNode root) {
  32. if (root == null) {
  33. return;
  34. }
  35. midOrder(root.getLChild());
  36. System.out.print(root.getData() + ",");
  37. midOrder(root.getRChild());
  38. }
  39.  
  40. // 后序遍历
  41. public void postOrder(TreeNode root) {
  42. if (root == null) {
  43. return;
  44. }
  45. postOrder(root.getLChild());
  46. postOrder(root.getRChild());
  47. System.out.print(root.getData() + ",");
  48. }
  49.  
  50. // 获取树大小
  51. private int getSize(TreeNode node) {
  52. if (node == null) {
  53. return 0;
  54. } else {
  55. return 1 + getSize(node.leftChild) + getSize(node.rightChild);
  56. }
  57. }
  58.  
  59. /*求二叉树的高*/
  60. public int getHeight() {
  61. return getHeight(this.root);
  62. }
  63.  
  64. private int getHeight(TreeNode node) {
  65. if (node != null) { //左子树和右子树中谁大返回谁
  66. int i = getHeight(node.leftChild);
  67. int j = getHeight(node.rightChild);
  68. return (i > j) ? i + 1 : j + 1;
  69. } else {
  70. return 0;
  71. }
  72. }
  73.  
  74. //获得叶子数
  75. public int getLeaf(TreeNode node) {
  76. if (node == null) {
  77. return 0;
  78. }
  79. if (node.leftChild == null && node.rightChild == null) {
  80. System.out.println("Leaf node: " + node.getData());
  81. return 1;
  82. } else {
  83. return getLeaf(node.leftChild) + getLeaf(node.rightChild);
  84. }
  85.  
  86. }
  87.  
  88. //获得第K层节点数
  89. public int getNodeKNum(TreeNode node, int k) {
  90. if (k == 1) {
  91. if (node == null)
  92. return 0;
  93. System.out.println("K Node:" + node.getData());
  94. return 1;
  95. }
  96. return getNodeKNum(node.getLChild(), k - 1) + getNodeKNum(node.getRChild(), k - 1);
  97. }
  98.  
  99. //查找某个节点
  100. public TreeNode findNode(int data) {
  101. return findNode(this.root, data);
  102. }
  103.  
  104. public TreeNode findNode(TreeNode node, int data) {
  105. if (node == null) {
  106. return null;
  107. } else if (node.getData() == data) {
  108. return node;
  109. }
  110. TreeNode leftNode = findNode(node.getLChild(), data);
  111. if (null != leftNode)
  112. return leftNode;
  113. TreeNode rightNode = findNode(node.getRChild(), data);
  114. if (null != rightNode)
  115. return rightNode;
  116. return null;
  117. }
  118.  
  119. //返回某节点的父节点
  120. public TreeNode getParent(int data) {
  121. return getParent(this.root, data);
  122. }
  123.  
  124. public TreeNode getParent(TreeNode node, int data) {
  125. if (node == null)
  126. return null;
  127. TreeNode childL = node.getLChild();
  128. TreeNode childR = node.getRChild();
  129. if ((childL != null && childL.getData() == data) || childR != null && childR.getData() == data)
  130. return node;
  131. TreeNode parentL = getParent(node.getLChild(), data);
  132. if (parentL != null)
  133. return parentL;
  134. TreeNode parentR = getParent(node.getRChild(), data);
  135. if (parentR != null)
  136. return parentR;
  137. return null;
  138. }
  139.  
  140. //层次遍历,用到队列
  141. public void BTreeLevelOrder() {
  142. TreeNode root = this.root;
  143. Queue<TreeNode> queue = new LinkedList<TreeNode>();
  144. LinkedList<TreeNode> list = new LinkedList<TreeNode>();
  145. queue.offer(root);
  146. while (!queue.isEmpty()) {
  147. TreeNode pre = queue.poll();
  148. list.add(pre);
  149. if (pre.getLChild() != null)
  150. queue.offer(pre.getLChild());
  151. if (pre.getRChild() != null)
  152. queue.offer(pre.getRChild());
  153. }
  154. Iterator<TreeNode> it = list.iterator();
  155. while (it.hasNext()) {
  156. TreeNode cur = it.next();
  157. System.out.print(cur.getData() + ", ");
  158. }
  159. }
  160.  
  161. //判断一棵树是否是完全二叉树(层次遍历的变形)
  162. public boolean isCompleteBTree() {
  163. TreeNode root = this.root;
  164. Queue<TreeNode> queue = new LinkedList<TreeNode>();
  165. queue.offer(root);
  166.  
  167. while (!queue.isEmpty()) {
  168. TreeNode node = queue.poll();
  169. if (node == null)
  170. break;
  171. queue.offer(node.getLChild());
  172. queue.offer(node.getRChild());
  173.  
  174. }
  175. while (!queue.isEmpty()) {
  176. TreeNode cur = queue.poll();
  177. if (cur != null)
  178. return false;
  179. }
  180. return true;
  181.  
  182. }
  183.  
  184. class TreeNode {
  185. private TreeNode leftChild;
  186. private TreeNode rightChild;
  187. private int data;
  188.  
  189. public TreeNode(int data) {
  190. this.data = data;
  191. }
  192.  
  193. public void setLChild(TreeNode left) {
  194. this.leftChild = left;
  195. }
  196.  
  197. public void setRChild(TreeNode right) {
  198. this.rightChild = right;
  199. }
  200.  
  201. public void setData(int data) {
  202. this.data = data;
  203. }
  204.  
  205. public int getData() {
  206. return this.data;
  207. }
  208.  
  209. public TreeNode getLChild() {
  210. return this.leftChild;
  211. }
  212.  
  213. public TreeNode getRChild() {
  214. return this.rightChild;
  215. }
  216. }
  217.  
  218. public static void main(String[] agrs) {
  219. BinaryTree tree = new BinaryTree();
  220. int[] a = new int[]{1, 2, 3, '#', '#', 4, '#', '#', 5, 6, '#', '#', '#'};
  221. // int[] a = new int[]{1, 2, '#', 3, '#', '#', 4, '#', 5, 6, '#', '#', '#'};
  222. // 1
  223. // / \
  224. // 2 5
  225. // / \ / \
  226. // 3 4 6 #
  227. // / \ / \ / \
  228. // # # # # # #
  229. tree.root = tree.CreateBTree(a);
  230. System.out.print("先序遍历:");
  231. tree.prevOrder(tree.root);
  232. System.out.print("\n中序遍历:");
  233. tree.midOrder(tree.root);
  234. System.out.print("\n后序遍历:");
  235. tree.postOrder(tree.root);
  236. System.out.println();
  237.  
  238. System.out.println("Tree size Num: " + tree.getSize(tree.root));
  239. System.out.println("Tree Leaf Num: " + tree.getLeaf(tree.root));
  240. System.out.println("K=2 num: " + tree.getNodeKNum(tree.root, 2));
  241. System.out.println("Tree height: " + tree.getHeight());
  242.  
  243. System.out.println("3 find: " + tree.findNode(3).getData());
  244. System.out.println("1 find: " + tree.findNode(1).getData());
  245. System.out.println("6 find: " + tree.findNode(6).getData());
  246. System.out.println("7 find: " + tree.findNode(7));
  247.  
  248. System.out.println("6 parent node is : " + tree.getParent(6).getData());
  249. System.out.println("3 paren node is : " + tree.getParent(3).getData());
  250. System.out.println("5 paren node is : " + tree.getParent(5).getData());
  251. System.out.println("1 paren node is : " + tree.getParent(1));
  252. System.out.print("层序遍历:");
  253. tree.BTreeLevelOrder();
  254. System.out.println();
  255.  
  256. System.out.println("the tree is complete? " + tree.isCompleteBTree());
  257.  
  258. }
  259. }

二叉树BinaryTree构建测试(无序)的更多相关文章

  1. 软件测试之构建测试---BVT

    1. 构建的基本流程: a. 开发人员在他们的个人计算机上编写源代码文件 b. 他们将编写好的文件存放在一个统一集中的地方,构建组将所有的源代码编译成可以在计算机上运行的二进制文件,且用安装工具把各种 ...

  2. java实现二叉树的构建以及3种遍历方法

    转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...

  3. docker构建测试环境

    构建测试环境首先要根据自己的需求,构建出适合自己项目的image,有了自己的image,就可以快速的搭建出来一套测试环境了. 下边就说一下构建image的两种方式. 1.DOCKFILE创建文件夹:m ...

  4. Java 实现二叉树的构建以及3种遍历方法

    转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...

  5. java实现二叉树的构建以及3种遍历方法(转)

    转 原地址:http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历 ...

  6. Maven创建Web工程并执行构建/测试/打包/部署

    创建工程基本参考上一篇Java Application工程,不同的是命令参数变了,创建Web工程的命令如下: mvn archetype:generate -DgroupId=com.jsoft.te ...

  7. Maven的构建/测试/打包

    继上一篇http://www.cnblogs.com/EasonJim/p/6809882.html使用Maven创建工程后,接下来是使用Maven进行构建/测试/打包. 在打包之前,先熟悉一下Mav ...

  8. 数据结构之二叉树的构建C++版

    二叉树的构建要注意与链式表的区别,二叉树这里的构建十分低级,每个树只是构建了一个单一的二叉树节点,总体来看是有下向上构建的.用户需要手动去构建自己需要的树,而不是直接去插入数据就到二叉树中了,因为不是 ...

  9. @vue/cli3+配置build命令构建测试包&正式包

    上一篇博客介绍了vue-cli2.x配置build命令构建测试包和正式包,但现在前端开发vue项目大多数使用新版@vue/cli脚手架搭建vue项目(vue create project-name) ...

随机推荐

  1. php上传超大文件

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  2. 过滤emoji表情的方法

    public static function replaceEmoji($str) { $str = preg_replace_callback( '/./u', function (array $m ...

  3. Java如何获取ResultSet结果中的每一列的数据类型

    示例代码片段: ResultSet resultSet = statement.executeQuery(sql); ResultSetMetaData metaData = resultSet.ge ...

  4. windows服务器入门 初始化数据盘

    本人在寒假的时候自行搭建了一个服务器,在此分享一下我的方法.本人服务器的系统为Windows 2012R2 在后面的讲解中中文英文都会有    所以不用在意系统的语言问题 1)第一步  自然就是打开服 ...

  5. Python 面向对象总结

    面向对象 类 class 类型 类变量 实例方法 init attack bite 类指针 - 指向父类 对象 对象指针 实例变量 self.name slef.age 组合 一个对象作为一个属性 s ...

  6. 20175316 盛茂淞 2018-2019-2 《Java程序设计》实验四 《Android程序设计》 实验报告

    实验四 Android程序设计 实验要求 参考Android开发简易教程 完成云班课中的检查点,也可以先完成实验报告,直接提交.注意不能只有截图,要有知识点,原理,遇到的问题和解决过程等说明.实验报告 ...

  7. Snapshot Array

    Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializ ...

  8. 【牛客网】Whalyzh's Problem

    [牛客网]Whalyzh's Problem 每个\(b_{i,j}\)建一个点,认为选了\(b_{i,j}\)一定会选\(a_{i}\)和\(a_{j}\) 选了\(a_{i}\)的话会带了一个\( ...

  9. (七)mysql 记录长度

    MySQL记录长度 MySQL中规定:任何一条记录最长不能超过 65535个字节: 这句话,就表明 varchar 永远也达不到理论值 : varchar 的实际存储长度能达到多少,是需要看具体的字符 ...

  10. Python class and object

    # Python继承 class Person(object): """人""" def __init__(self, name, age) ...