写在前面

前面讲了树的基本概念,这篇文章主要讲常见的树的基本操作,如查找,新增,删除等。其中通过动图的方式使得更加容易理解。

二叉查找树

二叉查找树(BST,Binary Sort Tree),也称二叉排序树,或二叉搜索树。一棵二叉查找树满足以下条件:

  • 左子树的所有值均小于根节点的值
  • 右子树的所有值均大于根节点的值
  • 左右子树同时也满足以上两点

通俗来说就是一棵二叉树,节点左小右大

06.二叉查找树&AVL树 - 图1

插入操作

一棵二叉查找树的插入操作,如果插入的值 x 从根节点开始:

  1. x值小于该节点值,在左子树中继续
  2. x值大于该节点值,在右子树中继续
  3. 如果节点是叶节点,X值小于该节点值则插入左子节点,否则插入右节点

在上面的二叉排序树中,如果我们插入节点的值为 80,具体操作如下:

06.二叉查找树&AVL树 - 图2

查找操作

一棵二叉查找树的查找操作,如果查找的值 x 从根节点开始:

  1. 如果x小于根节点,则在左子树中继续查找
  2. 如果x大于根节点,则在右子树中继续查找
  3. 如果x的值等于根节点,则返回该节点
  4. 如果都查找不到,则返回null

在上面的二叉排序树中,如果我们需要查找节点的值为10的节点,具体操作如下:

06.二叉查找树&AVL树 - 图3

遍历树

树的遍历有三种方法。前序(preorder),中序(inorder),后序(postorder)。

前序遍历

06.二叉查找树&AVL树 - 图4

中序遍历

06.二叉查找树&AVL树 - 图5

后序遍历

06.二叉查找树&AVL树 - 图6

最大值和最小值

最小值:找到根节点的左子节点,一直向左查找直到没有左子节点的节点即为最小节点

最大值:找到根节点的右子节点,一直向右查找直到没有右子节点的节点即为最小节点

删除操作

该节点是叶子节点

结点为叶子结点时,直接删除,把父节点指向子节点的引用设为null。

一棵二叉查找树的删除操作,如果删除的值 x 从根节点开始:

  1. 如果节点的值等于 x,则删除
  2. x值小于该节点值,在左子树中继续
  3. x值大于该节点值,在右子树中继续

如果我们删除节点的值为 80,具体操作如下:

06.二叉查找树&AVL树 - 图7

该节点有一个子节点

节点有一个子节点,分两种情况,判断是父节点的左子结点还是右子节点,把父节点的引用指向结点的子节点(子节点也要分左右子节点情况,相当于一共四种情况)

左左左:即删除节点在根节点的边,且删除节点在其父节点的边,且删除节点的子节点为子节点

06.二叉查找树&AVL树 - 图8

左右左:即删除节点在根节点的边,且删除节点在其父节点的边,且删除节点的子节点为子节点

06.二叉查找树&AVL树 - 图9

右右右:即删除节点在根节点的边,且删除节点在其父节点的边,且删除节点的子节点为子节点

06.二叉查找树&AVL树 - 图10

右左右:即删除节点在根节点的边,且删除节点在其父节点的边,且删除节点的子节点为子节点

06.二叉查找树&AVL树 - 图11

该节点有两个子节点

由于二叉搜索树的特性,保证了某个结点的左子树的值都小于该节点,右子树的值都大于该节点,只需找到左子树中的最大值或者右子树中的最小值(也叫作中续后继节点)来替换该结点,即可保证节点删除后任为二叉搜索树。

后继节点

在二叉查找树中,节点是按照左小右大的方式排列的,对任意一个节点来说,比该节点的值次高的节点为它的中续后继,简称为后继节点。由于左侧节点总小于右侧节点及父节点,所以后继节点没有左子节点,可能存在右子节点。通过后继节点来替换该结点,即可保证节点删除后仍为二叉搜索树。

