• SplayTree高分测试用例


    测试用例结果展示

    覆盖率

     变异得分

    测试注意点

    1. 从SplayTree测起,然后再测SubSplayTree,因为前者调用后者。
    2. SplaySubTree的remove方法大部分内容需要通过反射才能测到。
    3. value和index在SplayTree当中都不是唯一的。一个index可能对应多个value。

    不足之处

    1. 没考虑到异常怎么接住。
    2. 对SplayTree这个数据结构的理解还很浅显。

    测试文件MyTest.java

    1. package net.mooctest;
    2. import static org.junit.Assert.*;
    3. import java.lang.reflect.Field;
    4. import java.util.Arrays;
    5. import org.junit.Before;
    6. import org.junit.Test;
    7. public class MyTest {
    8. private Integer valArr[];
    9. private Integer remArr[];
    10. private Integer conArr[];
    11. private SplayTree splayTree;
    12. private int howmanynumbers;
    13. private int removeCnt;
    14. private int containsCnt;
    15. @Before
    16. public void initializeValArr() {
    17. this.howmanynumbers = 100;
    18. this.removeCnt = 0;
    19. this.containsCnt = 0;
    20. valArr = new Integer[howmanynumbers];
    21. remArr = new Integer[howmanynumbers/7+1];
    22. conArr = new Integer[howmanynumbers/9+1];
    23. for(int i=0;i<this.howmanynumbers;i++) {
    24. int val = (int)(Math.random()*100);
    25. // System.out.println(val);
    26. valArr[i] = val;
    27. if(i%7==0) {
    28. remArr[this.removeCnt++] = val;
    29. }
    30. if(i%9==0) {
    31. conArr[this.containsCnt++] = val;
    32. }
    33. }
    34. }
    35. @Test
    36. public void testMain() {
    37. SplayTree.main(null);
    38. }
    39. // 测试remove和contains
    40. @Test
    41. public void test001() {
    42. splayTree = new SplayTree();
    43. for(int i=0;i<this.howmanynumbers;i++) {
    44. splayTree.add(valArr[i]);
    45. assertNull(splayTree.root.join(splayTree.root));
    46. }
    47. assertNotNull(splayTree.root.add(null));
    48. assertEquals(howmanynumbers, splayTree.size());
    49. for(int i=0;i<this.removeCnt;i++) {
    50. int valToRemove = remArr[i];
    51. assertTrue(splayTree.contains(valToRemove));
    52. splayTree.remove(valToRemove);
    53. }
    54. assertEquals(howmanynumbers-removeCnt, splayTree.size());
    55. for(int i=0;i<this.containsCnt;i++) {
    56. int valToVarify = conArr[i];
    57. assertTrue(splayTree.contains(valToVarify));
    58. }
    59. }
    60. // 测试remove和contains
    61. @Test
    62. public void test002() {
    63. splayTree = new SplayTree();
    64. for(int i=0;i<this.howmanynumbers;i++) {
    65. splayTree.add(valArr[i]);
    66. assertNull(splayTree.root.split(splayTree.root.getData()));
    67. }
    68. assertEquals(howmanynumbers, splayTree.size());
    69. for(int i=0;i<this.removeCnt;i++) {
    70. int valToRemove = remArr[i];
    71. assertTrue(splayTree.contains(valToRemove));
    72. splayTree.remove(valToRemove);
    73. }
    74. assertEquals(howmanynumbers-removeCnt, splayTree.size());
    75. for(int i=0;i<this.containsCnt;i++) {
    76. int valToVarify = conArr[i];
    77. assertTrue(splayTree.contains(valToVarify));
    78. }
    79. }
    80. // 测试get和indexOf
    81. // 不能再用随机生成的数据,因为随机数据很可能重复
    82. @Test
    83. public void test003() {
    84. splayTree = new SplayTree();
    85. for(int i=0;i<this.howmanynumbers;i++) {
    86. splayTree.add(i*17);
    87. }
    88. assertEquals(howmanynumbers, splayTree.size());
    89. long idxArr[] = new long[howmanynumbers];
    90. for(int i=0;i
    91. idxArr[i] = splayTree.indexOf(i*17);
    92. }
    93. for(int i=0;i
    94. assertEquals(i*17, (int)splayTree.get(idxArr[i]));
    95. }
    96. assertNull(splayTree.get(-1));
    97. assertNull(splayTree.get(splayTree.size()+1));
    98. }
    99. //测试toString()
    100. @Test
    101. public void test004() {
    102. int primeArr[] = {
    103. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71
    104. };
    105. splayTree = new SplayTree();
    106. for(int i=0;i
    107. splayTree.add(primeArr[i]);
    108. }
    109. // System.out.println(splayTree.toString());
    110. // String expectedStr = " data=2 left= null right null sz=1 cnt=1\n";
    111. // assertEquals(expectedStr, splayTree.toString());
    112. String expectedStr = " data=71 left=67 right null sz=20 cnt=1\n" +
    113. " data=67 left=61 right null sz=19 cnt=1\n" +
    114. " data=61 left=59 right null sz=18 cnt=1\n" +
    115. " data=59 left=53 right null sz=17 cnt=1\n" +
    116. " data=53 left=47 right null sz=16 cnt=1\n" +
    117. " data=47 left=43 right null sz=15 cnt=1\n" +
    118. " data=43 left=41 right null sz=14 cnt=1\n" +
    119. " data=41 left=37 right null sz=13 cnt=1\n" +
    120. " data=37 left=31 right null sz=12 cnt=1\n" +
    121. " data=31 left=29 right null sz=11 cnt=1\n" +
    122. " data=29 left=23 right null sz=10 cnt=1\n" +
    123. " data=23 left=19 right null sz=9 cnt=1\n" +
    124. " data=19 left=17 right null sz=8 cnt=1\n" +
    125. " data=17 left=13 right null sz=7 cnt=1\n" +
    126. " data=13 left=11 right null sz=6 cnt=1\n" +
    127. " data=11 left=7 right null sz=5 cnt=1\n" +
    128. " data=7 left=5 right null sz=4 cnt=1\n" +
    129. " data=5 left=3 right null sz=3 cnt=1\n" +
    130. " data=3 left=2 right null sz=2 cnt=1\n" +
    131. " data=2 left= null right null sz=1 cnt=1\n";
    132. assertEquals(expectedStr, splayTree.toString());
    133. }
    134. //测试SplaySubTree--find
    135. @Test(timeout=4000)
    136. public void test005() {
    137. SplaySubTree splaySubTree = new SplaySubTree(null);
    138. assertNull(splaySubTree.find(1));
    139. int primeArr[] = {
    140. 2, 41, 5, 7, 67, 23, 11, 17, 19
    141. };
    142. for(int i=0;i
    143. splaySubTree = splaySubTree.add(primeArr[i]);
    144. }
    145. // System.out.println(splaySubTree.toString());
    146. assertNull(splaySubTree.find(20));
    147. }
    148. //测试SplaySubTree--remove
    149. @Test(timeout=4000)
    150. public void test006() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    151. SplaySubTree splaySubTree = new SplaySubTree(0);
    152. int primeArr[] = {
    153. 2, 41, 5, 7, 23, 11, 67, 17, 19
    154. };
    155. System.out.println(primeArr.length);
    156. for(int i=0;i
    157. splaySubTree = splaySubTree.add(primeArr[i]);
    158. }
    159. assertNotNull(splaySubTree.remove(20));
    160. assertEquals(primeArr.length+1,splaySubTree.remove(null).size());
    161. // 获取 SplaySubTree 的 Class 对象
    162. Class splaySubTreeClass = splaySubTree.getClass();
    163. // 获取私有属性 count 的 Field 对象
    164. Field countField = splaySubTreeClass.getDeclaredField("count");
    165. // 设置私有属性 count 的可访问性
    166. countField.setAccessible(true);
    167. splaySubTree = splaySubTree.remove(7);
    168. // 访问私有属性 count 的值
    169. int countValue = (int) countField.get(splaySubTree);
    170. // System.out.println("countValue: " + countValue);
    171. assertEquals(0, countValue);
    172. // System.out.println(splaySubTree.toString());
    173. splaySubTree = splaySubTree.remove(7);
    174. countValue = (int) countField.get(splaySubTree);
    175. // System.out.println("countValue: " + countValue);
    176. assertEquals(-1, countValue);
    177. }
    178. //测试SplaySubTree--remove
    179. @Test(timeout=4000)
    180. public void test007() {
    181. SplaySubTree splaySubTree = new SplaySubTree(1);
    182. splaySubTree.remove(1);
    183. }
    184. //测试SplaySubTree--remove
    185. @Test(timeout=4000)
    186. public void test008() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
    187. SplaySubTree splaySubTree = new SplaySubTree(1);
    188. // 获取 SplaySubTree 的 Class 对象
    189. Class splaySubTreeClass = splaySubTree.getClass();
    190. // 获取私有属性 count 的 Field 对象
    191. Field leftField = splaySubTreeClass.getDeclaredField("left");
    192. // 设置私有属性 count 的可访问性
    193. leftField.setAccessible(true);
    194. SplaySubTree splaySubTree2 = new SplaySubTree(2);
    195. leftField.set(splaySubTree,splaySubTree2);
    196. splaySubTree.remove(1);
    197. }
    198. //测试SplaySubTree--remove
    199. @Test(timeout=4000)
    200. public void test009() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
    201. SplaySubTree splaySubTree = new SplaySubTree(1);
    202. // 获取 SplaySubTree 的 Class 对象
    203. Class splaySubTreeClass = splaySubTree.getClass();
    204. // 获取私有属性 count 的 Field 对象
    205. Field rightField = splaySubTreeClass.getDeclaredField("right");
    206. // 设置私有属性 count 的可访问性
    207. rightField.setAccessible(true);
    208. SplaySubTree splaySubTree2 = new SplaySubTree(2);
    209. rightField.set(splaySubTree,splaySubTree2);
    210. splaySubTree.remove(1);
    211. }
    212. //测试SplaySubTree--remove
    213. @Test(timeout=4000)
    214. public void test0010() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
    215. SplaySubTree splaySubTree = new SplaySubTree(1);
    216. // 获取 SplaySubTree 的 Class 对象
    217. Class splaySubTreeClass = splaySubTree.getClass();
    218. // 获取私有属性 count 的 Field 对象
    219. Field leftField = splaySubTreeClass.getDeclaredField("left");
    220. Field parentField = splaySubTreeClass.getDeclaredField("parent");
    221. // 设置私有属性 count 的可访问性
    222. leftField.setAccessible(true);
    223. parentField.setAccessible(true);
    224. SplaySubTree splaySubTree2 = new SplaySubTree(2);
    225. leftField.set(splaySubTree,splaySubTree2);
    226. SplaySubTree splaySubTree3 = new SplaySubTree(3);
    227. parentField.set(splaySubTree,splaySubTree3);
    228. splaySubTree.remove(1);
    229. }
    230. //测试SplaySubTree--remove
    231. @Test(timeout=4000)
    232. public void test0011() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
    233. SplaySubTree splaySubTree = new SplaySubTree(1);
    234. // 获取 SplaySubTree 的 Class 对象
    235. Class splaySubTreeClass = splaySubTree.getClass();
    236. // 获取私有属性 count 的 Field 对象
    237. Field rightField = splaySubTreeClass.getDeclaredField("right");
    238. Field parentField = splaySubTreeClass.getDeclaredField("parent");
    239. // 设置私有属性 count 的可访问性
    240. rightField.setAccessible(true);
    241. parentField.setAccessible(true);
    242. SplaySubTree splaySubTree2 = new SplaySubTree(2);
    243. rightField.set(splaySubTree,splaySubTree2);
    244. SplaySubTree splaySubTree3 = new SplaySubTree(3);
    245. parentField.set(splaySubTree,splaySubTree3);
    246. splaySubTree.remove(1);
    247. }
    248. //测试SplaySubTree--remove
    249. @Test(timeout=4000)
    250. public void test0012() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
    251. SplaySubTree splaySubTree = new SplaySubTree(1);
    252. // 获取 SplaySubTree 的 Class 对象
    253. Class splaySubTreeClass = splaySubTree.getClass();
    254. // 获取私有属性 count 的 Field 对象
    255. Field rightField = splaySubTreeClass.getDeclaredField("right");
    256. Field leftField = splaySubTreeClass.getDeclaredField("left");
    257. // 设置私有属性 count 的可访问性
    258. rightField.setAccessible(true);
    259. leftField.setAccessible(true);
    260. SplaySubTree splaySubTree2 = new SplaySubTree(2);
    261. rightField.set(splaySubTree,splaySubTree2);
    262. SplaySubTree splaySubTree3 = new SplaySubTree(3);
    263. leftField.set(splaySubTree,splaySubTree3);
    264. String expectedStr = " data=1 left=3 right=2 sz=1 cnt=1\n" +
    265. " data=3 left= null right null sz=1 cnt=1\n" +
    266. " data=2 left= null right null sz=1 cnt=1\n";
    267. assertEquals(expectedStr, splaySubTree.toString());
    268. }
    269. }

    被测文件(1/2)SplayTree.java

    1. package net.mooctest;
    2. import java.util.Arrays;
    3. public class SplayTree extends Comparable> {
    4. SplaySubTree root;
    5. public SplayTree(){
    6. root = new SplaySubTree(null);
    7. }
    8. /**
    9. * @param index - of the node to search for.
    10. * @return - null if index<=0 or index>=size otherwise SubTree at index.
    11. */
    12. public T get(long index) {
    13. SplaySubTree cT = root.get(index);
    14. if(cT==null)return null;
    15. cT.splay();
    16. root = cT;
    17. return cT.getData();
    18. }
    19. /**
    20. * @return - the number of nodes in the tree.
    21. */
    22. public long size() { return root.size();}
    23. /**
    24. * @param node - to search for.
    25. * @return - the index of node. All nodes are ordered according to the compareTo(T) method.
    26. *
    27. */
    28. public long indexOf(T node) {
    29. long index = root.indexOf(node);
    30. get(index);
    31. return index;
    32. }
    33. /**
    34. * @param node - is added to the tree.
    35. * If node is null tree is unchanged.
    36. */
    37. public void add(T node) {
    38. root = root.add(node);
    39. }
    40. /**
    41. * @param node - is removed from the tree.
    42. * If node is null tree is unchanged.
    43. */
    44. public void remove(T node) {
    45. root = root.remove(node);
    46. }
    47. /**
    48. * @param node
    49. * @return
    50. */
    51. public boolean contains(T node) {
    52. SplaySubTree temp = root.find(node);
    53. if(temp!=null){
    54. temp.splay();
    55. root = temp;
    56. }
    57. return temp != null;
    58. }
    59. @Override
    60. public String toString(){
    61. return root.toString();
    62. }
    63. public static void main(String[] args) {
    64. SplayTree test = new SplayTree();
    65. int howmanynumbers = 10000;
    66. for (int i = 0; i < howmanynumbers; i++) {
    67. int val = (int)(Math.random()*100);
    68. test.add(val);
    69. }
    70. System.out.println(test);
    71. }
    72. }

    被测文件(2/2)SubSplayTree.java

    1. package net.mooctest;
    2. public class SplaySubTreeextends Comparable> {
    3. private T data;
    4. private SplaySubTree left, right, parent;
    5. private long size; // number of nodes in tree
    6. private int count;
    7. /**
    8. * @param node
    9. * - If node==null then size will be 0 otherwise node will be in the
    10. * tree and size will be 1
    11. */
    12. public SplaySubTree(T node) {
    13. data = node;
    14. if (node != null) {
    15. size = count = 1;
    16. }
    17. }
    18. public String toString() {
    19. String lft = "";
    20. String rght = "";
    21. String myData = " data=" + data;
    22. if (left != null) {
    23. myData += " left=" + left.data;
    24. lft = left.toString();
    25. } else {
    26. myData += " left= null";
    27. }
    28. if (right != null) {
    29. myData += " right=" + right.data;
    30. rght = right.toString();
    31. } else {
    32. myData += " right null";
    33. }
    34. myData += " sz="+size + " cnt="+count;
    35. return myData + "\n" + lft + rght;
    36. }
    37. public T getData() {
    38. return data;
    39. }
    40. /**
    41. * @param index
    42. * - of the node to search for.
    43. * @return - null if index<=0 or index>=size otherwise SubTree at index.
    44. */
    45. public SplaySubTree get(long index) {
    46. if (index > size || index < 0)
    47. return null;
    48. long cS = 1;
    49. SplaySubTree cT = this;
    50. if (cT.left != null)
    51. cS += cT.left.size;
    52. while (cS != index) {
    53. if (cS > index) {
    54. cS--;
    55. cT = cT.left;
    56. if (cT != null && cT.right != null)
    57. cS -= cT.right.size;
    58. } else {
    59. cS++;
    60. cT = cT.right;
    61. if (cT != null && cT.left != null)
    62. cS += cT.left.size;
    63. }
    64. }
    65. return cT;
    66. }
    67. /**
    68. * @return - the number of nodes in the tree.
    69. */
    70. public long size() {
    71. return size;
    72. }
    73. /**
    74. * @param node
    75. * - to search for.
    76. * @return - the index of node. All nodes are ordered according to the
    77. * compareTo(T) method.
    78. *
    79. */
    80. public long indexOf(T node) {
    81. if (node == null)
    82. return -1;
    83. long cI = 1;
    84. SplaySubTree cT = this;
    85. if (cT.left != null)
    86. cI += cT.left.size;
    87. while (!cT.data.equals(node)) {
    88. if (cT.data.compareTo(node) > 0) {
    89. cI--;
    90. cT = cT.left;
    91. if (cT != null && cT.right != null)
    92. cI -= cT.right.size;
    93. } else {
    94. cI++;
    95. cT = cT.right;
    96. if (cT != null && cT.left != null)
    97. cI += cT.left.size;
    98. }
    99. if (cT == null)
    100. return -1;
    101. }
    102. return cI;
    103. }
    104. /**
    105. * @param node
    106. * - is added to the tree. If node is null tree is unchanged.
    107. * @return - New root of the tree.
    108. */
    109. public SplaySubTree add(T node) {
    110. if (node == null)
    111. return this;
    112. if (this.data == null)
    113. return new SplaySubTree(node);
    114. SplaySubTree current = this;
    115. SplaySubTree child = null;
    116. if (this.data.compareTo(node) < 0)
    117. child = this.right;
    118. else if(this.data.compareTo(node)>0)
    119. child = this.left;
    120. while (child != null && current.data.compareTo(node)!=0) {
    121. current = child;
    122. if (current.data.compareTo(node) < 0)
    123. child = current.right;
    124. else if(current.data.compareTo(node)>0)
    125. child = current.left;
    126. }
    127. SplaySubTree newNode = new SplaySubTree(node);
    128. if (current.data.compareTo(node) < 0) {
    129. current.right = newNode;
    130. } else if(current.data.compareTo(node)>0){
    131. current.left = newNode;
    132. }else {
    133. current.size++;
    134. current.count++;
    135. newNode = current;
    136. current = newNode.parent;
    137. }
    138. newNode.parent = current;
    139. if (newNode.splay())
    140. return newNode;
    141. return this;
    142. }
    143. /**
    144. * @param node
    145. * - is removed from the tree. If node is null tree is unchanged.
    146. * @return - New root of the tree.
    147. */
    148. public SplaySubTree remove(T node) {
    149. if (node == null)
    150. return this;
    151. SplaySubTree x = find(node);
    152. if (x == null)
    153. return this;
    154. if(x.data.equals(node)) {
    155. x.count--;
    156. x.size--;
    157. if(size>0) {
    158. x.splay();
    159. return x;
    160. }
    161. }
    162. // To delete a node x:
    163. // if x has no children remove it.
    164. if (x.left == null && x.right == null) {
    165. if (x.parent != null) {
    166. if (x.parent.left == x) {
    167. parent.left = null;
    168. } else
    169. parent.right = null;
    170. } else
    171. return new SplaySubTree(null);
    172. }
    173. // if x has one child remove x, and put the child in place of x
    174. if (x.left == null) {
    175. if (x.parent != null) {
    176. if (x.parent.left == x) {
    177. parent.left = x.right;
    178. x.right.parent = parent;
    179. x = x.right;
    180. } else {
    181. parent.right = x.right;
    182. x.right.parent = parent;
    183. x = x.right;
    184. }
    185. } else {
    186. x.right.parent = null;
    187. return x.right;
    188. }
    189. } else if (x.right == null) {
    190. if (x.parent != null) {
    191. if (x.parent.left == x) {
    192. parent.left = x.left;
    193. x.left.parent = parent;
    194. x = x.left;
    195. } else {
    196. parent.right = x.left;
    197. x.left.parent = parent;
    198. x = x.left;
    199. }
    200. } else {
    201. x.left.parent = null;
    202. return x.left;
    203. }
    204. } else {
    205. // if x has two children, swap its value with that of
    206. // the rightmost node of its left sub tree
    207. SplaySubTree rmc = x.left;
    208. while (rmc.right != null)
    209. rmc = rmc.right;
    210. x.data = rmc.data;
    211. // Then remove that node instead.
    212. rmc.left.parent = rmc.parent;
    213. if (rmc.parent == x) {
    214. x.left = rmc.left;
    215. } else {
    216. rmc.parent.right = rmc.left;
    217. }
    218. x = rmc;
    219. }
    220. // After deletion, splay the parent of the removed node to the top of
    221. // the tree.
    222. x.splay();
    223. return x;
    224. }
    225. /**
    226. * @param other
    227. * @return
    228. */
    229. public SplaySubTree join(SplaySubTree other) {
    230. return null;
    231. }
    232. /**
    233. * @param node
    234. * @return
    235. */
    236. public SplaySubTree split(T node) {
    237. return null;
    238. }
    239. /**
    240. * @param node
    241. * @return
    242. */
    243. public SplaySubTree find(T node) {
    244. SplaySubTree current = this;
    245. if (this.data == null)
    246. return null;
    247. while (current != null) {
    248. if (node.equals(current.data))
    249. return current;
    250. if (node.compareTo(current.data) < 0)
    251. current = current.left;
    252. else
    253. current = current.right;
    254. }
    255. return current;
    256. }
    257. /**
    258. * Assuming this node is an interior or leaf node of a larger tree this method
    259. * moves this node up to the root balancing the tree in the process
    260. */
    261. public boolean splay() {
    262. while (this.parent != null) {
    263. SplaySubTree p = this.parent;
    264. SplaySubTree g = p.parent;
    265. if (g == null && this == p.left) {
    266. zig();
    267. } else if (g == null && this == p.right) {
    268. zag();
    269. } else if (p.left == this && g.left == p) {
    270. zigzig();
    271. } else if (p.right == this && g.right == p) {
    272. zagzag();
    273. } else if (p.right == this && g.left == p) {
    274. zigzag();
    275. } else {
    276. zagzig();
    277. }
    278. }
    279. return true;
    280. }
    281. /**
    282. * This is a helper method used in the splay() operation
    283. */
    284. private void zig() {
    285. SplaySubTree b = this.right;
    286. SplaySubTree p = this.parent;
    287. this.right = p;
    288. p.parent = this;
    289. p.left = b;
    290. if (b != null)
    291. b.parent = p;
    292. this.parent = null;
    293. p.size = p.count;
    294. if (p.right != null)
    295. p.size += p.right.size;
    296. if (b != null)
    297. p.size += b.size;
    298. this.size = p.size + this.count;
    299. if (this.left != null)
    300. this.size += this.left.size;
    301. }
    302. /**
    303. * This is a helper method used in the splay() operation
    304. */
    305. private void zag() {
    306. SplaySubTree b = this.left;
    307. SplaySubTree p = this.parent;
    308. this.left = p;
    309. p.parent = this;
    310. p.right = b;
    311. if (b != null)
    312. b.parent = p;
    313. this.parent = null;
    314. p.size = p.count;
    315. if (b != null)
    316. p.size += b.size;
    317. if (p.left != null)
    318. p.size += p.left.size;
    319. this.size = p.size + this.count;
    320. if (this.right != null)
    321. this.size += this.right.size;
    322. }
    323. /**
    324. * This is a helper method used by zigzig, zagzag, zigzag, zagzig This "fixes"
    325. * the great grandparent
    326. */
    327. private void fixGG(SplaySubTree g) {
    328. SplaySubTree gg = g.parent;
    329. if (gg != null) {
    330. if (g == gg.left)
    331. gg.left = this;
    332. if (g == gg.right)
    333. gg.right = this;
    334. }
    335. this.parent = gg;
    336. // might need to update size
    337. }
    338. /**
    339. * This is a helper method used in the splay() operation
    340. */
    341. private void zigzig() {
    342. SplaySubTree g = parent.parent;
    343. SplaySubTree b = this.right;
    344. SplaySubTree p = this.parent;
    345. SplaySubTree c = p.right;
    346. fixGG(g);
    347. if (b != null)
    348. b.parent = p;
    349. p.left = b;
    350. if (c != null)
    351. c.parent = g;
    352. g.left = c;
    353. g.parent = p;
    354. p.right = g;
    355. p.parent = this;
    356. this.right = p;
    357. g.size = g.count;
    358. if (c != null)
    359. g.size += c.size;
    360. if (g.right != null)
    361. g.size += g.right.size;
    362. p.size = p.count;
    363. if (g != null)
    364. p.size += g.size;
    365. if (b != null)
    366. p.size += b.size;
    367. this.size = p.size + this.count;
    368. if (this.left != null)
    369. this.size += this.left.size;
    370. }
    371. /**
    372. * This is a helper method used in the splay() operation
    373. */
    374. private void zagzag() {
    375. SplaySubTree g = parent.parent;
    376. SplaySubTree b = this.left;
    377. SplaySubTree p = this.parent;
    378. SplaySubTree c = p.left;
    379. fixGG(g);
    380. if (b != null)
    381. b.parent = p;
    382. // above line throws java.lang.NullPointerException
    383. p.right = b;
    384. if (c != null)
    385. c.parent = g;
    386. g.right = c;
    387. g.parent = p;
    388. p.left = g;
    389. p.parent = this;
    390. this.left = p;
    391. g.size = g.count;
    392. if (g.left != null)
    393. g.size += g.left.size;
    394. if (c != null)
    395. g.size += c.size;
    396. p.size = g.size + p.count;
    397. if (b != null)
    398. p.size += b.size;
    399. this.size = p.size + this.count;
    400. if (this.right != null)
    401. this.size += this.right.size;
    402. }
    403. /**
    404. * This is a helper method used in the splay() operation
    405. */
    406. private void zigzag() {
    407. SplaySubTree g = parent.parent;
    408. SplaySubTree b = this.left;
    409. SplaySubTree p = this.parent;
    410. SplaySubTree c = this.right;
    411. fixGG(g);
    412. if (b != null)
    413. b.parent = p;
    414. p.right = b;
    415. if (c != null)
    416. c.parent = g;
    417. g.left = c;
    418. p.parent = this;
    419. this.left = p;
    420. g.parent = this;
    421. this.right = g;
    422. g.size = g.count;
    423. if (g.right != null)
    424. g.size += g.right.size;
    425. if (c != null)
    426. g.size += c.size;
    427. p.size = p.count;
    428. if (p.left != null)
    429. p.size += p.left.size;
    430. if (b != null)
    431. p.size += b.size;
    432. this.size = g.size + p.size + this.count;
    433. }
    434. /**
    435. * This is a helper method used in the splay() operation
    436. */
    437. private void zagzig() {
    438. SplaySubTree g = parent.parent;
    439. SplaySubTree b = this.right;
    440. SplaySubTree p = this.parent;
    441. SplaySubTree c = this.left;
    442. fixGG(g);
    443. if (b != null)
    444. b.parent = p;
    445. p.left = b;
    446. if (c != null)
    447. c.parent = g;
    448. g.right = c;
    449. p.parent = this;
    450. this.right = p;
    451. g.parent = this;
    452. this.left = g;
    453. g.size = g.count;
    454. if (g.left != null)
    455. g.size += g.left.size;
    456. if (c != null)
    457. g.size += c.size;
    458. p.size = p.count;
    459. if (p.right != null)
    460. p.size += p.right.size;
    461. if (b != null)
    462. p.size += b.size;
    463. this.size = g.size + p.size + this.count;
    464. }
    465. }

  • 相关阅读:
    `Algorithm-Solution` `LeetCode` 6256. 将节点分成尽可能多的组
    MapReduce & YARN 的部署
    python协整与异步调用,压榨程序的摸鱼时间——使用异步编写需要循环执行的程序,并获取返回值(2)
    URL和URI
    【mysql】linux安装安装 mysql
    从 C 到 C++ 编程 — 基于 template 的泛型编程
    Dubbo中的Triple协议和应用级服务发现
    10.19QT作业
    线性表的插入、删除和查询操作
    43、Flink之Hive 读写及详细验证示例
  • 原文地址:https://blog.csdn.net/weixin_44997802/article/details/134353947