SplayTree高分测试用例

测试用例结果展示

覆盖率

 变异得分

测试注意点

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

不足之处

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

测试文件MyTest.java

package net.mooctest;

import static org.junit.Assert.*;

import java.lang.reflect.Field;
import java.util.Arrays;

import org.junit.Before;
import org.junit.Test;

public class MyTest {
	
	private Integer valArr[];
	private Integer remArr[];
	private Integer conArr[];
	private SplayTree<Integer> splayTree;
	private int howmanynumbers;
	private int removeCnt;
	private int containsCnt;
	
	@Before
	public void initializeValArr() {
		
		this.howmanynumbers = 100;
		this.removeCnt = 0;
		this.containsCnt = 0;
		
		valArr = new Integer[howmanynumbers];  
		remArr = new Integer[howmanynumbers/7+1];  
		conArr = new Integer[howmanynumbers/9+1];
		
		for(int i=0;i<this.howmanynumbers;i++) {
			int val = (int)(Math.random()*100);
//			System.out.println(val);
			valArr[i] = val;
			if(i%7==0) {
				remArr[this.removeCnt++] = val;
			}
			if(i%9==0) {
				conArr[this.containsCnt++] = val;
			}
		}
		
	}

	@Test
	public void testMain() {
		SplayTree.main(null);
	}
	
	// 测试remove和contains
	@Test
	public void test001() {
		splayTree = new SplayTree<Integer>();
		for(int i=0;i<this.howmanynumbers;i++) {
			splayTree.add(valArr[i]);
			assertNull(splayTree.root.join(splayTree.root));
		}
		assertNotNull(splayTree.root.add(null));
		assertEquals(howmanynumbers, splayTree.size());
		
		for(int i=0;i<this.removeCnt;i++) {
			int valToRemove = remArr[i];
			assertTrue(splayTree.contains(valToRemove));
			splayTree.remove(valToRemove);
		}
		assertEquals(howmanynumbers-removeCnt, splayTree.size());
		
		for(int i=0;i<this.containsCnt;i++) {
			int valToVarify = conArr[i];
			assertTrue(splayTree.contains(valToVarify));
		}
	}
	
	// 测试remove和contains
	@Test
	public void test002() {
		splayTree = new SplayTree<Integer>();
		for(int i=0;i<this.howmanynumbers;i++) {
			splayTree.add(valArr[i]);
			assertNull(splayTree.root.split(splayTree.root.getData()));
		}
		assertEquals(howmanynumbers, splayTree.size());
		
		for(int i=0;i<this.removeCnt;i++) {
			int valToRemove = remArr[i];
			assertTrue(splayTree.contains(valToRemove));
			splayTree.remove(valToRemove);
		}
		assertEquals(howmanynumbers-removeCnt, splayTree.size());
		
		for(int i=0;i<this.containsCnt;i++) {
			int valToVarify = conArr[i];
			assertTrue(splayTree.contains(valToVarify));
		}
	}
	
