二叉树介绍

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

image.png

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

image.png

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

image.png

  1. 满二叉树一定是完全二叉树,反之不成立

    遍历节点

  2. 前序遍历:先输出父节点,再遍历左子树和右子树

    1. 先输出当前节点(初始的时候是root节点)
    2. 如果当前节点的左子节点不为空,则递归继续前序遍历
    3. 如果当前节点的右子节点不为空,则递归继续前序遍历
  3. 中序遍历:先遍历左子树,再输出父节点,再遍历右子树

    1. 如果当前节点的左子节点不为空,则递归继续中序遍历
    2. 输出当前节点
    3. 如果当前节点的右子节点不为空,则递归继续中序遍历
  4. 后序遍历:先遍历左子树,在遍历右子树,最后输出父节点

    1. 如果当前节点的左子节点不为空,则递归继续后序遍历
    2. 如果当前节点的右子节点不为空,则递归继续后序遍历
    3. 输出当前节点

总结:看输出父节点的顺序,就确定是前中后序

代码

就跟链表一个路子,单链表每个节点指向一个next,二叉树每个节点指向两个节点left和right

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApp1
  5. {
  6. public class BinaryTree
  7. {
  8. public BTNode root;
  9. //前序遍历
  10. public void preOrder()
  11. {
  12. if (this.root != null)
  13. this.root.preOrder();
  14. else
  15. Console.WriteLine("当前二叉树为空");
  16. }
  17. //中序遍历
  18. public void infixOrder()
  19. {
  20. if (this.root != null)
  21. this.root.infixOrder();
  22. else
  23. Console.WriteLine("当前二叉树为空");
  24. }
  25. //后序遍历
  26. public void postOrder()
  27. {
  28. if (this.root != null)
  29. this.root.postOrder();
  30. else
  31. Console.WriteLine("当前二叉树为空");
  32. }
  33. }
  34. //二叉树节点
  35. public class BTNode
  36. {
  37. public int id;
  38. public string name;
  39. public BTNode left;
  40. public BTNode right;
  41. public BTNode(int id,string name)
  42. {
  43. this.id = id;
  44. this.name = name;
  45. }
  46. //前序遍历
  47. public void preOrder()
  48. {
  49. //1.先输出当前节点
  50. Console.WriteLine(this.id+this.name);
  51. //2.如果当前节点的左子节点不为空,则递归继续前序遍历
  52. if (this.left != null)
  53. this.left.preOrder();
  54. //3.如果当前节点的右子节点不为空,则递归继续前序遍历
  55. if (this.right != null)
  56. this.right.preOrder();
  57. }
  58. //中序遍历
  59. public void infixOrder()
  60. {
  61. //1.如果当前节点的左子节点不为空,则递归继续中序遍历
  62. if (this.left != null)
  63. this.left.infixOrder();
  64. //2.输出当前节点
  65. Console.WriteLine(this.id+this.name);
  66. //3.如果当前节点的右子节点不为空,则递归继续中序遍历
  67. if (this.right != null)
  68. this.right.infixOrder();
  69. }
  70. //后续遍历
  71. public void postOrder()
  72. {
  73. //1.如果当前节点的左子节点不为空,则递归继续后序遍历
  74. if (this.left != null)
  75. this.left.postOrder();
  76. //2.如果当前节点的右子节点不为空,则递归继续后序遍历
  77. if (this.right != null)
  78. this.right.postOrder();
  79. //3.输出当前节点
  80. Console.WriteLine(this.id+this.name);
  81. }
  82. }
  83. }

查找节点

代码