查找方式也分为2种

06.二叉查找树&AVL树 - 图12

若使用左子树中的最大值来替换

06.二叉查找树&AVL树 - 图13

若使用右子树的最小值(后继节点)来替换

06.二叉查找树&AVL树 - 图14

代码实现

  1. public class BSTTree {
  2. /**
  3. * 节点
  4. */
  5. public static class Node {
  6. //数据,为简化代码,本程序默认节点里存储一个int变量,实际程序里可以存储自定义类型变量
  7. int data;
  8. //左子节点
  9. Node leftChild;
  10. //右子节点
  11. Node rightChild;
  12. public Node(int data) {
  13. this.data = data;
  14. }
  15. @Override
  16. public String toString() {
  17. return "Node{" +
  18. "data=" + data +
  19. ", leftChild=" + leftChild +
  20. ", rightChild=" + rightChild +
  21. '}';
  22. }
  23. }
  24. /**
  25. * 新增节点 采用递归的方式
  26. *
  27. * @param root 根节点
  28. * @param data 插入的数据
  29. * @return
  30. */
  31. public static Node insert(Node root, int data) {
  32. if (root == null) {
  33. root = new Node(data);
  34. return root;
  35. }
  36. //若插入的数据小于根节点,插入到其左子树
  37. if (data <= root.data) {
  38. root.leftChild = insert(root.leftChild, data);
  39. } else {
  40. //插入到其右子树
  41. root.rightChild = insert(root.rightChild, data);
  42. }
  43. return root;
  44. }
  45. /**
  46. * 前序遍历
  47. *
  48. * @param root
  49. */
  50. public static void preOrder(Node root) {
  51. if (root != null) {
  52. System.out.println(root.data + "->");
  53. preOrder(root.leftChild);
  54. preOrder(root.rightChild);
  55. }
  56. }
  57. /**
  58. * 中序遍历
  59. *
  60. * @param root
  61. */
  62. public static void inOrder(Node root) {
  63. if (root != null) {
  64. inOrder(root.leftChild);
  65. System.out.print(root.data + "->");
  66. inOrder(root.rightChild);
  67. }
  68. }
  69. /**
  70. * 后序遍历
  71. *
  72. * @param root
  73. */
  74. public static void postOrder(Node root) {
  75. if (root != null) {
  76. postOrder(root.leftChild);
  77. postOrder(root.rightChild);
  78. System.out.print(root.data + "->");
  79. }
  80. }
  81. /**
  82. * 查找数据
  83. *
  84. * @param data
  85. * @return
  86. */
  87. public static Node find(Node root, int data) {
  88. //若查找的数据小于根节点,则向左查找(也可以采用递归的方式查找)
  89. Node current = root;
  90. while (current != null) {
  91. if (data < current.data) {
  92. current = current.leftChild;
  93. } else if (data > current.data) {
  94. //向右查找
  95. current = current.rightChild;
  96. } else {
  97. return current;
  98. }
  99. }
  100. return null;
  101. }
  102. /**
  103. * 最小值
  104. *
  105. * @param root
  106. * @return
  107. */
  108. public static Node minimum(Node root) {
  109. if (root == null) {
  110. return null;
  111. }
  112. while (root.leftChild != null) {
  113. root = root.leftChild;
  114. }
  115. return root;
  116. }
  117. /**
  118. * 最大值
  119. *
  120. * @param root
  121. * @return
  122. */
  123. public static Node maximum(Node root) {
  124. if (root == null) {
  125. return null;
  126. }
  127. while (root.rightChild != null) {
  128. root = root.rightChild;
  129. }
  130. return root;
  131. }
  132. /**
  133. * 删除节点
  134. * 1.该节点是叶子节点,即没有子节点
  135. * 2.该节点有一个子节点
  136. * 3.该节点有两个子节点(通过该节点的中续后继节点来代替需要删除的节点,
  137. * 因为后继节点比删除节点的右节点都小,比删除节点的左节点都大)
  138. * 中续后继节点:比该节点值次高的节点为中续后继节点,如节点2的后继节点为3
  139. * 4
  140. * / \
  141. * 2 6
  142. * / \ / \
  143. * 1 3 5 8
  144. *
  145. * @param root
  146. * @param data 要删除的节点的值
  147. */
  148. public static boolean delete(Node root, int data) {
  149. //用来表示要删除节点的父节点
  150. Node parent = null;
  151. //需要删除的节点是否为父节点的左子节点
  152. boolean ifLeftChild = true;
  153. //需要删除的节点
  154. Node current = root;
  155. //定位删除节点的位置及其父节点
  156. while (true) {
  157. if (data == current.data) {
  158. break;
  159. } else if (data < current.data) {
  160. ifLeftChild = true;
  161. parent = current;
  162. current = current.leftChild;
  163. } else if (data > current.data) {
  164. ifLeftChild = false;
  165. parent = current;
  166. current = current.rightChild;
  167. }
  168. //若找不到直接返回fasle
  169. if (current == null) {
  170. return false;
  171. }
  172. }
  173. //1.该节点是叶子节点
  174. if (current.leftChild == null && current.rightChild == null) {
  175. //若为根节点,删除整棵树
  176. if (current == root) {
  177. root = null; //GC
  178. }
  179. //若为左子节点
  180. if (ifLeftChild) {
  181. parent.leftChild = null;
  182. } else {
  183. parent.rightChild = null;
  184. }
  185. }
  186. //2.该节点有一个子节点
  187. if (current.leftChild != null && current.rightChild == null) {//若删除节点的左子节点不为null
  188. //如果该节点为根节点,将根节点的左子节点变为根节点
  189. if (current == root) {
  190. root = current.leftChild;
  191. }
  192. if (ifLeftChild) {
  193. //左左左:若该节点为父节点的左子节点,则该节点的左子节点变为父节点的左子节点
  194. parent.leftChild = current.leftChild;
  195. } else {
  196. //左右左:若该节点为父节点的左子节点,则该节点的左子节点变为父节点的右子节点
  197. parent.rightChild = current.leftChild;
  198. }
  199. } else if (current.leftChild == null && current.rightChild != null) {
  200. if (current == root) {
  201. root = current.rightChild;
  202. }
  203. if (ifLeftChild) {
  204. //右左右:若该节点为父节点的左子节点,则该节点的右子节点变为父节点的左子节点
  205. parent.leftChild = current.rightChild;
  206. } else {
  207. //右右右:若该节点为父节点的右子节点,则该节点的右子节点变为父节点的右子节点
  208. parent.rightChild = current.rightChild;
  209. }
  210. }
  211. //3.该节点有两个子节点,这里通过后继节点的方式删除
  212. if (current.leftChild != null && current.rightChild != null) {
  213. //获取删除节点且重新构建的后继结点
  214. Node successor = getSuccessor(current);
  215. if (root == current) {
  216. root = successor;
  217. } else if (ifLeftChild) {
  218. parent.leftChild = successor;
  219. } else {
  220. parent.rightChild = successor;
  221. }
  222. }
  223. return true;
  224. }
  225. /**
  226. * @param node 要删除的节点(假设此时该节点存在右子节点)
  227. * @return 删除节点的后继节点
  228. */
  229. public static Node getSuccessor(Node node) {
  230. //node节点的左子节点
  231. Node leftChild = node.leftChild;
  232. //定义后继节点的父节点
  233. Node successorParent = node;
  234. //定义后继节点
  235. Node successor = node;
  236. //定义一个临时变量current,先获取删除节点的右子节点,然后再获取右子节点的最小值
  237. Node current = node.rightChild;
  238. //这一步就是查找删除节点的后继节点
  239. while (current != null) {
  240. //找到后继几点的父节点
  241. successorParent = successor;
  242. successor = current;
  243. //获取右子节点的最小值,直左子树的左子节点为null说明该节点就是删除节点的右子节点的最小值
  244. current = current.leftChild;
  245. }
  246. //找到后继节点之后重新构建后继节点树
  247. if (current != node.rightChild) {
  248. /* 后继节点的父节点的左子节点由原来的后继节点变为原来后继点的右子节点(因为左子节点的值始终小于父节点的值)
  249. * 如下55若为后继节点提出去后,58就变为了60的左子节点
  250. * 60 55
  251. * / \ \
  252. * 55 80 ---重新构建--- 60
  253. * \ / \
  254. * 58 58 80
  255. */
  256. successorParent.leftChild = successor.rightChild;
  257. successor.rightChild = node.rightChild;
  258. successor.leftChild = leftChild;
  259. }
  260. return successor;
  261. }
  262. public static void main(String[] args) {
  263. /*
  264. * 新增操作
  265. * 4
  266. * / \
  267. * 2 6
  268. * / \ / \
  269. * 1 3 5 8
  270. * /
  271. * 7
  272. */
  273. Node root = null;
  274. root = insert(root, 4);
  275. root = insert(root, 2);
  276. root = insert(root, 1);
  277. root = insert(root, 3);
  278. root = insert(root, 6);
  279. root = insert(root, 5);
  280. root = insert(root, 8);
  281. root = insert(root, 7);
  282. // root = insert(root, 50);
  283. // root = insert(root, 25);
  284. // root = insert(root, 15);
  285. // root = insert(root, 35);
  286. // root = insert(root, 5);
  287. // root = insert(root, 20);
  288. // root = insert(root, 30);
  289. // root = insert(root, 40);
  290. //delete(root, 25);
  291. inOrder(root);
  292. // System.out.println("---------");
  293. // //查找操作 4
  294. // Node node = find(root, 4);
  295. // printTree(node);
  296. // System.out.println("---------");
  297. // //删除操作
  298. // Node delete = delete(root, 4);
  299. // printTree(delete);
  300. }
  301. /**
  302. * 打印
  303. *
  304. * @param root
  305. */
  306. public static void printTree(Node root) {
  307. System.out.println("根节点" + root.data);
  308. if (root.leftChild != null) {
  309. System.out.print("左子节点:");
  310. printTree(root.leftChild);
  311. }
  312. if (root.rightChild != null) {
  313. System.out.print("右子节点:");
  314. printTree(root.rightChild);
  315. }
  316. }
  317. }