	// 测试get和indexOf
	// 不能再用随机生成的数据,因为随机数据很可能重复
	@Test
	public void test003() {
		splayTree = new SplayTree<Integer>();
		for(int i=0;i<this.howmanynumbers;i++) {
			splayTree.add(i*17);
		}
		assertEquals(howmanynumbers, splayTree.size());
		
		long idxArr[] = new long[howmanynumbers];
		
		for(int i=0;i<splayTree.size();i++) {
			idxArr[i] = splayTree.indexOf(i*17);
		}
		
		for(int i=0;i<splayTree.size();i++) {
			assertEquals(i*17, (int)splayTree.get(idxArr[i]));
		}
		assertNull(splayTree.get(-1));
		assertNull(splayTree.get(splayTree.size()+1));
	}
	
	
	//测试toString()
	@Test
	public void test004() {
		int primeArr[] = {
			2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 	
		};
		
		splayTree = new SplayTree<Integer>();
		
		for(int i=0;i<primeArr.length;i++) {
			splayTree.add(primeArr[i]);
		}
		
//		System.out.println(splayTree.toString());
//		String expectedStr = " data=2 left= null right null sz=1 cnt=1\n";
//		assertEquals(expectedStr, splayTree.toString());
		
		String expectedStr = " data=71 left=67 right null sz=20 cnt=1\n" + 
				" data=67 left=61 right null sz=19 cnt=1\n" + 
				" data=61 left=59 right null sz=18 cnt=1\n" + 
				" data=59 left=53 right null sz=17 cnt=1\n" + 
				" data=53 left=47 right null sz=16 cnt=1\n" + 
				" data=47 left=43 right null sz=15 cnt=1\n" + 
				" data=43 left=41 right null sz=14 cnt=1\n" + 
				" data=41 left=37 right null sz=13 cnt=1\n" + 
				" data=37 left=31 right null sz=12 cnt=1\n" + 
				" data=31 left=29 right null sz=11 cnt=1\n" + 
				" data=29 left=23 right null sz=10 cnt=1\n" + 
				" data=23 left=19 right null sz=9 cnt=1\n" + 
				" data=19 left=17 right null sz=8 cnt=1\n" + 
				" data=17 left=13 right null sz=7 cnt=1\n" + 
				" data=13 left=11 right null sz=6 cnt=1\n" + 
				" data=11 left=7 right null sz=5 cnt=1\n" + 
				" data=7 left=5 right null sz=4 cnt=1\n" + 
				" data=5 left=3 right null sz=3 cnt=1\n" + 
				" data=3 left=2 right null sz=2 cnt=1\n" + 
				" data=2 left= null right null sz=1 cnt=1\n";
		
		assertEquals(expectedStr, splayTree.toString());
	}
	
	//测试SplaySubTree--find
	@Test(timeout=4000)
	public void test005() {
		SplaySubTree<Integer> splaySubTree = new SplaySubTree<Integer>(null);
		assertNull(splaySubTree.find(1));
		int primeArr[] = {
			2, 41, 5, 7, 67, 23, 11,  17, 19
		};
		for(int i=0;i<primeArr.length;i++) {
			splaySubTree = splaySubTree.add(primeArr[i]);
		}
//		System.out.println(splaySubTree.toString());
		assertNull(splaySubTree.find(20));
	}
	
	//测试SplaySubTree--remove
	@Test(timeout=4000)
	public void test006() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		SplaySubTree<Integer> splaySubTree = new SplaySubTree<Integer>(0);
		
		int primeArr[] = {
			2, 41, 5, 7, 23, 11, 67, 17, 19
		};
		
		System.out.println(primeArr.length);
		
		for(int i=0;i<primeArr.length;i++) {
			splaySubTree = splaySubTree.add(primeArr[i]);
		}
		
		assertNotNull(splaySubTree.remove(20));
		
		assertEquals(primeArr.length+1,splaySubTree.remove(null).size());

  
        // 获取 SplaySubTree<Integer> 的 Class 对象  
        Class<?> splaySubTreeClass = splaySubTree.getClass();  
  
        // 获取私有属性 count 的 Field 对象  
        Field countField = splaySubTreeClass.getDeclaredField("count");  
  
        // 设置私有属性 count 的可访问性  
        countField.setAccessible(true); 
		