思路就跟前中后序遍历大致相同,不过找到就返回,要注意,比如我左边节点返回了,就要在它执行下一段代码的时候判断有没有找到数据,如果找到了,不用执行另外分支的递归了,直接返回。如果没有这个判断的话,哪怕前面分支已经找到了结果,依旧去找后面的分支。两递归函数也不要用else if判断,哪怕一条分支返回为null,只能说明这条分支没找到,还有其他的分支呢,用else if其他的分支直接不执行了

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApp1
  5. {
  6. public class BinaryTree
  7. {
  8. public BTNode root;
  9. public void preOrder(){};//前序遍历
  10. public void infixOrder(){}//中序遍历
  11. public void postOrder(){};//后序遍历
  12. //前序查找
  13. public BTNode preFind(int id)
  14. {
  15. if (root != null)
  16. return this.root.preFind(id);
  17. else
  18. throw new Exception("二叉树为空");
  19. }
  20. //中序查找
  21. public BTNode infixFind(int id)
  22. {
  23. if (root != null)
  24. return this.root.infixFind(id);
  25. else
  26. throw new Exception("二叉树为空");
  27. }
  28. //后序查找
  29. public BTNode postFind(int id)
  30. {
  31. if (root != null)
  32. return this.root.postFind(id);
  33. else
  34. throw new Exception("二叉树为空");
  35. }
  36. }
  37. public class BTNode
  38. {
  39. public int id;
  40. public string name;
  41. public BTNode left;
  42. public BTNode right;
  43. public BTNode(int id,string name)
  44. {
  45. this.id = id;
  46. this.name = name;
  47. }
  48. public void preOrder(){};//前序遍历
  49. public void infixOrder(){};//中序遍历
  50. public void postOrder(){};//后续遍历
  51. //前序查找
  52. public BTNode preFind(int id)
  53. {
  54. //1.定义一个辅助节点,这个节点用来装递归的结果,默认为null,如果都没有找到就返回默认的
  55. BTNode temp = null;
  56. //2.先比较当前节点,如果找到了就返回
  57. if (this.id == id)
  58. return this;
  59. //3.没找到就前序查找左节点
  60. if (this.left != null)
  61. temp = this.left.preFind(id);
  62. //3.1.只要找到了这个节点,temp不为空了就立马返回
  63. if (temp != null)
  64. return temp;
  65. //4.右边节点也前序查找
  66. if (this.right != null)
  67. temp = this.right.preFind(id);
  68. if (temp != null)
  69. return temp;
  70. //5.都没找到说明真没有这个要查找的值,返回temp,默认值为null
  71. return temp;
  72. }
  73. //中序查找
  74. public BTNode infixFind(int id)
  75. {
  76. //中序遍历一样的思路,前序查找一样的写法
  77. BTNode temp = null;
  78. if (this.left != null)
  79. temp = this.left.infixFind(id);
  80. if (temp != null)
  81. return temp;
  82. if (this.id == id)
  83. return this;
  84. if (this.right != null)
  85. temp = this.right.infixFind(id);
  86. if (temp != null)
  87. return temp;
  88. return temp;
  89. }
  90. //后序查找
  91. public BTNode postFind(int id)
  92. {
  93. //如上
  94. BTNode temp = null;
  95. if (this.left != null)
  96. temp = this.left.postFind(id);
  97. if (temp != null)
  98. return temp;
  99. if (this.right != null)
  100. temp = this.right.postFind(id);
  101. if (temp != null)
  102. return temp;
  103. if (this.id == id)
  104. return this;
  105. if (this.id == id)
  106. return this;
  107. return temp;
  108. }
  109. }
  110. }

删除节点