平衡二叉树(AVL)

平衡二叉树(AVL),是一个二叉排序树,同时任意节点左右两个子树的高度差(或平衡因子,简称BF)的绝对值不超过1,并且左右两个子树也满足。

06.二叉查找树&AVL树 - 图15

为什么使用平衡二叉树

通过二叉查找树的查找操作可以发现,一棵二叉查找树的查找效率取决于树的高度,如果使树的高度最低,那么树的查找效率也会变高。

如下面一个二叉树,全部由右子树构成

06.二叉查找树&AVL树 - 图16

这个时候的二叉树其实就类似于链表,此时的查找时间复杂度为O(n),而AVL树的查找时间复杂度为O(logn)。之前讲过O(logn)耗时是小于O(n)的,如下:

06.二叉查找树&AVL树 - 图17

可参考:常见数据结构的时间复杂度

平衡二叉树的调整

一棵平衡二叉树的插入操作会有2个结果:

如果平衡没有被打破,即任意节点的BF=1,则不需要调整

如果平衡被打破,则需要通过旋转调整,且该节点为失衡节点

一棵失衡的二叉树通过以下调整可以重新达到平衡:

左旋:以某个结点作为支点(旋转结点),其右子结点变为旋转结点的父结点,右子结点的左子结点变为旋转结点的右子结点,左子结点保持不变