		splaySubTree = splaySubTree.remove(7);
		// 访问私有属性 count 的值  
        int countValue = (int) countField.get(splaySubTree);  
//        System.out.println("countValue: " + countValue);
        assertEquals(0, countValue);
//		System.out.println(splaySubTree.toString());
		splaySubTree = splaySubTree.remove(7);
		countValue = (int) countField.get(splaySubTree);  
//        System.out.println("countValue: " + countValue); 
        assertEquals(-1, countValue);

	}
	
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test007() {
			SplaySubTree<Integer> splaySubTree = new SplaySubTree<Integer>(1);
			splaySubTree.remove(1);
		}
		
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test008() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
			SplaySubTree<Integer> splaySubTree = new SplaySubTree<Integer>(1);
			// 获取 SplaySubTree<Integer> 的 Class 对象  
	        Class<?> splaySubTreeClass = splaySubTree.getClass();  
	        // 获取私有属性 count 的 Field 对象  
	        Field leftField = splaySubTreeClass.getDeclaredField("left");  
	        // 设置私有属性 count 的可访问性  
	        leftField.setAccessible(true);         
			SplaySubTree<Integer> splaySubTree2 = new SplaySubTree<Integer>(2);
			
			leftField.set(splaySubTree,splaySubTree2);
			
			
			
			splaySubTree.remove(1);
		}
		
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test009() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
			SplaySubTree<Integer> splaySubTree = new SplaySubTree<Integer>(1);
			// 获取 SplaySubTree<Integer> 的 Class 对象  
	        Class<?> splaySubTreeClass = splaySubTree.getClass();  
	        // 获取私有属性 count 的 Field 对象  
	        Field rightField = splaySubTreeClass.getDeclaredField("right");  
	        // 设置私有属性 count 的可访问性  
	        rightField.setAccessible(true);         
			SplaySubTree<Integer> splaySubTree2 = new SplaySubTree<Integer>(2);
			
			rightField.set(splaySubTree,splaySubTree2);
		
			splaySubTree.remove(1);
		}
				
		
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test0010() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
			SplaySubTree<Integer> splaySubTree = new SplaySubTree<Integer>(1);
			// 获取 SplaySubTree<Integer> 的 Class 对象  
	        Class<?> splaySubTreeClass = splaySubTree.getClass();  
	        // 获取私有属性 count 的 Field 对象  
	        Field leftField = splaySubTreeClass.getDeclaredField("left");
	        Field parentField = splaySubTreeClass.getDeclaredField("parent");  
	        // 设置私有属性 count 的可访问性  
	        leftField.setAccessible(true);   
	        parentField.setAccessible(true);   
	        
			SplaySubTree<Integer> splaySubTree2 = new SplaySubTree<Integer>(2);
			leftField.set(splaySubTree,splaySubTree2);
			SplaySubTree<Integer> splaySubTree3 = new SplaySubTree<Integer>(3);
			parentField.set(splaySubTree,splaySubTree3);
		
			splaySubTree.remove(1);
		}
		
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test0011() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
			SplaySubTree<Integer> splaySubTree = new SplaySubTree<Integer>(1);
			// 获取 SplaySubTree<Integer> 的 Class 对象  
	        Class<?> splaySubTreeClass = splaySubTree.getClass();  
	        // 获取私有属性 count 的 Field 对象  
	        Field rightField = splaySubTreeClass.getDeclaredField("right");  
	        Field parentField = splaySubTreeClass.getDeclaredField("parent"); 
	        // 设置私有属性 count 的可访问性  
	        rightField.setAccessible(true);   
	        parentField.setAccessible(true); 
	        
			SplaySubTree<Integer> splaySubTree2 = new SplaySubTree<Integer>(2);
			rightField.set(splaySubTree,splaySubTree2);
			SplaySubTree<Integer> splaySubTree3 = new SplaySubTree<Integer>(3);
			parentField.set(splaySubTree,splaySubTree3);
		
			splaySubTree.remove(1);
		}
		
		//测试SplaySubTree--remove
		@Test(timeout=4000)
		public void test0012() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NullPointerException {
			SplaySubTree<Integer> splaySubTree = new SplaySubTree<Integer>(1);
			// 获取 SplaySubTree<Integer> 的 Class 对象  
	        Class<?> splaySubTreeClass = splaySubTree.getClass();  
	        // 获取私有属性 count 的 Field 对象  
	        Field rightField = splaySubTreeClass.getDeclaredField("right");  
	        Field leftField = splaySubTreeClass.getDeclaredField("left"); 
	        // 设置私有属性 count 的可访问性  
	        rightField.setAccessible(true);   
	        leftField.setAccessible(true); 
	        
			SplaySubTree<Integer> splaySubTree2 = new SplaySubTree<Integer>(2);
			rightField.set(splaySubTree,splaySubTree2);
			SplaySubTree<Integer> splaySubTree3 = new SplaySubTree<Integer>(3);
			leftField.set(splaySubTree,splaySubTree3);

			String expectedStr = " data=1 left=3 right=2 sz=1 cnt=1\n" + 
					" data=3 left= null right null sz=1 cnt=1\n" + 
					" data=2 left= null right null sz=1 cnt=1\n";
			
			assertEquals(expectedStr, splaySubTree.toString());
		}

}

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

package net.mooctest;

import java.util.Arrays;

public class SplayTree <T extends Comparable<T>> {
	SplaySubTree<T> root;
	