思路

  1. 因为这个二叉树是单向的,所以我们要判断当前节点的子节点是否需要被删除,而不是判断当前节点,和单链表删除一样
  2. 当前节点的子节点又分为左右两个子节点,需要都判断左子节点和右子节点是不是这个要被删除的节点,如果是,就把当前节点对应的子节点赋值为null,然后返回(结束递归)
  3. 如果第2步没有删除节点,就继续向左子数递归删除
  4. 如果第2步也没有删除节点,则应当向右子树进行递归删除

    代码

    ```csharp using System; using System.Collections.Generic; using System.Text;

namespace ConsoleApp1 { public class BinaryTree { public BTNode root;

  1. public void preOrder(){};//前序遍历
  2. public void infixOrder(){};//中序遍历
  3. public void postOrder(){};//后序遍历
  4. public BTNode preFind(int id){};//前序查找
  5. public BTNode infixFind(int id){};//中序查找
  6. public BTNode postFind(int id){};//后序查找
  7. //删除节点
  8. public void delNode(int id)
  9. {
  10. if (root != null)
  11. {
  12. if (root.id == id)
  13. {
  14. root = null;
  15. return;
  16. }
  17. else
  18. {
  19. root.delNode(id);
  20. }
  21. }
  22. else
  23. {
  24. Console.WriteLine("空树");
  25. }
  26. }
  27. }
  28. public class BTNode
  29. {
  30. public int id;
  31. public string name;
  32. public BTNode left;
  33. public BTNode right;
  34. public BTNode(int id, string name)
  35. {
  36. this.id = id;
  37. this.name = name;
  38. }
  39. public void preOrder(){};//前序遍历
  40. public void infixOrder(){};//中序遍历
  41. public void postOrder(){};//后序遍历
  42. public BTNode preFind(int id){};//前序查找
  43. public BTNode infixFind(int id){};//中序查找
  44. public BTNode postFind(int id){};//后序查找
  45. //删除节点
  46. public void delNode(int id)
  47. {
  48. if (this.left != null && this.left.id == id)
  49. {
  50. this.left = null;
  51. return;
  52. }
  53. if (this.right != null && this.right.id == id)
  54. {
  55. this.right = null;
  56. return;
  57. }
  58. //当前节点的左右节点都不是要删除的节点,就要向左右树递归删除
  59. if (this.left != null)
  60. {
  61. this.left.delNode(id);
  62. }
  63. if (this.right != null)
  64. {
  65. this.right.delNode(id);
  66. }
  67. }
  68. }

}

  1. <a name="qaLkk"></a>
  2. # 顺序存储二叉树
  3. 从数据存储来看,数组存储方式和树的存储方式可以相互转换,即数组可以转换成树,树也可以转换成数组。<br />可以用二叉树前中后序遍历的方式遍历数组。
  4. 顺序存储二叉树有如下特点
  5. 1. 顺序存储二叉树只考虑完全二叉树
  6. 1. 第n个元素的左子节点为`2*n+1`
  7. 1. 第n个元素的右子节点为`2*n+2`
  8. 1. 第n个元素的父节点为`(n-1)/2`
  9. 1. n表示二叉树中第几个元素,从0开始(为了跟数组保持一致)
  10. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/21464164/1644979413555-3071da35-bff8-4663-ada0-63392d6f4a07.png#clientId=u3daa3f58-8541-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=335&id=uac5e9af3&margin=%5Bobject%20Object%5D&name=image.png&originHeight=553&originWidth=760&originalType=binary&ratio=1&rotation=0&showTitle=true&size=37756&status=done&style=none&taskId=u3c41491c-9a06-4fca-9323-59e1e93bc69&title=%E9%A1%BA%E5%BA%8F%E5%AD%98%E5%82%A8%E4%BA%8C%E5%8F%89%E6%A0%91&width=460.60603398374883 "顺序存储二叉树")<br />上图对应的数组就是`[1,2,3,4,5,6,7]`
  11. <a name="bewHh"></a>
  12. ## 代码
  13. ```csharp
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Text;
  17. namespace ConsoleApp1
  18. {
  19. public class ArrayBinaryTree
  20. {
  21. int[] array = null;
  22. public ArrayBinaryTree(int[] array)
  23. {
  24. this.array = array;
  25. }
  26. //前序
  27. public void preOrder(int index)
  28. {
  29. if (array == null || array.Length == 0)
  30. {
  31. Console.WriteLine("数组为空");
  32. return;
  33. }
  34. //1.输出当前元素
  35. Console.WriteLine(array[index]);
  36. //2.向左递归遍历
  37. if (2 * index + 1 < array.Length)
  38. preOrder(2 * index + 1);
  39. //3.向右递归遍历
  40. if (2 * index + 2 < array.Length)
  41. preOrder(2 * index + 2);
  42. }
  43. //中序
  44. public void infixOrder(int index)
  45. {
  46. if (array == null || array.Length == 0)
  47. {
  48. Console.WriteLine("数组为空");
  49. return;
  50. }
  51. if (2 * index + 1 < array.Length)
  52. infixOrder(2 * index + 1);
  53. Console.WriteLine(array[index]);
  54. if (2 * index + 2 < array.Length)
  55. infixOrder(2 * index + 2);
  56. }
  57. //后序
  58. public void postOrder(int index)
  59. {
  60. if (array == null || array.Length == 0)
  61. {
  62. Console.WriteLine("数组为空");
  63. return;
  64. }
  65. if (2 * index + 1 < array.Length)
  66. postOrder(2 * index + 1);
  67. if (2 * index + 2 < array.Length)
  68. postOrder(2 * index + 2);
  69. Console.WriteLine(array[index]);
  70. }
  71. }
  72. }

