/** * 递归:在方法体重调用本身这个方法 **/ public class DiGui { public static void main(String[] args) { DiGui.test(0); } public static void test(int value){ if(value<100){ System.out.println("第一步:" value); test( value); System.out.println("第二步:" value); System.out.println("第三步:" value); } }
}
---------------------------------------------------------------------------------------------------------------------------------
/** * 三角数字:数列中第1项为1,第n项由n-1项加n得到 **/ public class SanJiao { public static void main(String[] args) { //System.out.println(SanJiao(1)); //System.out.println(SanJiao(2)); //System.out.println(SanJiao(3)); //System.out.println(SanJiao(4)); System.out.println(DiGuiSanJiao(1)); System.out.println(DiGuiSanJiao(2)); System.out.println(DiGuiSanJiao(3)); System.out.println(DiGuiSanJiao(4)); } public static int SanJiao(int n){ int total=0; while (n>0){ total=total n; n--; } return total; } public static int DiGuiSanJiao(int n){ if(n==1){ return 1; }else{ return n DiGuiSanJiao(--n); } }
------------------------------------------------------------------------------------------------------------------------------
/** * Fibonacci数列中第1,2项为1,第n项由n-1项加n-2项得到 **/ public class TestFibonacci { public static void main(String[] args) { System.out.println(Fibonacci(1)); System.out.println(Fibonacci(2)); System.out.println(Fibonacci(3)); System.out.println(Fibonacci(4)); System.out.println(Fibonacci(5)); System.out.println(Fibonacci(6)); } public static int Fibonacci(int n){ if(n==1){ return 1; }else if(n==2){ return 1; }else { return Fibonacci(n-1) Fibonacci(n-2); } } }
}
------------------------------------------------------------------------------------------------------------------------------
/** * 核心思想:链接点中包含一个数据域和一个指针域,其中数据域用来包装数据,而指针域用来指向下一个链接点 **/ public class Link { private long data; private Link next; public Link(long data) { this.data = data; } public long getData() { return data; } public void setData(long data) { this.data = data; } public Link getNext() { return next; } public void setNext(Link next) { this.next = next; } /*public static void main(String[] args) { Link link1 = new Link(10); Link link2 = new Link(30); Link link3 = new Link(100); Link link4 = new Link(20); link1.setNext(link2); link2.setNext(link3); link3.setNext(link4); System.out.println(link1.getData()); System.out.println(link1.getNext().getData()); }*/
}
/** * 链表:链表中只包含一个数据项,即对第一个链接点的引用 **/ public class LinkList { private Link first; public void insert(long value){ Link link=new Link(value); if(first==null){ first=link; }else{ link.setNext(first); first=link; } } public void displayAll(){ Link current=first; while (current!=null){ System.out.println(current.getData()); current=current.getNext(); } } public Link find(long data){ Link current=first; while (current.getData()!=data){ if(current.getNext()==null){ return null; } current=current.getNext(); } return current; } public void insert(long data,int pos){ if(pos==0){ this.insert(data); }else{ Link current=first; for (int j=0;j<pos-1;j ){ current=current.getNext(); } Link link=new Link(data); link.setNext(current.getNext()); current.setNext(link); } } public void delete(long data){ Link current = first; Link ago = first; while (current.getData()!=data){ if(current.getNext() == null) { return; } else { ago = current; current = current.getNext(); } } if(current==first){ first=first.getNext(); }else{ ago.setNext(current.getNext()); } } public static void main(String[] args) { LinkList linkList = new LinkList(); linkList.insert(40); linkList.insert(12); linkList.insert(23); linkList.insert(10); linkList.displayAll(); System.out.println("找到结点,数据为 " linkList.find(23).getData()); linkList.insert(20, 0); System.out.println("-----------------------"); linkList.displayAll(); linkList.delete(12); System.out.println("-----------------------"); linkList.displayAll(); } }
------------------------------------------------------------------------------------------------------------------------------
/** * 在优先级队列中,数据项按关键字的值有序,这样关键字最小的数据项(或者最大)总是在队头, * 数据项插入时会按照顺序插入到合适的位置,确保队列的顺序 **/ public class PriorityQueue { private long[] arr; private int maxSize; private int elems; public PriorityQueue(int maxSize){ this.maxSize=maxSize; arr=new long[maxSize]; elems=0; } public void insert(long value) { int i; for (i = 0; i < elems; i ) { if(value < arr[i]) { break; } } System.out.println("i:" i); for(int j = elems; j > i;j--){ arr[j] = arr[j - 1]; } arr[i] = value; elems ; } public long remove() { long value = arr[elems - 1]; elems--; return value; } public boolean isEmpty() { return (elems == 0); } public boolean isFull() { return (elems == maxSize); } public int size() { return elems; } /*public static void main(String[] args) { PriorityQueue priorityQueue = new PriorityQueue(30); priorityQueue.insert(20); priorityQueue.insert(15); priorityQueue.insert(30); while (!priorityQueue.isEmpty()){ long value = priorityQueue.remove(); System.out.print(value " "); } }*/ }
------------------------------------------------------------------------------------------------------------------------------
public class Node { //关键字 private int keyData; //其他数据 private int otherData; //左子节点 private Node leftNode; //右子节点 private Node rightNode; public Node(int keyData, int otherData) { this.keyData = keyData; this.otherData = otherData; } public int getKeyData() { return keyData; } public void setKeyData(int keyData) { this.keyData = keyData; } public int getOtherData() { return otherData; } public void setOtherData(int otherData) { this.otherData = otherData; } public Node getLeftNode() { return leftNode; } public void setLeftNode(Node leftNode) { this.leftNode = leftNode; } public Node getRightNode() { return rightNode; } public void setRightNode(Node rightNode) { this.rightNode = rightNode; } //显示方法 public void display(){ System.out.println(keyData "," otherData); } }
/** * 二叉树:树中每个节点最多只能有两个子节点 * 插入节点:1.如果不存在节点,则直接插入。 * 2.从根开始查找一个相应的节点,即新节点的父节点,当父节点找到后,根据新节点的值来确定新节点是连接到左子节点还是右子节点 * * 查找节点:从根开始查找,如果查找节点值比父节点要小,则查找其左子树,否则查找其右子树,直到查找到为止,如果不存在节点,则返回null * 修改节点:首先查找节点,找到相应节点后,再进行修改。 * 遍历二叉树:遍历二叉树分为三种方法,一种先序遍历,一种中序遍历,一种后序遍历 * 前序遍历:根节点->左子树->右子树 * * 中序遍历:左子树->根节点->右子树 * * 后序遍历:左子树->右子树->根节点 **/ public class Tree { //根 private Node root; //插入节点 public void insert(int keyData,int otherData){ Node newNode = new Node(keyData, otherData); //如果说没有节点 if(root==null){ root=newNode; }else{ Node current=root; Node parent; while (true){ parent=current; if(keyData < current.getKeyData()){ current=current.getLeftNode(); if(current == null){ parent.setLeftNode(newNode); return; } }else { current=current.getRightNode(); if(current == null){ parent.setRightNode(newNode); return; } } } } } //查找 public Node find(int keyData){ Node current=root; while (current.getKeyData() != keyData){ if(keyData < current.getKeyData()){ current=current.getLeftNode(); }else{ current=current.getRightNode(); } if(current == null){ return null; } } return current; } //修改方法 public void change(int keyData, int newOtherData){ Node findNode = find(keyData); findNode.setOtherData(newOtherData); } //先序遍历 public void preOrder(Node node) { if (node != null) { node.display(); preOrder(node.getLeftNode()); preOrder(node.getRightNode()); } } // 中序遍历 public void inOrder(Node node) { if (node != null) { inOrder(node.getLeftNode()); node.display(); inOrder(node.getRightNode()); } } // 后序遍历 public void endOrder(Node node) { if (node != null) { endOrder(node.getLeftNode()); endOrder(node.getRightNode()); node.display(); } } public Node getRoot() { return root; } public void setRoot(Node root) { this.root = root; } public static void main(String[] args) { Tree tree = new Tree(); tree.insert(10,23); tree.insert(7,17); tree.insert(18,28); tree.insert(12,22); tree.preOrder(tree.getRoot()); System.out.println("-----------"); tree.inOrder(tree.getRoot()); System.out.println("-----------"); tree.endOrder(tree.getRoot()); } }
------------------------------------------------------------------------------------------------------------------------------
未完待续!