	public SplayTree(){
		root = new SplaySubTree<T>(null);
	}
	
	/**
	 * @param index - of the node to search for.
	 * @return  - null if index<=0 or index>=size otherwise SubTree at index. 
	 */
	public T get(long index) {
		SplaySubTree<T> cT = root.get(index);
		if(cT==null)return null;
		cT.splay();
		root = cT;
		return cT.getData();
	}
	
	/**
	 * @return - the number of nodes in the tree.
	 */
	public long size() { return root.size();}

	/**
	 * @param node - to search for.
	 * @return - the index of node. All nodes are ordered according to the compareTo(T) method.
	 *         
	 */
	public long indexOf(T node) {
		long index = root.indexOf(node);
		get(index);
		return index;
	}
	
	/**
	 * @param node - is added to the tree.
	 *             If node is null tree is unchanged.
	 */
	public void add(T node) {
		root = root.add(node);
	}
	
	/**
	 * @param node - is removed from the tree.
	 *             If node is null tree is unchanged.
	 */
	public void remove(T node) {
		root = root.remove(node);
	}
	
	/**
	 * @param node
	 * @return
	 */
	public boolean contains(T node) {
		SplaySubTree<T> temp = root.find(node);
		if(temp!=null){
			temp.splay();
			root = temp;
		}
		return temp != null;
	}
	
	@Override
	public String toString(){
		return root.toString();
	}

	public static void main(String[] args) {
		SplayTree<Integer> test = new SplayTree<Integer>();
		int howmanynumbers = 10000;
		for (int i = 0; i < howmanynumbers; i++) {
			int val = (int)(Math.random()*100);
			test.add(val);
		}
		

		System.out.println(test);
	}
}

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

package net.mooctest;

public class SplaySubTree<T extends Comparable<T>> {

	private T data;
	private SplaySubTree<T> left, right, parent;
	private long size; // number of nodes in tree
	private int count;

	/**
	 * @param node
	 *            - If node==null then size will be 0 otherwise node will be in the
	 *            tree and size will be 1
	 */
	public SplaySubTree(T node) {
		data = node;
		if (node != null) {
			size = count = 1;
		}
	}
	
	

	public String toString() {
		String lft = "";
		String rght = "";
		String myData = " data=" + data;
		if (left != null) {
			myData += " left=" + left.data;
			lft = left.toString();
		} else {
			myData += " left= null";
		}
		if (right != null) {
			myData += " right=" + right.data;
			rght = right.toString();
		} else {
			myData += " right null";
		}
		myData += " sz="+size + " cnt="+count;
		return myData + "\n" + lft + rght;
	}

	public T getData() {
		return data;
	}

	/**
	 * @param index
	 *            - of the node to search for.
	 * @return - null if index<=0 or index>=size otherwise SubTree at index.
	 */
	public SplaySubTree<T> get(long index) {
		if (index > size || index < 0)
			return null;
		long cS = 1;
		SplaySubTree<T> cT = this;
		if (cT.left != null)
			cS += cT.left.size;
		while (cS != index) {
			if (cS > index) {
				cS--;
				cT = cT.left;
				if (cT != null && cT.right != null)
					cS -= cT.right.size;
			} else {
				cS++;
				cT = cT.right;
				if (cT != null && cT.left != null)
					cS += cT.left.size;
			}
		}
		return cT;
	}

	/**
	 * @return - the number of nodes in the tree.
	 */
	public long size() {
		return size;
	}

	/**
	 * @param node
	 *            - to search for.
	 * @return - the index of node. All nodes are ordered according to the
	 *         compareTo(T) method.
	 * 
	 */
	public long indexOf(T node) {
		if (node == null)
			return -1;
		long cI = 1;
		SplaySubTree<T> cT = this;
		if (cT.left != null)
			cI += cT.left.size;
		while (!cT.data.equals(node)) {
			if (cT.data.compareTo(node) > 0) {
				cI--;
				cT = cT.left;
				if (cT != null && cT.right != null)
					cI -= cT.right.size;
			} else {
				cI++;
				cT = cT.right;
				if (cT != null && cT.left != null)
					cI += cT.left.size;
			}
			if (cT == null)
				return -1;
		}

		return cI;
	}