线索二叉树

线索化二叉树思路

n个节点的二叉树中含有n+1个(公式2n-(n-1)=n+1)个空指针域。利用二叉树中这些空指针域,存放指向该节点在某种遍历次序下的前驱和后继节点指针(这种附加的指针称为 “线索”)。
image.png
这种加上了线索的二叉树称为线索二叉树。根据线索性质的不同,线索二叉树可以分为前序线索二叉树中序线索二叉树,和后序线索二叉树三种。

一个节点的前一个节点,称为前驱节点。
一个节点的有一个结点,称为后继节点。

如上图的二叉树,3,4,5,6节点的指针域并没有使用完。3的右子节点为空,4,5,6的左右节点都为空,所以空的指针节点有7个。
根据上图公式2n-(n-1)=n+1得到 2*6-(6-1)=6+1=7
image.png

根据中序遍历,结果为4,2,5,1,6,3 输出4后,下一个节点应该是输出2,这时候就用4的右指针指向2(2是4的后继节点)。这时候4的左指针还是空的,4是第一个输出,因此没有前驱节点。 5的后继节点是1,就用5的右指针指向1,而5的左指针还是空的,就指向前驱节点2。 6的后继节点是3,右指针指向3,左指针是空的,就指向6的前驱节点1。 3没有后继节点,左指针又不为空,就不动。 以此类推。这就是附加线索

这里举例是前序,中后序同理。

说明:当线索化二叉树后节点的left和right有如下情况。

  1. left指向的是左子树,也有可能指向前驱节点,如上图,1节点的left就指向左子树,而4节点的left指向的就是前驱节点
  2. right指向的是右子数,也有可能指向后继节点,如上图,1节点的right就指向右子树,而4节点的right指向的就是后继节点

因此需要给每个节点的left和right设置一个属性,来判断他是子树还是线索节点

