Skip to content

Instantly share code, notes, and snippets.

View jyhjuzi's full-sized avatar

Yanhong Ju jyhjuzi

  • China
View GitHub Profile
package Chapter5;
public class Q5_3{
public static void main(String[] arg){
int test1 = 0b001100111001111; // 001100111010111 // 001100110111110
System.out.println(Integer.toBinaryString(getNext(test1)));
System.out.println(Integer.toBinaryString(getPrevious(test1)));
int test2 = 0b001100000111100; // 001100001000111 // 1100000111010
System.out.println(Integer.toBinaryString(getNext(test2)));
System.out.println(Integer.toBinaryString(getPrevious(test2)));
package Chapter5;
public class Q5_2{
public static void main(String[] args){
System.out.println(printBinary(0.75));
System.out.println(printBinary(0.87948723));
}
static String printBinary(double input){
if(input>1 || input<0)
return "ERROR";
double remain = input;
public class Q5_1{
public static void main(String[] args){
int m =0b10011;
int n = 0b10000000000;
int i = 2;
int j = 6;
System.out.println(Integer.toBinaryString(insert(m,n,i,j)));
}
public class Q4_9{
public static void main(String[] args){
int[] array = { 1,2,3,7,5,-6,-4,8,3,-2,5,4 };
TreeNode<Integer> root1 = new Q4_3().arrayToBST(array, 0, 11);
/* -6
/ \
3 3
/ \ / \
1 7 -4 5
public class Q4_8{
public static void main(String[] args){
int[] array = { 1,2,3,4,5,6,7,8 };
int[] subArray ={5,6,7,8};
TreeNode<Integer> root1 = new Q4_3().arrayToBST(array, 0, 7);
TreeNode<Integer> root2 = new Q4_3().arrayToBST(subArray, 0, 3);
/*TreeNode<Integer> root2 = new TreeNode<Integer>(7,null,new TreeNode<Integer>(8));*/
System.out.println(isSubTree(root1,root2));
}
public static boolean isSubTree(TreeNode<Integer> root1, TreeNode<Integer> root2){
public class Q4_7 {
public static void main(String[] args) {
int[] test = { 1, 2, 3, 4, 5, 6, 7, 8 };
TreeNode<Integer> rootNode = new Q4_3().arrayToBST(test, 0, 7);
TreeNode<Integer> cur = rootNode;
System.out.println(covers(rootNode, rootNode.right.left));
System.out.println(covers(rootNode, rootNode.right.right.right));
System.out.println(findCommonAncestor(rootNode, rootNode.left.left,
rootNode.right).value); // 4
public class Q4_6{
public static void main(String[] args){
TreeNode4_6 root = new TreeNode4_6(9);
TreeNode4_6 node5 = new TreeNode4_6(5,root);
TreeNode4_6 node13 = new TreeNode4_6(13,root);
root.left = node5;
root.right = node13;
TreeNode4_6 node4 = new TreeNode4_6(4,node5);
node5.left= node4;
TreeNode4_6 node12 = new TreeNode4_6(12,node13);
public class Q4_5{
public static void main(String[] args){
TreeNode root1 = new TreeNode(7,new TreeNode(2,new TreeNode(1),new TreeNode(3,null,new TreeNode(8))),new TreeNode(11));
System.out.println(isBST(root1,Integer.MAX_VALUE,Integer.MIN_VALUE));
}
static boolean isBST(TreeNode root, int max, int min){
if(root == null)
return true;
int value = (Integer) root.value;
if(value>max || value<min)
import java.util.ArrayList;
import java.util.LinkedList;
public class Q4_4{
public static void main(String[] args){
int[] array ={1,2,3,4,5,6,7,8};
TreeNode test = new Q4_3().arrayToBST(array, 0,7);
ArrayList<LinkedList<TreeNode>> result = new ArrayList<LinkedList<TreeNode>>();
treeToLinkedLists2(test,result);
public class Q4_3{
public static void main(String[] args){
int[] test ={1,2,3,4,5,6,7,8};
TreeNode result = arrayToBST(test,1,6);
printOut(result);
}
private static void printOut(TreeNode result) {
if(result == null)
return;
printOut(result.left);