	/**
	 * @param node
	 *            - is added to the tree. If node is null tree is unchanged.
	 * @return - New root of the tree.
	 */
	public SplaySubTree<T> add(T node) {
		if (node == null)
			return this;
		if (this.data == null)
			return new SplaySubTree<T>(node);
		SplaySubTree<T> current = this;
		SplaySubTree<T> child = null;

		if (this.data.compareTo(node) < 0)
			child = this.right;
		else if(this.data.compareTo(node)>0)
			child = this.left;
		while (child != null && current.data.compareTo(node)!=0) {
			current = child;
			if (current.data.compareTo(node) < 0)
				child = current.right;
			else if(current.data.compareTo(node)>0)
				child = current.left;
		}
		
		SplaySubTree<T> newNode = new SplaySubTree<T>(node);
		if (current.data.compareTo(node) < 0) {
			current.right = newNode;
		} else if(current.data.compareTo(node)>0){
			current.left = newNode;
		}else {
			current.size++;
			current.count++;
			newNode = current;
			current = newNode.parent;
		}
		newNode.parent = current;
		if (newNode.splay())
			return newNode;
		return this;
	}

	/**
	 * @param node
	 *            - is removed from the tree. If node is null tree is unchanged.
	 * @return - New root of the tree.
	 */
	public SplaySubTree<T> remove(T node) {
		if (node == null)
			return this;
		SplaySubTree<T> x = find(node);
		if (x == null)
			return this;
		if(x.data.equals(node)) {
			x.count--;
			x.size--;
			if(size>0) {
				x.splay();
				return x;
			}
		}
		// To delete a node x:
		// if x has no children remove it.

		if (x.left == null && x.right == null) {
			if (x.parent != null) {
				if (x.parent.left == x) {
					parent.left = null;
				} else
					parent.right = null;
			} else
				return new SplaySubTree(null);

		}
		// if x has one child remove x, and put the child in place of x
		if (x.left == null) {
			if (x.parent != null) {
				if (x.parent.left == x) {
					parent.left = x.right;
					x.right.parent = parent;
					x = x.right;
				} else {
					parent.right = x.right;
					x.right.parent = parent;
					x = x.right;
				}
			} else {
				x.right.parent = null;
				return x.right;
			}
		} else if (x.right == null) {
			if (x.parent != null) {
				if (x.parent.left == x) {
					parent.left = x.left;
					x.left.parent = parent;
					x = x.left;
				} else {
					parent.right = x.left;
					x.left.parent = parent;
					x = x.left;
				}
			} else {
				x.left.parent = null;
				return x.left;
			}
		} else {
			// if x has two children, swap its value with that of
			// the rightmost node of its left sub tree
			SplaySubTree<T> rmc = x.left;
			while (rmc.right != null)
				rmc = rmc.right;
			x.data = rmc.data;
			// Then remove that node instead.
			rmc.left.parent = rmc.parent;
			if (rmc.parent == x) {
				x.left = rmc.left;
			} else {
				rmc.parent.right = rmc.left;
			}
			x = rmc;
		}

		// After deletion, splay the parent of the removed node to the top of
		// the tree.
		x.splay();
		return x;
	}

	/**
	 * @param other
	 * @return
	 */
	public SplaySubTree<T> join(SplaySubTree<T> other) {
		return null;

	}

	/**
	 * @param node
	 * @return
	 */
	public SplaySubTree<T> split(T node) {
		return null;
	}

	/**
	 * @param node
	 * @return
	 */
	public SplaySubTree<T> find(T node) {
		SplaySubTree<T> current = this;
		if (this.data == null)
			return null;
		while (current != null) {
			if (node.equals(current.data))
				return current;
			if (node.compareTo(current.data) < 0)
				current = current.left;
			else
				current = current.right;
		}
		return current;
	}