线索化代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApp1
  5. {
  6. public class ThreadedBinaryTree
  7. {
  8. public TBTNode root;
  9. //为了实现线索化,需要创建要给指向当前节点的前驱节点的指针
  10. //在递归进行线索化时,pre总是保留前一个节点
  11. public TBTNode pre;
  12. //前序线索化二叉树
  13. public void preThreadedNodes(TBTNode node)
  14. {
  15. //思路如中序线索化
  16. if (node == null)
  17. return;
  18. if (node.left == null)
  19. {
  20. node.left = pre;
  21. node.lefttype = 1;
  22. }
  23. if (pre != null && pre.right == null)
  24. {
  25. pre.right = node;
  26. pre.righttype = 1;
  27. }
  28. pre = node;
  29. if(node.lefttype==0)
  30. preThreadedNodes(node.left);
  31. if(node.righttype==0)
  32. preThreadedNodes(node.right);
  33. }
  34. //中序线索化二叉树,node就是需要线索化的节点
  35. public void infixThreadedNodes(TBTNode node)
  36. {
  37. if (node == null)//不能线索化
  38. return;
  39. //(一).先线索化左子树
  40. infixThreadedNodes(node.left);
  41. //(二).线索化当前节点
  42. //先处理当前节点的前驱节点
  43. if (node.left == null)
  44. {
  45. node.left = pre;//让当前节点的左指针指向前驱节点
  46. node.lefttype = 1;//修改当前结点的左指针类型,指向前驱节点
  47. }
  48. //处理后继节点,其实是在下次递归的时候处理当前节点的后继节点
  49. //也就是说当前这次是处理上一节点的后继节点与当前节点的前驱节点
  50. if(pre!= null && pre.right==null)
  51. {
  52. pre.right = node;//让前驱节点的右指针指向当前节点
  53. pre.righttype = 1;//修改前驱节点的右指针类型
  54. //这样就处理好了 上一个节点 的后继节点
  55. }
  56. pre = node;//前一个节点进行迭代
  57. //(三).线索化右子树
  58. infixThreadedNodes(node.right);
  59. }
  60. //后序线索化二叉树
  61. public void postThreadedNodes(TBTNode node)
  62. {
  63. if (node == null)
  64. return;
  65. postThreadedNodes(node.left);
  66. postThreadedNodes(node.right);
  67. if (node.left == null)
  68. {
  69. node.left = pre;
  70. node.lefttype = 1;
  71. }
  72. if (pre != null && pre.right == null)
  73. {
  74. pre.right = node;
  75. pre.righttype = 1;
  76. }
  77. pre = node;
  78. }
  79. }
  80. public class TBTNode
  81. {
  82. public int id;
  83. public string name;
  84. public TBTNode left;
  85. public TBTNode right;
  86. //说明
  87. //1.如果lefttype==0,表示指向的是左子树,如果1表示指向前驱节点
  88. //2.如果righttype==0,表示指向是右子树,如果1表示指向后继节点
  89. public int lefttype;
  90. public int righttype;
  91. public TBTNode(int id, string name)
  92. {
  93. this.id = id;
  94. this.name = name;
  95. }
  96. }
  97. }

遍历线索化二叉树

因为线索化了之后,各个节点指向有变化,因此原来的遍历方式不能使用,这时需要使用新的方式遍历线索化二叉树,各个节点可以通过线型方式遍历,因此无需使用递归方式,这样提高了遍历的效率。遍历的次序和使用的线性化的次序一致。

遍历线索化二叉树代码