06.二叉查找树&AVL树 - 图18

右旋:以某个结点作为支点(旋转结点),其左子结点变为旋转结点的父结点,左子结点的右子结点变为旋转结点的左子结点,右子结点保持不变

06.二叉查找树&AVL树 - 图19

通过旋转最小失衡子树来实现失衡调整:

在一棵平衡二叉树新增节点,在新增的节点向上查找,第一个平衡因子的绝对值超过1(BF>1)的节点为根的子树称为最小不平衡子树。也就是说,一棵失衡的树,有可能多棵子树同时失衡,这时只需要调整最小的不平衡子树即可

看完下面的旋转方式之后再回来看最小失衡子树旋转就很清晰了

06.二叉查找树&AVL树 - 图20

LL旋转

向左子树(L)的左孩子(L)插入新节点

插入节点失衡节点的左子树的左边,经过一次右旋即可达到平衡,如下

  • 插入新节点5,旧根节点40为失衡节点

  • 旧根节点40为新根节点20的右子树

  • 新根节点20的右子树30为旧根节点40的左子树

06.二叉查找树&AVL树 - 图21

旋转过程

06.二叉查找树&AVL树 - 图22

RR旋转

插入节点失衡节点的右子树的右边,经过一次左旋即可达到平衡,如下

  • 插入新节点60,旧根节点20为失衡节点

  • 旧根节点20为新根节点40的左子树

  • 新根节点40的左子树30为旧根节点20的右子树