	/**
	 * Assuming this node is an interior or leaf node of a larger tree this method
	 * moves this node up to the root balancing the tree in the process
	 */
	public boolean splay() {
		
		while (this.parent != null) {
			SplaySubTree<T> p = this.parent;
			SplaySubTree<T> g = p.parent;
			if (g == null && this == p.left) {
				zig();
			} else if (g == null && this == p.right) {
				zag();
			} else if (p.left == this && g.left == p) {
				zigzig();
			} else if (p.right == this && g.right == p) {
				zagzag();
			} else if (p.right == this && g.left == p) {
				zigzag();
			} else {
				zagzig();
			}
		}
		return true;
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zig() {
		SplaySubTree<T> b = this.right;
		SplaySubTree<T> p = this.parent;

		this.right = p;
		p.parent = this;
		p.left = b;
		if (b != null)
			b.parent = p;
		this.parent = null;
		p.size = p.count;
		if (p.right != null)
			p.size += p.right.size;
		if (b != null)
			p.size += b.size;
		this.size = p.size + this.count;
		if (this.left != null)
			this.size += this.left.size;
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zag() {
		SplaySubTree<T> b = this.left;
		SplaySubTree<T> p = this.parent;

		this.left = p;
		p.parent = this;
		p.right = b;
		if (b != null)
			b.parent = p;
		this.parent = null;
		p.size = p.count;
		if (b != null)
			p.size += b.size;
		if (p.left != null)
			p.size += p.left.size;
		this.size = p.size + this.count;
		if (this.right != null)
			this.size += this.right.size;
	}

	/**
	 * This is a helper method used by zigzig, zagzag, zigzag, zagzig This "fixes"
	 * the great grandparent
	 */
	private void fixGG(SplaySubTree<T> g) {
		SplaySubTree<T> gg = g.parent;
		if (gg != null) {
			if (g == gg.left)
				gg.left = this;
			if (g == gg.right)
				gg.right = this;
		}
		this.parent = gg;
		// might need to update size
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zigzig() {
		SplaySubTree<T> g = parent.parent;
		SplaySubTree<T> b = this.right;
		SplaySubTree<T> p = this.parent;
		SplaySubTree<T> c = p.right;
		fixGG(g);

		if (b != null)
			b.parent = p;
		p.left = b;
		if (c != null)
			c.parent = g;
		g.left = c;
		g.parent = p;
		p.right = g;
		p.parent = this;
		this.right = p;

		g.size = g.count;
		if (c != null)
			g.size += c.size;
		if (g.right != null)
			g.size += g.right.size;
		p.size = p.count;
		if (g != null)
			p.size += g.size;
		if (b != null)
			p.size += b.size;
		this.size = p.size + this.count;
		if (this.left != null)
			this.size += this.left.size;
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zagzag() {
		SplaySubTree<T> g = parent.parent;
		SplaySubTree<T> b = this.left;
		SplaySubTree<T> p = this.parent;
		SplaySubTree<T> c = p.left;

		fixGG(g);
		if (b != null)
			b.parent = p;
		// above line throws java.lang.NullPointerException

		p.right = b;
		if (c != null)
			c.parent = g;
		g.right = c;
		g.parent = p;
		p.left = g;
		p.parent = this;
		this.left = p;
		g.size = g.count;
		if (g.left != null)
			g.size += g.left.size;
		if (c != null)
			g.size += c.size;
		p.size = g.size + p.count;
		if (b != null)
			p.size += b.size;
		this.size = p.size + this.count;
		if (this.right != null)
			this.size += this.right.size;
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zigzag() {
		SplaySubTree<T> g = parent.parent;
		SplaySubTree<T> b = this.left;
		SplaySubTree<T> p = this.parent;
		SplaySubTree<T> c = this.right;

		fixGG(g);
		if (b != null)
			b.parent = p;
		p.right = b;
		if (c != null)
			c.parent = g;
		g.left = c;
		p.parent = this;
		this.left = p;
		g.parent = this;
		this.right = g;
		g.size = g.count;
		if (g.right != null)
			g.size += g.right.size;
		if (c != null)
			g.size += c.size;
		p.size = p.count;
		if (p.left != null)
			p.size += p.left.size;
		if (b != null)
			p.size += b.size;
		this.size = g.size + p.size + this.count;
	}

	/**
	 * This is a helper method used in the splay() operation
	 */
	private void zagzig() {

		SplaySubTree<T> g = parent.parent;
		SplaySubTree<T> b = this.right;
		SplaySubTree<T> p = this.parent;
		SplaySubTree<T> c = this.left;
		fixGG(g);
		if (b != null)
			b.parent = p;
		p.left = b;
		if (c != null)
			c.parent = g;
		g.right = c;
		p.parent = this;
		this.right = p;
		g.parent = this;
		this.left = g;
		g.size = g.count;
		if (g.left != null)
			g.size += g.left.size;
		if (c != null)
			g.size += c.size;
		p.size = p.count;
		if (p.right != null)
			p.size += p.right.size;
		if (b != null)
			p.size += b.size;
		this.size = g.size + p.size + this.count;
	}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/133417.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

[西湖论剑 2022]real_ez_node

文章目录 前置知识EJS模板注入&#xff08;CVE-2022-29078&#xff09;原型链污染漏洞 &#xff08;CVE-2021-25928&#xff09;HTTP响应拆分攻击&#xff08;CRLF&#xff09; 解题过程代码审计构造payload 前置知识 EJS模板注入&#xff08;CVE-2022-29078&#xff09; EJS…

ChatGPT 如何改变科研之路

《Nature》全球博士后调查[1]中约有三分之一的受访者正在使用人工智能聊天机器人来帮助完善文本、生成或编辑代码、整理其领域的文献等等。 来自巴西的 Rafael Bretas 在日本生活了十多年&#xff0c;日语说得很好。书面日语的各个方面&#xff0c;例如严格的礼貌等级制度&…

【蓝桥每日一题]-快速幂,倍增,滑动窗口(保姆级教程 篇1) #麦森数 #青蛙跳

之前是考试准备&#xff0c;所以有几天没更新&#xff0c;今天开始继续更新 目录 快速幂模板 题目&#xff1a;麦森数 思路&#xff1a; 题目&#xff1a;青蛙跳 思路&#xff1a; 快速幂模板 #include <bits/stdc.h> #define ll long long using namespa…

如何知道一个程序为哪些信号注册了哪些信号处理函数?

https://unix.stackexchange.com/questions/379694/is-there-a-way-to-know-if-signals-are-present-in-your-application-and-which-sign 使用 strace

数据结构与算法【二分查找】Java实现

需求&#xff1a;在有序数组 A 内&#xff0c;查找值target 如果找到返回索引如果找不到返回 -1 前提 给定一个内含 n 个元素的有序数组 A&#xff0c;一个待查值 target 1 设置 i0&#xff0c;jn-1 2 如果 i \gt j&#xff0c;结束查找&#xff0c;没找到 3 设置 m (…

只会CRUD程序员朋友,你开始拥抱云计算技术了吗

阅读建议 嗨&#xff0c;伙计&#xff01;刷到这篇文章咱们就是有缘人&#xff0c;在阅读这篇文章前我有一些建议&#xff1a; 本篇文章大概6000多字&#xff0c;预计阅读时间长需要6分钟。本篇文章的理论性较强&#xff0c;是一篇质量分数较高的技术文章&#xff0c;建议收藏…

STM32F407的看门狗

文章目录 看门狗时钟两种看门狗IWDG结构图作用 寄存器IWDG_KR键值寄存器IWDG_PR预分频寄存器2-0 PR预分频器系数 IWDG_RLR重装载寄存器IWDG_SR状态寄存器1RVU 重载值更新0 PVU 预分频值更新注意 写保护超时时间使用步骤取消寄存器 写保护设置预分频系数和重装载值看门狗溢出时间…

阿里云服务器搭建sql 服务

阿里云搭建mysql服务 环境准备 系统镜像 ubuntu 如果买点的实例不是ubuntu 系统镜像&#xff0c;需要停止服务之后&#xff0c;更改镜像 更新 apt-get &#xff1a; 更新apt-get: sudo apt-get update 如果没有出现&#xff1a;apt-get 找不到此命令的错误&#xff0c;可能是…

【教3妹学编程-算法题】2923. 找到冠军 I

3妹&#xff1a;2哥2哥&#xff0c;你看到新闻了吗&#xff1f;襄阳健桥医院院长 公然“贩卖出生证明”&#xff0c; 真是太胆大包天了吧。 2哥 : 我也看到新闻了&#xff0c;7人被采取刑事强制措施。 就应该好好查查他们&#xff0c; 一查到底&#xff01; 3妹&#xff1a;真的…

箱线图(boxplot)

箱线图 boxplot 简述原理绘制方法python - matplotlib加载功能模块加载数据绘制boxplot python - seaborn加载功能模块加载数据绘制boxplot R - ggplot加载功能模块加载数据绘制boxplot 简述 因图形形状如箱子而得名。箱线图常用于展示一组连续型数据的分散情况。学术界普遍认…

SQL SERVER Inregration Services-OLE DB、Oracle和ODBC操作

OLE DB链接器 OLE DB插件下载&#xff1a;https://learn.microsoft.com/zh-cn/sql/connect/oledb/download-oledb-driver-for-sql-server?viewsql-server-ver16 配置OLE DB Connection Manager 在点击“新建”时&#xff0c;会弹出警告信息“不支持指定的提供程序&#xff0…

文件夹批量改名技巧:简单步骤,实现文件夹随机重命名

在日常生活和工作中&#xff0c;经常需要处理大量的文件夹&#xff0c;需要对其进行有效的管理。在这些情况下&#xff0c;文件夹的命名就变得非常重要。一个好的命名策略可以快速找到所需的文件夹&#xff0c;也可以帮助更好地组织文件。然而&#xff0c;手动为每个文件夹重命…

研究方法——案例研究设计与方法

作者&#xff1a;罗伯特K.殷 &#xff08;一&#xff09;计划&#xff1a;如何把握何处、何时用案例研究方法 1.问题&#xff1a; 按照作者的观点&#xff0c;案例研究1984年之后才逐渐得到重视&#xff0c;可是在数据信息有效收集的时代&#xff0c;几乎所有的经典都是以案例…

电路中模拟地和数字地的分割方法

电路中只要是地&#xff0c;最终都要接到一起&#xff0c;然后入大地。如果不接在一起就是“浮地”&#xff0c;存在压差&#xff0c;容易积累电荷&#xff0c;造成静电。 地是参考0电位&#xff0c;所有电压都是参考地得出的&#xff0c;地的标准一致&#xff0c;故各种地应短…

计蒜客详解合集(3)期

目录 T1236——分苹果 T1113——整理药名 T1153——整数奇偶排列 T1249——漂亮的字符串 T1168——统计素数个数 T1160——甲流病人筛选 T1236——分苹果 分享一道特别简单的题。 蒜头君要把一堆苹果分给 个小朋友&#xff0c;要使每个人都能拿到苹果&#xff0c;而目每…

搞怪python代码

微信消息重发代码&#xff1a; from pynput.keyboard import Key,Controller import time keyboard Controller()a input("请输入你需要循环输出的内容&#xff1a;") b eval(input(请输入你想要循环的次数&#xff1a;)) print("数据已接收&#xff01;请将…

一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

目录 1解题思路&#xff1a; 2代码如下&#xff1a; 3运行结果&#xff1a; 4总结&#xff1a; 5介绍&#xff1a; 1解题思路&#xff1a; 利用循环&#xff08;穷举法&#xff09;来 对 所 需要的数 进行确定 2代码如下&#xff1a; #include <stdio.h>int main() …

【Python大数据笔记_day06_Hive】

hive内外表操作 建表语法 create [external] table [if not exists] 表名(字段名 字段类型 , 字段名 字段类型 , ... ) [partitioned by (分区字段名 分区字段类型)] # 分区表固定格式 [clustered by (分桶字段名) into 桶个数 buckets] # 分桶表固定格式 注意: 可以排序[so…

Java中的多态究竟是什么?

目录 一.概念二.使用条件三.重写1.概念2.使用条件3.与重载对比4.举例5.为什么需要重写1.重写规则 2.静态绑定--重载3.动态绑定--重写 四.向上转型第一种传参方式&#xff1a;直接赋值第二种传参方式&#xff1a;通过传参优缺点 五.向下转型举例缺点 六.多态的优缺点优点缺点 一…

ros1 基础学习08- 实现Server端自定义四 Topic模式控制海龟运动

一、服务模型 Server端本身是进行模拟海龟运动的命令端&#xff0c;它的实现是通过给海龟发送速度&#xff08;Twist&#xff09;的指令&#xff0c;来控制海龟运动&#xff08;本身通过Topic实现&#xff09;。 Client端相当于海龟运动的开关&#xff0c;其发布Request来控制…