需要注意的是,后序遍历线索二叉树,需要每个节点添加一个父节点,在创建二叉树时每个节点就指向了父节点

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApp1
  5. {
  6. public class ThreadedBinaryTree
  7. {
  8. public TBTNode root;
  9. //为了实现线索化,需要创建要给指向当前节点的前驱节点的指针
  10. //在递归进行线索化时,pre总是保留前一个节点
  11. public TBTNode pre;
  12. public void preThreadedNodes(TBTNode node){};//前序线索化二叉树
  13. public void infixThreadedNodes(TBTNode node){};//中序线索化二叉树
  14. public void postThreadedNodes(TBTNode node){};//后序线索化二叉树
  15. //前序遍历线索二叉树
  16. public void preThreadedList()
  17. {
  18. TBTNode node = root;
  19. while (node != null)
  20. {
  21. //前序遍历,先输出当前节点
  22. //如果lefttype==0,说明left是左子树
  23. while (node.lefttype == 0)
  24. {
  25. Console.WriteLine(node.id + node.name);
  26. node = node.left;
  27. }
  28. Console.WriteLine(node.id + node.name);
  29. node = node.right;
  30. }
  31. }
  32. //中序遍历线索二叉树
  33. public void infixThreadedList()
  34. {
  35. //定义一个变量,存储当前遍历的结点,从root开始
  36. TBTNode node = root;
  37. while (node != null)
  38. {
  39. //循环的找到lefttype==1的节点
  40. //因为是中序遍历,左边的节点先输出
  41. //所以第一个输出的节点本身指针为null,线索化后,lefttype==1,该节点是按照线索化处理的有效节点
  42. while (node.lefttype==0)
  43. {
  44. node = node.left;
  45. }
  46. Console.WriteLine(node.id+node.name);
  47. //如果当前节点的右指针指向的是后继节点就一直输出
  48. while (node.righttype == 1)
  49. {
  50. //改变当前节点为下一个节点
  51. node = node.right;
  52. Console.WriteLine(node.id+node.name);
  53. }
  54. //退出了上面的循环,就说明遇到了下一个节点不是后继节点的节点,直接替换它
  55. node = node.right;
  56. }
  57. }
  58. //后序遍历线索二叉树
  59. public void postThreadedList()
  60. {
  61. TBTNode node = root;
  62. TBTNode pre = null;
  63. //后序的第一个值是左边的叶子节点,所以当lefttype==1时,说明这个节点就是第一个输出的点,用循环先定位到这个点
  64. while (node != null&&node.lefttype == 0)
  65. {
  66. node = node.left;
  67. }
  68. //然后开始遍历节点
  69. while (node != null)
  70. {
  71. //如果当前节点有后继节点
  72. if (node.righttype == 1)
  73. {
  74. Console.WriteLine(node.id + node.name);
  75. pre = node;
  76. node = node.right;
  77. }
  78. else//没有后继节点,想象三角形顶上的那个点,左右节点都是子节点
  79. {
  80. //如果上一个处理的节点是当前节点的右节点(当前节点来到了三角形的顶端节点,想想后序遍历的顺序)
  81. if (pre == node.right)
  82. {
  83. Console.WriteLine(node.id+node.name);
  84. if (node == root)//根节点是后序遍历的最后一个点
  85. return;
  86. pre = node;
  87. node = node.parent;//往父节点迭代
  88. }
  89. else//已经退回了root节点,上一个处理的节点不是当前节点(root)的右节点
  90. {
  91. //开始往根节点的右边遍历
  92. node = node.right;
  93. //同样的找到最左边的点开始
  94. while (node != null && node.lefttype == 0)
  95. {
  96. node = node.left;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. public class TBTNode
  104. {
  105. public int id;
  106. public string name;
  107. public TBTNode left;
  108. public TBTNode right;
  109. public TBTNode parent;//当前节点的父节点,用于后序遍历线索二叉树
  110. //说明
  111. //1.如果lefttype==0,表示指向的是左子树,如果1表示指向前驱节点
  112. //2.如果righttype==0,表示指向是右子树,如果1表示指向后继节点
  113. public int lefttype;
  114. public int righttype;
  115. public TBTNode(int id, string name)
  116. {
  117. this.id = id;
  118. this.name = name;
  119. }
  120. }
  121. }

前中后序遍历线索二叉树比较

  1. 前序线索化二叉树遍历相对最容易理解,实现起来也比较简单。由于前序遍历的顺序是:根左右,所以从根节点开始,沿着左子树进行处理,当子节点的left指针类型是线索时,说明到了最左子节点,然后处理子节点的right指针指向的节点,可能是右子树,也可能是后继节点,无论是哪种类型继续按照上面的方式(先沿着左子树处理,找到子树的最左子节点,然后处理right指针指向),以此类推,直到节点的right指针为空,说明是最后一个,遍历完成。

  2. 中序线索化二叉树的网上相关介绍最多。中序遍历的顺序是:左根右,因此第一个节点一定是最左子节点,先找到最左子节点,依次沿着right指针指向进行处理(无论是指向子节点还是指向后继节点),直到节点的right指针为空,说明是最后一个,遍历完成。

  3. 后序遍历线索化二叉树最为复杂,通用的二叉树数节点存储结构不能够满足后序线索化,因此我们扩展了节点的数据结构,增加了父节点的指针。后序的遍历顺序是:左右根,先找到最左子节点,沿着right后继指针处理,当right不是后继指针时,并且上一个处理节点是当前节点的右节点,则处理当前节点的右子树,遍历终止条件是:当前节点是root节点,并且上一个处理的节点是root的right节点。