06.二叉查找树&AVL树 - 图23

旋转过程

06.二叉查找树&AVL树 - 图24

LR旋转

插入节点失衡节点的左子树的右边,先左旋,再右旋,如下

06.二叉查找树&AVL树 - 图25

旋转过程

06.二叉查找树&AVL树 - 图26

RL旋转

插入节点失衡节点的右子树的左边,先右旋,再左旋,如下

06.二叉查找树&AVL树 - 图27

旋转过程

06.二叉查找树&AVL树 - 图28

代码实现

  1. public class AVLTree {
  2. //节点
  3. public static class Node {
  4. int data; //数据
  5. Node leftChild; //左子节点
  6. Node rightChild;//右子节点
  7. int height; // 记录该节点所在的高度
  8. public Node(int data) {
  9. this.data = data;
  10. }
  11. }
  12. //获取节点的高度
  13. public static int getHeight(Node p){
  14. return p == null ? -1 : p.height; // 空树的高度为-1
  15. }
  16. public static void main(String[] args) {
  17. Node root = null;
  18. root = insert(root,40);
  19. root = insert(root,20);
  20. root = insert(root,50);
  21. root = insert(root,10);
  22. root = insert(root,30);
  23. //插入节点在失衡结点的左子树的左边
  24. root = insert(root,5);
  25. //打印树,按照先打印左子树,再打印右子树的方式
  26. printTree(root);
  27. }
  28. public static void printTree(Node root) {
  29. System.out.println(root.data);
  30. if(root.leftChild !=null){
  31. System.out.print("left:");
  32. printTree(root.leftChild);
  33. }
  34. if(root.rightChild !=null){
  35. System.out.print("right:");
  36. printTree(root.rightChild);
  37. }
  38. }
  39. // AVL树的插入方法
  40. public static Node insert(Node root, int data) {
  41. if (root == null) {
  42. root = new Node(data);
  43. return root;
  44. }
  45. if (data <= root.data) { // 插入到其左子树上
  46. root.leftChild = insert(root.leftChild, data);
  47. //平衡调整
  48. if (getHeight(root.leftChild) - getHeight(root.rightChild) > 1) {
  49. if (data <= root.leftChild.data) { // 插入节点在失衡结点的左子树的左边
  50. System.out.println("LL旋转");
  51. root = LLRotate(root); // LL旋转调整
  52. }else{ // 插入节点在失衡结点的左子树的右边
  53. System.out.println("LR旋转");
  54. root = LRRotate(root);
  55. }
  56. }
  57. }else{ // 插入到其右子树上
  58. root.rightChild = insert(root.rightChild, data);
  59. //平衡调整
  60. if(getHeight(root.rightChild) - getHeight(root.leftChild) > 1){
  61. if(data <= root.rightChild.data){//插入节点在失衡结点的右子树的左边
  62. System.out.println("RL旋转");
  63. root = RLRotate(root);
  64. }else{
  65. System.out.println("RR旋转");//插入节点在失衡结点的右子树的右边
  66. root = RRRotate(root);
  67. }
  68. }
  69. }
  70. //重新调整root节点的高度值
  71. root.height = Math.max(getHeight(root.leftChild), getHeight(root.rightChild)) + 1;
  72. return root;
  73. }
  74. /**
  75. * LR旋转
  76. */
  77. public static Node LRRotate(Node p){
  78. p.leftChild = RRRotate(p.leftChild); // 先将失衡点p的左子树进行RR旋转
  79. return LLRotate(p); // 再将失衡点p进行LL平衡旋转并返回新节点代替原失衡点p
  80. }
  81. /**
  82. * RL旋转
  83. */
  84. public static Node RLRotate(Node p){
  85. p.rightChild = LLRotate(p.rightChild); // 先将失衡点p的右子树进行LL平衡旋转
  86. return RRRotate(p); // 再将失衡点p进行RR平衡旋转并返回新节点代替原失衡点p
  87. }
  88. /*
  89. * LL旋转
  90. * 右旋示意图(对结点20进行右旋)
  91. * 40 20
  92. * / \ / \
  93. * 20 50 10 40
  94. * / \ LL旋转 / / \
  95. * 10 30 5 30 50
  96. * /
  97. * 5
  98. */
  99. public static Node LLRotate(Node p){ // 40为失衡点
  100. Node lsubtree = p.leftChild; //失衡点的左子树的根结点20作为新的结点
  101. p.leftChild = lsubtree.rightChild; //将新节点的右子树30成为失衡点40的左子树
  102. lsubtree.rightChild = p; // 将失衡点40作为新结点的右子树
  103. // 重新设置失衡点40和新节点20的高度
  104. p.height = Math.max(getHeight(p.leftChild), getHeight(p.rightChild)) + 1;
  105. lsubtree.height = Math.max(getHeight(lsubtree.leftChild), p.height) + 1;
  106. return lsubtree; // 新的根节点取代原失衡点的位置
  107. }
  108. /*
  109. * RR旋转
  110. * 左旋示意图(对结点20进行左旋)
  111. * 20 40
  112. * / \ / \
  113. * 10 40 20 50
  114. * / \ RR旋转 / \ \
  115. * 30 50 10 30 60
  116. * \
  117. * 60
  118. */
  119. public static Node RRRotate(Node p){ // 20为失衡点
  120. Node rsubtree = p.rightChild; //失衡点的右子树的根结点40作为新的结点
  121. p.rightChild = rsubtree.leftChild; //将新节点的左子树30成为失衡点20的右子树
  122. rsubtree.leftChild = p; // 将失衡点20作为新结点的左子树
  123. // 重新设置失衡点20和新节点40的高度
  124. p.height = Math.max(getHeight(p.leftChild), getHeight(p.rightChild)) + 1;
  125. rsubtree.height = Math.max(getHeight(rsubtree.leftChild), getHeight(rsubtree.rightChild)) + 1;
  126. return rsubtree; // 新的根节点取代原失衡点的位置
  127. }
  128. }

总结

能看到这的,都是狠人。其实并不难,主要理解左旋和右旋的概念,我觉得就很清晰了。这篇也花了我一整天时间,基本我也是从0到1去接触的,如果感兴趣可以多研究研究。

更新记录

修改时间 修改内容
2021-7-20 二叉排序树删除操作(代码逻辑错误)