飞道的博客

java数据结构与算法之数结构------------二叉树

502人阅读  评论(0)

1.比较各种数据存储的方式

 1) 数组存储方式分析

    优点:通过下标的方式访问元素,速度快,对于有序数组,还可以使用二分查找提高检索速度。

    缺点:如果要检索每个具体的值,或者插入之(按一定的顺序)会整体移动,效率低。

  

 2)链式存储的方式分析:

   优点:在一定程度上对数组的存储的方式有优化(例如:插入一个数值节点,只需要将插入节点,链接到链表中即可,删除效率也高)。

  缺点:在进行检索时,效率仍然很低,(例如;要检索每个值时,需要从头开始遍历)。

 

3)数结构存储方式分析:

     能提高数据存储、读取的效率。例如利用二叉排序树,既可以保证数据的检索速度,同时也可以保证数据的插入、删除、修改的速度。

   

 

2. 树结构示意图

       

   数的常用术语:

   1. 节点   2. 根节点  3 .  父节点  4. 子节点    5.  叶子节点 (没有子节点的节点) 6. 节点的权(节点的值)

   7. 路径(从root 节点找到该节点的路径) 8 层 9 子树   10.数的高度 (最大层数) 11。森林:(多颗子树构成树林)

 

3.二叉树的概念

     每个节点最多只能有两个子节点的一种形式称为二叉树。 二叉树的子节点分为左节点和右节点。

  

     如果二叉树的所有叶子结点都在最后一层,并且节点总数=2^n -1, n为层数,则我们称为满二叉树。

     

    如果二叉树的所有叶子节点都在最后一层,或者倒数第二层,而且最后一层的叶子结点在左边连续倒数第二层的叶子节点在右边连续,我们称为完全二叉数。 

 

 

 4. 二叉数遍历说明

    使用前序、中序、后序对先下面的二叉数进行遍历

 1) 前序遍历:先输出父节点,在遍历左子树和右子树。

 2)中序遍历:先遍历左子树,在输出父节点,再遍历右子树。

3)后序遍历:先遍历左子树,在遍历右子树,最后输出父节点。

   根据输出父节点的前后顺序,确定是前序、中序、后序

5  二叉树遍历应用实例(前序、中序、后序)思路分析。

 6 代码实现


  
  1. package BinaryTreeDemo;
  2. public class BinaryTreeDemo {
  3. public static void main(String[] args) {
  4. //先需要创建一颗二叉树
  5. BinaryTree binaryTree = new BinaryTree();
  6. //创建需要的节点
  7. HeroNode root= new HeroNode( 1, "宋江");
  8. HeroNode noed2= new HeroNode( 2, "吴用");
  9. HeroNode noed3= new HeroNode( 3, "卢俊义");
  10. HeroNode noed4= new HeroNode( 4, "林冲");
  11. HeroNode noed5= new HeroNode( 5, "关胜");
  12. //我们先手动创建二叉树
  13. root.setLeft(noed2);
  14. root.setRight(noed3);
  15. noed3.setRight(noed4);
  16. noed4.setLeft(noed5);
  17. binaryTree.setRoot(root);
  18. //测试
  19. System. out.println( "前序遍历");
  20. binaryTree.preOrder();
  21. //测试
  22. System. out.println( "中序遍历");
  23. binaryTree.infixOrder();
  24. //测试
  25. System. out.println( "后序遍历");
  26. binaryTree.postOrder();
  27. }
  28. }
  29. //先创建HeroNode节点
  30. class HeroNode{
  31. private int no;
  32. private String name;
  33. private HeroNode left; //默认 null
  34. private HeroNode right; //默认null
  35. public HeroNode(int no, String name) {
  36. this.no = no;
  37. this.name = name;
  38. }
  39. public int getNo() {
  40. return no;
  41. }
  42. public void setNo(int no) {
  43. this.no = no;
  44. }
  45. public String getName() {
  46. return name;
  47. }
  48. public void setName(String name) {
  49. this.name = name;
  50. }
  51. public HeroNode getLeft() {
  52. return left;
  53. }
  54. public void setLeft(HeroNode left) {
  55. this.left = left;
  56. }
  57. public HeroNode getRight() {
  58. return right;
  59. }
  60. public void setRight(HeroNode right) {
  61. this.right = right;
  62. }
  63. @ Override
  64. public String toString( ) {
  65. return "HeroNode{" +
  66. "no=" + no +
  67. ", name='" + name + '\'' +
  68. '}';
  69. }
  70. //编写前序遍历的方法
  71. public void preOrder(){
  72. System. out.println( this); //先输出父节点
  73. //递归左子树前序遍历
  74. if ( this.left!= null){
  75. this.left.preOrder();
  76. }
  77. //递归右子树前序遍历
  78. if ( this.right!= null){
  79. this.right.preOrder();
  80. }
  81. }
  82. //编写中序遍历的方法
  83. public void infixOrder(){
  84. //递归左子树中序遍历
  85. if ( this.left!= null){
  86. this.left.infixOrder();
  87. }
  88. System. out.println( this); //先输出父节点
  89. //递归右子树中序遍历
  90. if ( this.right!= null){
  91. this.right.infixOrder();
  92. }
  93. }
  94. //编写后序遍历的方法
  95. public void postOrder(){
  96. //递归左子树后序遍历
  97. if ( this.left!= null){
  98. this.left.postOrder();
  99. }
  100. //递归右子树后序遍历
  101. if ( this.right!= null){
  102. this.right.postOrder();
  103. }
  104. System. out.println( this); //先输出父节点
  105. }
  106. }
  107. //定义BinaryTree二叉树
  108. class BinaryTree{
  109. private HeroNode root;
  110. public void setRoot(HeroNode root) {
  111. this.root = root;
  112. }
  113. //前序遍历
  114. public void preOrder() {
  115. if ( this.root != null) {
  116. this.root.preOrder();
  117. } else {
  118. System. out.println( "二叉树为空,无法遍历");
  119. }
  120. }
  121. //中序遍历
  122. public void infixOrder() {
  123. if ( this.root != null) {
  124. this.root.infixOrder();
  125. } else {
  126. System. out.println( "二叉树为空,无法遍历");
  127. }
  128. }
  129. //后续遍历
  130. public void postOrder() {
  131. if ( this.root != null) {
  132. this.root.postOrder();
  133. } else {
  134. System. out.println( "二叉树为空,无法遍历");
  135. }
  136. }
  137. }

 

 


转载:https://blog.csdn.net/weixin_45519487/article/details/106958826
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场