二叉树和二叉树的遍历_Suger 二叉树的层次遍历

1. 二叉树

一般的树限制比较少,所以才提出了具有特色的二叉树的概念。二叉树顾名思义,每个节点最多有两个子节点,分别叫做左子节点和右子节点。有了这个限定性后,就可以干很多树不能干的事情了。如果树的所有层,除了最后一层的节点外都是两个子节点,那么称这个树为满二叉树。如下图

若设二叉树的高度为h,除第h 层外,其它各层 (1~h-1)的结点数都达到最大个数,第 h层所有的节点都连续集中在最左边,这就是完全二叉树。

2.二叉树的操作

二叉树具有为指定节点增加子节点操作、判断树是否为空、返回根节点、返回指定节点的父节点,返回指定节点的左子节点、返回指定节点的右子节点、返回树的深度、返回指定节点的位置。

3.二叉树的延伸

其实二叉树只是一个引子,计算机界很多的算法都是根据二叉树所展开的,比如排序二叉树、红黑树、哈夫曼树、线索二叉树等等。

4.顺序实现二叉树

下面我们来看看二叉树的顺序实现方式,顺序实现二叉树就是利用数组存储所有的二叉树的节点。代码如下

Java代码
  1. packagedateStructer.tree.binaryTree;
  2. public classArrayBinaryTree<T>{
  3. // 树的默认深度
  4. private static final intDefTreeDeep = 4;
  5. // 节点数组
  6. private Object[]datas;
  7. // 指定的树的深度
  8. private inttreeDeep;
  9. // 实际的数组个数
  10. private intarraySize;
  11. public ArrayBinaryTree(){
  12. // 设置默认的树深度
  13. treeDeep = DefTreeDeep;
  14. //2的DefTreeDeep次方-1个数组元素
  15. arraySize = (int)Math.pow(2, DefTreeDeep) - 1;
  16. datas = newObject[arraySize];
  17. }
  18. publicArrayBinaryTree(intdeep) {
  19. // 按指定深度
  20. treeDeep = deep;
  21. arraySize = (int)Math.pow(2, treeDeep) - 1;
  22. datas = newObject[arraySize];
  23. }
  24. publicArrayBinaryTree(intdeep, T data) {
  25. // 按指定深度
  26. treeDeep = deep;
  27. arraySize = (int)Math.pow(2, treeDeep) - 1;
  28. datas = newObject[arraySize];
  29. datas[0] = data;
  30. }
  31. public booleanaddNode(int index, T data,boolean isLeft){
  32. if (index *2+ 2 > arraySize ||datas[index] == null) {
  33. throw newRuntimeException("标记无效");
  34. }
  35. if (isLeft) {
  36. datas[index * 2 + 1] =data;
  37. } else {
  38. datas[index * 2 + 2] =data;
  39. }
  40. return true;
  41. }
  42. public boolean isEmpty(){
  43. return arraySize ==0;
  44. }
  45. @SuppressWarnings("unchecked")
  46. public T getRoot(){
  47. return (T)datas[0];
  48. }
  49. @SuppressWarnings("unchecked")
  50. public TgetParent(int index) {
  51. if (index >arraySize || datas[index] == null) {
  52. throw newRuntimeException("标记无效");
  53. }
  54. if (datas[(index -1)/ 2] == null) {
  55. throw newRuntimeException("无父节点");
  56. }
  57. return (T) datas[(index- 1) / 2];
  58. }
  59. @SuppressWarnings("unchecked")
  60. public TgetLeftNode(int index) {
  61. if (index *2+ 2 > arraySize ||datas[index] == null) {
  62. throw newRuntimeException("标记无效");
  63. }
  64. return (T) datas[index* 2+ 1];
  65. }
  66. @SuppressWarnings("unchecked")
  67. public TgetRightNode(int index) {
  68. if (index *2+ 2 > arraySize ||datas[index] == null) {
  69. throw newRuntimeException("标记无效");
  70. }
  71. return (T) datas[index* 2+ 2];
  72. }
  73. public intgetTreeDeep() {
  74. return treeDeep;
  75. }
  76. public intgetNodeIndex(T data) {
  77. for (inti = 0; i < arraySize; i++){
  78. if (data == datas[i]){
  79. return i;
  80. }
  81. }
  82. return -1;
  83. }
  84. @Override
  85. public String toString(){
  86. StringBuffer str = newStringBuffer("[");
  87. for (inti = 0; i < datas.length;i++) {
  88. str.append("[" + datas[i] + "],");
  89. }
  90. if (datas.length> 0) {
  91. returnstr.substring(0,str.lastIndexOf(",")) + "]";
  92. }
  93. returnstr.append("]").toString();
  94. }
  95. }
package dateStructer.tree.binaryTree;public class ArrayBinaryTree<T> {        // 树的默认深度        private static final int DefTreeDeep = 4;        // 节点数组        private Object[] datas;        // 指定的树的深度        private int treeDeep;        // 实际的数组个数        private int arraySize;                public ArrayBinaryTree() {                // 设置默认的树深度                treeDeep = DefTreeDeep;                // 2的DefTreeDeep次方-1个数组元素                arraySize = (int) Math.pow(2, DefTreeDeep) - 1;                datas = new Object[arraySize];        }                public ArrayBinaryTree(int deep) {                // 按指定深度                treeDeep = deep;                arraySize = (int) Math.pow(2, treeDeep) - 1;                datas = new Object[arraySize];        }                public ArrayBinaryTree(int deep, T data) {                // 按指定深度                treeDeep = deep;                arraySize = (int) Math.pow(2, treeDeep) - 1;                datas = new Object[arraySize];                datas[0] = data;        }                public boolean addNode(int index, T data, boolean isLeft) {                if (index * 2 + 2 > arraySize || datas[index] == null) {                        throw new RuntimeException("标记无效");                }                if (isLeft) {                        datas[index * 2 + 1] = data;                } else {                        datas[index * 2 + 2] = data;                }                return true;        }                public boolean isEmpty() {                return arraySize == 0;        }                @SuppressWarnings("unchecked")        public T getRoot() {                return (T) datas[0];        }                @SuppressWarnings("unchecked")        public T getParent(int index) {                if (index > arraySize || datas[index] == null) {                        throw new RuntimeException("标记无效");                }                if (datas[(index - 1) / 2] == null) {                        throw new RuntimeException("无父节点");                }                return (T) datas[(index - 1) /  2];        }                @SuppressWarnings("unchecked")        public T getLeftNode(int index) {                if (index * 2 + 2 > arraySize || datas[index] == null) {                        throw new RuntimeException("标记无效");                }                return (T) datas[index * 2 + 1];        }                @SuppressWarnings("unchecked")        public T getRightNode(int index) {                if (index * 2 + 2 > arraySize || datas[index] == null) {                        throw new RuntimeException("标记无效");                }                return (T) datas[index * 2 + 2];        }                public int getTreeDeep() {                return treeDeep;        }                public int getNodeIndex(T data) {                for (int i = 0; i < arraySize; i++) {                        if (data == datas[i]) {                                return i;                        }                }                return -1;        }        @Override        public String toString() {                StringBuffer str = new StringBuffer("[");                for (int i = 0; i < datas.length; i++) {                        str.append("[" + datas[i] + "],");                }                if (datas.length > 0) {                        return str.substring(0, str.lastIndexOf(",")) + "]";                }                return str.append("]").toString();        }}

测试代码如下

Java代码
  1. public static voidmain(String[] args) {
  2. ArrayBinaryTree<String>arrayBinaryTree = newArrayBinaryTree<String>(4,"汉献帝");
  3. System.out.println(arrayBinaryTree);
  4. arrayBinaryTree.addNode(0,"刘备", true);
  5. arrayBinaryTree.addNode(0,"曹操", false);
  6. arrayBinaryTree.addNode(1,"关羽", true);
  7. arrayBinaryTree.addNode(1,"张飞", false);
  8. arrayBinaryTree.addNode(2,"张辽", true);
  9. arrayBinaryTree.addNode(2,"许褚", false);
  10. System.out.println(arrayBinaryTree);
  11. System.out.println(arrayBinaryTree.getLeftNode(1));
  12. System.out.println(arrayBinaryTree.getRightNode(0));
  13. System.out.println(arrayBinaryTree.isEmpty());
  14. System.out.println(arrayBinaryTree.getParent(4));
  15. }
public static void main(String[] args) {                ArrayBinaryTree<String> arrayBinaryTree = new ArrayBinaryTree<String>(4,"汉献帝");                System.out.println(arrayBinaryTree);                arrayBinaryTree.addNode(0, "刘备", true);                arrayBinaryTree.addNode(0, "曹操", false);                arrayBinaryTree.addNode(1, "关羽", true);                arrayBinaryTree.addNode(1, "张飞", false);                arrayBinaryTree.addNode(2, "张辽", true);                arrayBinaryTree.addNode(2, "许褚", false);                System.out.println(arrayBinaryTree);                System.out.println(arrayBinaryTree.getLeftNode(1));                System.out.println(arrayBinaryTree.getRightNode(0));                System.out.println(arrayBinaryTree.isEmpty());                System.out.println(arrayBinaryTree.getParent(4));        }

测试效果如下

顺序实现是比较浪费资源的,可以看到数组没有元素的位置都是null。如果将测试代码稍微变更一下,如下因为树本身就是具有递归性质的结构。

Java代码
  1. public static voidmain(String[] args) {
  2. ArrayBinaryTree<String>arrayBinaryTree = newArrayBinaryTree<String>(4,"汉献帝");
  3. System.out.println(arrayBinaryTree);
  4. arrayBinaryTree.addNode(0,"刘备", true);
  5. arrayBinaryTree.addNode(0,"曹操", false);
  6. arrayBinaryTree.addNode(2,"张辽", true);
  7. arrayBinaryTree.addNode(2,"许褚", false);
  8. arrayBinaryTree.addNode(6,"李典", true);
  9. arrayBinaryTree.addNode(6,"乐进", false);
  10. System.out.println(arrayBinaryTree);
  11. System.out.println(arrayBinaryTree.getLeftNode(2));
  12. System.out.println(arrayBinaryTree.getRightNode(0));
  13. System.out.println(arrayBinaryTree.isEmpty());
  14. System.out.println(arrayBinaryTree.getParent(14));
  15. }
public static void main(String[] args) {                ArrayBinaryTree<String> arrayBinaryTree = new ArrayBinaryTree<String>(4,"汉献帝");                System.out.println(arrayBinaryTree);                arrayBinaryTree.addNode(0, "刘备", true);                arrayBinaryTree.addNode(0, "曹操", false);                arrayBinaryTree.addNode(2, "张辽", true);                arrayBinaryTree.addNode(2, "许褚", false);                arrayBinaryTree.addNode(6, "李典", true);                arrayBinaryTree.addNode(6, "乐进", false);                System.out.println(arrayBinaryTree);                System.out.println(arrayBinaryTree.getLeftNode(2));                System.out.println(arrayBinaryTree.getRightNode(0));                System.out.println(arrayBinaryTree.isEmpty());                System.out.println(arrayBinaryTree.getParent(14));        }

运行效果如下

可以看到数组中间资源浪费得很严重。

5.二叉链表实现二叉树

为了弥补顺序实现的空间浪费问题,可以使用链表方式实现二叉树,但是链表又分为两种情况,一种是二叉链表,另一种稍后再说。二叉链表的思想就是构造一个对象,记住它的两个子节点,所谓记住两个子节点可以是子节点的位置,可以是子节点的实体对象。如果记录了位置,其实是离不开数组的帮助的。如果记录了整个子节点对象,那么就可以完全脱离数组,完完全全,真真正正的链表离散式存储。这次使用记录节点位置,算法如下

Java代码
  1. packagedateStructer.tree.binaryTree;
  2. public classTwoLinkedBinaryTree<T>{
  3. // 树的默认深度
  4. private static final intDefTreeDeep = 4;
  5. // 节点数组
  6. privateTwoLinkNode<T>[]datas;
  7. // 指定的树的深度
  8. private inttreeDeep;
  9. // 实际的数组个数
  10. private intarraySize;
  11. //节点个数
  12. private intnodeSize;
  13. @SuppressWarnings("hiding")
  14. classTwoLinkNode<T> {
  15. public intleftChildIndex;
  16. public intrightChildIndex;
  17. public intindex;
  18. public T data;
  19. }
  20. @SuppressWarnings("unchecked")
  21. public TwoLinkedBinaryTree(){
  22. treeDeep = DefTreeDeep;
  23. arraySize = (int)Math.pow(2, treeDeep) - 1;
  24. datas = newTwoLinkNode[arraySize];
  25. }
  26. @SuppressWarnings("unchecked")
  27. publicTwoLinkedBinaryTree(intdeep, T data) {
  28. treeDeep = DefTreeDeep;
  29. arraySize = (int)Math.pow(2, treeDeep) - 1;
  30. datas = newTwoLinkNode[arraySize];
  31. TwoLinkNode<T> twoLinkNode= newTwoLinkNode<T>();
  32. twoLinkNode.data = data;
  33. twoLinkNode.leftChildIndex = 1;
  34. twoLinkNode.rightChildIndex = 2;
  35. twoLinkNode.index = 0;
  36. datas[0] = twoLinkNode;
  37. nodeSize = 1;
  38. }
  39. public booleanaddNode(int index, T data,boolean isLeft){
  40. if (index +1> arraySize || datas[index] ==null) {
  41. throw newRuntimeException("标记无效");
  42. }
  43. for (inti = index + 1; i < arraySize; i++){
  44. if (datas[i] ==null) {
  45. TwoLinkNode<T> twoLinkNode= newTwoLinkNode<T>();
  46. twoLinkNode.data = data;
  47. twoLinkNode.index = i;
  48. datas[i] = twoLinkNode;
  49. if (isLeft) {
  50. datas[index].leftChildIndex = i;
  51. } else {
  52. datas[index].rightChildIndex = i;
  53. }
  54. nodeSize ++;
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. public boolean isEmpty(){
  61. return nodeSize ==0;
  62. }
  63. @SuppressWarnings("unchecked")
  64. public T getRoot(){
  65. return (T)datas[0];
  66. }
  67. public TgetParent(int index) {
  68. if (index >arraySize || datas[index] == null) {
  69. throw newRuntimeException("标记无效");
  70. }
  71. for (inti = 0; i < arraySize; i++){
  72. if (datas[i] !=null) {
  73. if (datas[i].leftChildIndex== index
  74. || datas[i].rightChildIndex == index) {
  75. returndatas[i].data;
  76. }
  77. }
  78. }
  79. return null;
  80. }
  81. public TgetLeftNode(int index) {
  82. if (index +2> arraySize || datas[index] ==null) {
  83. throw newRuntimeException("标记无效");
  84. }
  85. return (T)datas[datas[index].leftChildIndex].data;
  86. }
  87. public TgetRightNode(int index) {
  88. if (index +2> arraySize || datas[index] ==null) {
  89. throw newRuntimeException("标记无效");
  90. }
  91. return (T)datas[datas[index].rightChildIndex].data;
  92. }
  93. public intgetTreeDeep() {
  94. return treeDeep;
  95. }
  96. public intgetNodeIndex(T data) {
  97. for (inti = 0; i < arraySize; i++){
  98. if (data == datas[i]){
  99. return i;
  100. }
  101. }
  102. return -1;
  103. }
  104. @Override
  105. public String toString(){
  106. StringBuffer str = newStringBuffer("[");
  107. for (inti = 0; i < nodeSize; i++){
  108. if(datas[i].data !=null){
  109. str.append("[" + datas[i].data +"],");
  110. }
  111. }
  112. if (datas.length> 0) {
  113. returnstr.substring(0,str.lastIndexOf(",")) + "]";
  114. }
  115. returnstr.append("]").toString();
  116. }
  117. }
package dateStructer.tree.binaryTree;public class TwoLinkedBinaryTree<T> {        // 树的默认深度        private static final int DefTreeDeep = 4;        // 节点数组        private TwoLinkNode<T>[] datas;        // 指定的树的深度        private int treeDeep;        // 实际的数组个数        private int arraySize;                //节点个数        private int nodeSize;                @SuppressWarnings("hiding")        class TwoLinkNode<T> {                public int leftChildIndex;                public int rightChildIndex;                public int index;                public T data;        }        @SuppressWarnings("unchecked")        public TwoLinkedBinaryTree() {                treeDeep = DefTreeDeep;                arraySize = (int) Math.pow(2, treeDeep) - 1;                datas = new TwoLinkNode[arraySize];        }        @SuppressWarnings("unchecked")        public TwoLinkedBinaryTree(int deep, T data) {                treeDeep = DefTreeDeep;                arraySize = (int) Math.pow(2, treeDeep) - 1;                datas = new TwoLinkNode[arraySize];                TwoLinkNode<T> twoLinkNode = new TwoLinkNode<T>();                twoLinkNode.data = data;                twoLinkNode.leftChildIndex = 1;                twoLinkNode.rightChildIndex = 2;                twoLinkNode.index = 0;                datas[0] = twoLinkNode;                nodeSize = 1;        }                public boolean addNode(int index, T data, boolean isLeft) {                if (index + 1 > arraySize || datas[index] == null) {                        throw new RuntimeException("标记无效");                }                for (int i = index + 1; i < arraySize; i++) {                        if (datas[i] == null) {                                TwoLinkNode<T> twoLinkNode = new TwoLinkNode<T>();                                twoLinkNode.data = data;                                twoLinkNode.index = i;                                datas[i] = twoLinkNode;                                if (isLeft) {                                        datas[index].leftChildIndex = i;                                } else {                                        datas[index].rightChildIndex = i;                                }                                nodeSize ++;                                return true;                        }                }                return false;        }                public boolean isEmpty() {                return nodeSize == 0;        }                @SuppressWarnings("unchecked")        public T getRoot() {                return (T) datas[0];        }                public T getParent(int index) {                if (index > arraySize || datas[index] == null) {                        throw new RuntimeException("标记无效");                }                for (int i = 0; i < arraySize; i++) {                        if (datas[i] != null) {                                if (datas[i].leftChildIndex == index                                                || datas[i].rightChildIndex == index) {                                        return datas[i].data;                                }                        }                }                return null;        }                public T getLeftNode(int index) {                if (index + 2 > arraySize || datas[index] == null) {                        throw new RuntimeException("标记无效");                }                return (T) datas[datas[index].leftChildIndex].data;        }                public T getRightNode(int index) {                if (index + 2 > arraySize || datas[index] == null) {                        throw new RuntimeException("标记无效");                }                return (T) datas[datas[index].rightChildIndex].data;        }                public int getTreeDeep() {                return treeDeep;        }                public int getNodeIndex(T data) {                for (int i = 0; i < arraySize; i++) {                        if (data == datas[i]) {                                return i;                        }                }                return -1;        }        @Override        public String toString() {                StringBuffer str = new StringBuffer("[");                for (int i = 0; i < nodeSize; i++) {                        if(datas[i].data != null){                                str.append("[" + datas[i].data + "],");                        }                }                if (datas.length > 0) {                        return str.substring(0, str.lastIndexOf(",")) + "]";                }                return str.append("]").toString();        }        }

使用这种实现其实是想利用好数组的空间。别让中间任何的节点空间白白浪费了。但是可以发现找父节点的时候比较麻烦。还得遍历一下整个节点。三叉链表就不必遍历,因为三叉链表比二叉链表多了记录了一个节点,那就是此节点的父节点。无论是父节点的位置或者父节点实体,都是一样的思想。

6.三叉链表实现二叉树

下面我们基于上面的二叉链表形式编写三叉链表。代码如下

Java代码
  1. packagedateStructer.tree.binaryTree;
  2. public classThreeLinkedBinaryTree<T>{
  3. // 树的默认深度
  4. private static final intDefTreeDeep = 4;
  5. // 节点数组
  6. privateThreeLinkNode<T>[]datas;
  7. // 指定的树的深度
  8. private inttreeDeep;
  9. // 实际的数组个数
  10. private intarraySize;
  11. // 节点个数
  12. private intnodeSize;
  13. @SuppressWarnings("hiding")
  14. classThreeLinkNode<T> {
  15. public intparentIndex;
  16. public intleftChildIndex;
  17. public intrightChildIndex;
  18. public intindex;
  19. public T data;
  20. }
  21. @SuppressWarnings("unchecked")
  22. publicThreeLinkedBinaryTree() {
  23. treeDeep = DefTreeDeep;
  24. arraySize = (int)Math.pow(2, treeDeep) - 1;
  25. datas = newThreeLinkNode[arraySize];
  26. }
  27. @SuppressWarnings("unchecked")
  28. publicThreeLinkedBinaryTree(intdeep, T data) {
  29. treeDeep = DefTreeDeep;
  30. arraySize = (int)Math.pow(2, treeDeep) - 1;
  31. datas = newThreeLinkNode[arraySize];
  32. ThreeLinkNode<T>threeLinkNode = newThreeLinkNode<T>();
  33. threeLinkNode.data = data;
  34. threeLinkNode.leftChildIndex = 1;
  35. threeLinkNode.rightChildIndex = 2;
  36. threeLinkNode.index = 0;
  37. threeLinkNode.parentIndex = -1;
  38. datas[0] = threeLinkNode;
  39. nodeSize = 1;
  40. }
  41. public booleanaddNode(int index, T data,boolean isLeft){
  42. if (index +1> arraySize || datas[index] ==null) {
  43. throw newRuntimeException("标记无效");
  44. }
  45. for (inti = index + 1; i < arraySize; i++){
  46. if (datas[i] ==null) {
  47. ThreeLinkNode<T>threeLinkNode = newThreeLinkNode<T>();
  48. threeLinkNode.data = data;
  49. threeLinkNode.index = i;
  50. threeLinkNode.parentIndex = index;
  51. datas[i] = threeLinkNode;
  52. if (isLeft) {
  53. datas[index].leftChildIndex = i;
  54. } else {
  55. datas[index].rightChildIndex = i;
  56. }
  57. nodeSize++;
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. public boolean isEmpty(){
  64. return nodeSize ==0;
  65. }
  66. @SuppressWarnings("unchecked")
  67. public T getRoot(){
  68. return (T)datas[0];
  69. }
  70. @SuppressWarnings("unchecked")
  71. public TgetParent(int index) {
  72. if (index >arraySize || datas[index] == null) {
  73. throw newRuntimeException("标记无效");
  74. }
  75. return (T)datas[datas[index].parentIndex];
  76. }
  77. public TgetLeftNode(int index) {
  78. if (index +2> arraySize || datas[index] ==null
  79. || datas[datas[index].leftChildIndex] ==null) {
  80. throw newRuntimeException("标记无效");
  81. }
  82. return (T)datas[datas[index].leftChildIndex].data;
  83. }
  84. public TgetRightNode(int index) {
  85. if (index +2> arraySize || datas[index] ==null
  86. || datas[datas[index].rightChildIndex] ==null) {
  87. throw newRuntimeException("标记无效");
  88. }
  89. return (T)datas[datas[index].rightChildIndex].data;
  90. }
  91. public intgetTreeDeep() {
  92. return treeDeep;
  93. }
  94. public intgetNodeIndex(T data) {
  95. for (inti = 0; i < arraySize; i++){
  96. if (data == datas[i]){
  97. return i;
  98. }
  99. }
  100. return -1;
  101. }
  102. @Override
  103. public String toString(){
  104. StringBuffer str = newStringBuffer("[");
  105. for (inti = 0; i < nodeSize; i++){
  106. if (datas[i].data !=null) {
  107. str.append("[" + datas[i].data +"],");
  108. }
  109. }
  110. if (datas.length> 0) {
  111. returnstr.substring(0,str.lastIndexOf(",")) + "]";
  112. }
  113. returnstr.append("]").toString();
  114. }
  115. }
package dateStructer.tree.binaryTree;public class ThreeLinkedBinaryTree<T> {        // 树的默认深度        private static final int DefTreeDeep = 4;        // 节点数组        private ThreeLinkNode<T>[] datas;        // 指定的树的深度        private int treeDeep;        // 实际的数组个数        private int arraySize;        // 节点个数        private int nodeSize;                @SuppressWarnings("hiding")        class ThreeLinkNode<T> {                public int parentIndex;                public int leftChildIndex;                public int rightChildIndex;                public int index;                public T data;        }        @SuppressWarnings("unchecked")        public ThreeLinkedBinaryTree() {                treeDeep = DefTreeDeep;                arraySize = (int) Math.pow(2, treeDeep) - 1;                datas = new ThreeLinkNode[arraySize];        }        @SuppressWarnings("unchecked")        public ThreeLinkedBinaryTree(int deep, T data) {                treeDeep = DefTreeDeep;                arraySize = (int) Math.pow(2, treeDeep) - 1;                datas = new ThreeLinkNode[arraySize];                ThreeLinkNode<T> threeLinkNode = new ThreeLinkNode<T>();                threeLinkNode.data = data;                threeLinkNode.leftChildIndex = 1;                threeLinkNode.rightChildIndex = 2;                threeLinkNode.index = 0;                threeLinkNode.parentIndex = -1;                datas[0] = threeLinkNode;                nodeSize = 1;        }                public boolean addNode(int index, T data, boolean isLeft) {                if (index + 1 > arraySize || datas[index] == null) {                        throw new RuntimeException("标记无效");                }                for (int i = index + 1; i < arraySize; i++) {                        if (datas[i] == null) {                                ThreeLinkNode<T> threeLinkNode = new ThreeLinkNode<T>();                                threeLinkNode.data = data;                                threeLinkNode.index = i;                                threeLinkNode.parentIndex = index;                                datas[i] = threeLinkNode;                                if (isLeft) {                                        datas[index].leftChildIndex = i;                                } else {                                        datas[index].rightChildIndex = i;                                }                                nodeSize++;                                return true;                        }                }                return false;        }                public boolean isEmpty() {                return nodeSize == 0;        }                @SuppressWarnings("unchecked")        public T getRoot() {                return (T) datas[0];        }                @SuppressWarnings("unchecked")        public T getParent(int index) {                if (index > arraySize || datas[index] == null) {                        throw new RuntimeException("标记无效");                }                return (T) datas[datas[index].parentIndex];        }                public T getLeftNode(int index) {                if (index + 2 > arraySize || datas[index] == null                                || datas[datas[index].leftChildIndex] == null) {                        throw new RuntimeException("标记无效");                }                return (T) datas[datas[index].leftChildIndex].data;        }                public T getRightNode(int index) {                if (index + 2 > arraySize || datas[index] == null                                || datas[datas[index].rightChildIndex] == null) {                        throw new RuntimeException("标记无效");                }                return (T) datas[datas[index].rightChildIndex].data;        }                public int getTreeDeep() {                return treeDeep;        }                public int getNodeIndex(T data) {                for (int i = 0; i < arraySize; i++) {                        if (data == datas[i]) {                                return i;                        }                }                return -1;        }        @Override        public String toString() {                StringBuffer str = new StringBuffer("[");                for (int i = 0; i < nodeSize; i++) {                        if (datas[i].data != null) {                                str.append("[" + datas[i].data + "],");                        }                }                if (datas.length > 0) {                        return str.substring(0, str.lastIndexOf(",")) + "]";                }                return str.append("]").toString();        }}

可以看到基本上方法实现都一样,就是找寻父节点的时候更省事了。因为节点对象多存了父节点的信息,当然就省事了。

7.二叉树的遍历

遍历二叉树实际上就是将一个非线性的二维结构给排列呈线性的过程。如果是顺序实现了二叉树的结构,自然底层就是线性的,无需转化。如果是纯链表实现呢,就需要将离散的节点重新组织组织了。

遍历也分为深度优先遍历、广度优先遍历。而对于深度优先遍历又分为三种模式:先根遍历、中根遍历、后根遍历。

深度优先遍历:就是优先访问树中最深层次的节点

广度优先遍历:就是从根往下一层一层遍历访问

先根遍历:先遍历根节点,之后处理其他子节点

中根遍历:先遍历根节点的左子树,之后遍历根节点,最后遍历右子树

后根遍历:先遍历根节点的左子树,之后遍历右子树,最后遍历根节点

先根遍历算法如下

Java代码
  1. publicList<TwoLinkNode<T>>firstRoot(TwoLinkNode<T> twoLinkNode){
  2. if (twoLinkNode ==null) {
  3. return null;
  4. }
  5. List<TwoLinkNode<T>>list = newArrayList<TwoLinkNode<T>>();
  6. list.add(twoLinkNode);
  7. if(twoLinkNode.leftChildIndex >0){
  8. list.addAll(firstRoot(datas[twoLinkNode.leftChildIndex]));
  9. }
  10. if(twoLinkNode.rightChildIndex >0){
  11. list.addAll(firstRoot(datas[twoLinkNode.rightChildIndex]));
  12. }
  13. return list;
  14. }
public List<TwoLinkNode<T>> firstRoot(TwoLinkNode<T> twoLinkNode) {        if (twoLinkNode == null) {                return null;        }        List<TwoLinkNode<T>> list = new ArrayList<TwoLinkNode<T>>();        list.add(twoLinkNode);        if (twoLinkNode.leftChildIndex > 0) {                list.addAll(firstRoot(datas[twoLinkNode.leftChildIndex]));        }        if (twoLinkNode.rightChildIndex > 0) {                list.addAll(firstRoot(datas[twoLinkNode.rightChildIndex]));        }        return list;}

中根遍历算法如下

Java代码
  1. publicList<TwoLinkNode<T>>minRoot(TwoLinkNode<T> twoLinkNode){
  2. if (twoLinkNode ==null) {
  3. return null;
  4. }
  5. List<TwoLinkNode<T>>list = newArrayList<TwoLinkNode<T>>();
  6. if(twoLinkNode.leftChildIndex >0){
  7. list.addAll(minRoot(datas[twoLinkNode.leftChildIndex]));
  8. }
  9. list.add(twoLinkNode);
  10. if(twoLinkNode.rightChildIndex >0){
  11. list.addAll(minRoot(datas[twoLinkNode.rightChildIndex]));
  12. }
  13. return list;
  14. }
public List<TwoLinkNode<T>> minRoot(TwoLinkNode<T> twoLinkNode) {        if (twoLinkNode == null) {                return null;        }        List<TwoLinkNode<T>> list = new ArrayList<TwoLinkNode<T>>();        if (twoLinkNode.leftChildIndex > 0) {                list.addAll(minRoot(datas[twoLinkNode.leftChildIndex]));        }        list.add(twoLinkNode);        if (twoLinkNode.rightChildIndex > 0) {                list.addAll(minRoot(datas[twoLinkNode.rightChildIndex]));        }        return list;}

后根遍历

Java代码
  1. publicList<TwoLinkNode<T>>afterRoot(TwoLinkNode<T> twoLinkNode){
  2. if (twoLinkNode ==null) {
  3. return null;
  4. }
  5. List<TwoLinkNode<T>>list = newArrayList<TwoLinkNode<T>>();
  6. if(twoLinkNode.leftChildIndex >0){
  7. list.addAll(afterRoot(datas[twoLinkNode.leftChildIndex]));
  8. }
  9. if(twoLinkNode.rightChildIndex >0){
  10. list.addAll(afterRoot(datas[twoLinkNode.rightChildIndex]));
  11. }
  12. list.add(twoLinkNode);
  13. return list;
  14. }
public List<TwoLinkNode<T>> afterRoot(TwoLinkNode<T> twoLinkNode) {        if (twoLinkNode == null) {                return null;        }        List<TwoLinkNode<T>> list = new ArrayList<TwoLinkNode<T>>();        if (twoLinkNode.leftChildIndex > 0) {                list.addAll(afterRoot(datas[twoLinkNode.leftChildIndex]));        }        if (twoLinkNode.rightChildIndex > 0) {                list.addAll(afterRoot(datas[twoLinkNode.rightChildIndex]));        }                list.add(twoLinkNode);        return list;}

广度优先遍历

Java代码
  1. publicList<TwoLinkNode<T>>deepFirst() {
  2. List<TwoLinkNode<T>>list = newArrayList<TwoLinkNode<T>>();
  3. Queue<TwoLinkNode<T>>queue = newArrayDeque<TwoLinkNode<T>>();
  4. queue.add(datas[0]);
  5. while (!queue.isEmpty()){
  6. list.add(queue.peek());
  7. TwoLinkNode<T> twoLinkNode= queue.poll();
  8. if(twoLinkNode.leftChildIndex >0){
  9. queue.add(datas[twoLinkNode.leftChildIndex]);
  10. }
  11. if(twoLinkNode.rightChildIndex >0){
  12. queue.add(datas[twoLinkNode.rightChildIndex]);
  13. }
  14. }
  15. return list;
  16. }
public List<TwoLinkNode<T>> deepFirst() {        List<TwoLinkNode<T>> list = new ArrayList<TwoLinkNode<T>>();        Queue<TwoLinkNode<T>> queue = new ArrayDeque<TwoLinkNode<T>>();        queue.add(datas[0]);        while (!queue.isEmpty()) {                list.add(queue.peek());                TwoLinkNode<T> twoLinkNode = queue.poll();                if (twoLinkNode.leftChildIndex > 0) {                        queue.add(datas[twoLinkNode.leftChildIndex]);                }                if (twoLinkNode.rightChildIndex > 0) {                        queue.add(datas[twoLinkNode.rightChildIndex]);                }        }        return list;}

8.二叉树、树、森林转换

其实这三个结构可以互相转换,具体参考

http://jpkc.nwu.edu.cn/sjjg/study_online/book/6/4_2.htm

9. 总结

这次总结学习了二叉树的概念、用法、实现细节、二叉树的遍历法。当然了这篇总结还不是很全面。回头再补上吧。

from:琉璃仙境

  

爱华网本文地址 » http://www.aihuau.com/a/25101015/265110.html

更多阅读

老妻少夫:李玉成和马玉琴的幸福生活之二辽视访谈

辽宁电视台对李玉成和马玉琴的访谈,录制地点就在他们家的小屋里...3日中午,李玉成打来电话,说辽宁电视台某专栏摄制组记者来采访他和他的妻子马玉琴.邀请我过去,顺便看一看他俩的新家.晚上6点后,我处理完私事后,按照李玉成说的那

关于硕士研究生学制、学习年限和学位论文答辩的暂行

关于硕士研究生学制、学习年限和学位论文答辩的暂行规定(2008年4月2日合肥工业大学校长办公会修订通过)为保证研究生培养质量,提高研究生学术水平和能力,根据我校学科特点和研究生培养的实际情况,现就硕士研究生学制、学习年限和学位论

腌制韭菜花和韭菜花酱的做法 韭菜花怎么腌制

腌制韭菜花和韭菜花酱的做法 韭菜花是秋天里韭白上生出的白色花簇,多在欲开未开时采摘,韭菜花是中国南北城乡普遍食用的一种佐料。腌制韭菜花或磨碎后腌制成酱食用,一般人群均可食用。适宜夜盲症、皮肤粗糙、便秘、干眼病患者多食。 怎

电影《喊山》和《雨中的树》2013.5.9. 喊山电影为什么被禁

电影《喊山》和《雨中的树》(2013.5.9.)**近段时间,无意发现几部好电影,都是在央视电影频道看到的。现在的世界很精彩,书实在太多了,看不完!电影也实在太多了,看不完!偶尔看到几部名不见经传的电影,竟感觉超过那些“大喊大叫”、“呼风唤雨”

声明:《二叉树和二叉树的遍历_Suger 二叉树的层次遍历》为网友余生一个浪分享!如侵犯到您的合法权益请联系我们删除