-------------------------單向鏈表java
1 //自制單向鏈表 2 class MyList { 3 private Cell head; 4 private int length; 5 class Cell { 6 private String data; 7 private Cell next; 8 public Cell(String data) { 9 this.data = data; 10 } 11 } 12 public void add(String data) { 13 Cell c = new Cell(data); 14 Cell tail = getTail(); 15 if (tail == null) { 16 head = c; 17 } else { 18 tail.next = c; 19 } 20 this.length++; 21 } 22 public int size() { 23 return this.length; 24 } 25 public Cell getTail() { 26 if (head == null) { 27 return null; 28 } 29 Cell p = head; 30 while (p.next != null) { 31 p = p.next; 32 } 33 return p; 34 } 35 public String get(int index) { 36 if (index >= this.length) { 37 throw new IndexOutOfBoundsException("下標越界"); 38 } 39 Cell p = head; 40 for (int i = 0; i < index; i++) { 41 p = p.next; 42 } 43 return p.data; 44 } 45 } 46 public class Demo { 47 public static void main(String[] args) { 48 MyList list = new MyList(); 49 list.add("lala"); 50 list.add("hehe"); 51 list.add("haha"); 52 list.add("heihei"); 53 System.out.println(list.size()); 54 for (int i = 0; i < list.size(); i++) { 55 System.out.println(list.get(i)); 56 } 57 } 58 }
---------------------二叉樹node
1 /** 2 * 自定義二叉樹,存放int型數據 中序排列 3 */ 4 class MyTree { 5 // 定義樹根 6 private Node root; 7 // 定義二叉樹節點 8 class Node { 9 private int data;// 存放數據 10 private Node left;// 左子樹地址 11 private Node right;// 右子樹地址 12 public Node(int data) { 13 // 初始化成員變量 14 this.data = data; 15 this.left = this.right = null; 16 } 17 /** 18 * 根據規則,把節點添加到二叉樹中 19 * 20 * @param n 21 * 節點 22 */ 23 public void add(Node n) { 24 // 首先比較data的大小,大於根節點放在右邊,小於根節點放在左邊 25 if (this.data > n.data) { 26 if (this.left != null) { 27 this.left.add(n); 28 } else { 29 this.left = n; 30 } 31 } else { 32 if (this.right != null) { 33 this.right.add(n); 34 } else { 35 this.right = n; 36 } 37 } 38 } 39 public void bianLi() { 40 if (this.left != null) { 41 this.left.bianLi(); 42 } 43 System.out.println(this.data); 44 if (this.right != null) { 45 this.right.bianLi(); 46 } 47 } 48 } 49 /** 50 * 定義add方法 51 * 52 * @param data 53 * 要存放的數據 54 */ 55 public void add(int data) { 56 // 實例化一個節點 57 Node n = new Node(data); 58 // 添加到二叉樹中,首先判斷樹是否存在,若是不存在,就把這個節點當作根節點 59 // 若是存在,調用Node的add方法,將節點添加進去 60 if (this.root == null) { 61 this.root = n; 62 } else { 63 this.root.add(n); 64 } 65 } 66 public void bianLi() { 67 if (this.root == null) { 68 System.out.println("空"); 69 } else { 70 this.root.bianLi(); 71 } 72 } 73 } 74 public class Demo3 { 75 public static void main(String[] args) { 76 MyTree tree = new MyTree(); 77 for (int i = 0; i < 20; i++) { 78 int num = (int) (Math.random() * 100 + 1); 79 tree.add(num); 80 } 81 tree.bianLi(); 82 } 83 }
-------------------------雙向鏈表微信
1 //雙向鏈表 2 class LinkedList { 3 private Node head; 4 private Node tail; 5 /** 6 * 定義構造方法,初始化頭結點和尾結點。 7 */ 8 public LinkedList() { 9 head = new Node("Head"); 10 tail = new Node("Tail"); 11 head.next = tail; 12 tail.prev = head; 13 } 14 /** 15 * 定義結點類 16 */ 17 class Node { 18 private String data; 19 private Node prev; 20 private Node next; 21 public Node(String data) { 22 this.data = data; 23 this.prev = this.next = null; 24 } 25 } 26 /** 27 * 從頭結點插入,每次都是在頭結點以後插入 28 * 29 * @param data 30 * 結點數據 31 */ 32 public void addFirst(String data) { 33 Node n = new Node(data); 34 n.prev = head; 35 n.next = head.next; 36 head.next = n; 37 n.next.prev = n; 38 } 39 /** 40 * 從尾結點插入 41 * 42 * @param data 43 * 結點數據 44 */ 45 public void addTail(String data) { 46 Node n = new Node(data); 47 n.next = tail; 48 n.prev = tail.prev; 49 tail.prev = n; 50 n.prev.next = n; 51 } 52 /** 53 * 獲得鏈表的size 54 * @return int 鏈表的size 55 */ 56 public int size() { 57 Node p = head.next; 58 int count = 0; 59 while (true) { 60 if (p.next == null) { 61 break; 62 } 63 p = p.next; 64 count++; 65 } 66 return count; 67 } 68 /** 69 * 獲得指定位置的數據 70 * @param index int 索引 71 * @return String 指定索引處的數據 72 */ 73 public String get(int index) { 74 if (index >= this.size() || index < 0) { 75 throw new IndexOutOfBoundsException("下標越界"); 76 } 77 Node p = head.next; 78 for (int i = 0; i < index; i++) { 79 p = p.next; 80 } 81 return p.data; 82 } 83 } 84 // 雙向循環鏈表 85 class DoubleLinkedList { 86 // 定義頭結點 87 private Node head; 88 private int length; 89 public DoubleLinkedList() { 90 this.head = null; 91 this.length = 0; 92 } 93 class Node { 94 private String data; 95 private Node prev; 96 private Node next; 97 public Node(String data) { 98 this.data = data; 99 this.prev = this.next = this; 100 } 101 } 102 // 順序添加到鏈表 103 public void add(String data) { 104 // 實例化節點 105 Node n = new Node(data); 106 // 獲得最後一個節點 107 Node last = getLast(); 108 if (last == null) { 109 head = n; 110 } else { 111 // 總共修改四個值,最後一個節點的next,新加節點的prev和next,頭結點的prev 112 last.next = n; 113 n.prev = last; 114 n.next = head; 115 head.prev = n; 116 } 117 this.length++; 118 } 119 public Node getLast() { 120 if (this.length == 0) { 121 return null; 122 } 123 return head.prev; 124 } 125 public int size() { 126 return this.length; 127 } 128 public String get(int index) { 129 Node n = head; 130 if (index < 0 || index > length - 1) { 131 throw new IndexOutOfBoundsException("下標越界"); 132 } 133 for (int i = 0; i < index; i++) { 134 n = n.next; 135 } 136 return n.data; 137 } 138 } 139 public class Demo4 { 140 public static void main(String[] args) { 141 DoubleLinkedList dll = new DoubleLinkedList(); 142 dll.add("lala"); 143 dll.add("hehe"); 144 dll.add("jaja"); 145 dll.add("fafa"); 146 System.out.println(dll.size()); 147 for (int i = 0; i < dll.size(); i++) { 148 System.out.print(dll.get(i) + " "); 149 } 150 System.out.println(); 151 LinkedList ll = new LinkedList(); 152 ll.addFirst("11");// 首插入 153 ll.addFirst("22");// 首插入,而後"11"後移 154 ll.addTail("33");// 尾插入 155 ll.addTail("44");// 尾插入,"33"前移 156 System.out.println(ll.size()); 157 for (int i = 0; i < ll.size(); i++) { 158 System.out.print(ll.get(i) + " "); 159 } 160 } 161 }
------------------------------樹的Map實現dom
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.List; 4 import java.util.Map; 5 /** 6 * 利用Map實現樹型結構 1.能得到子節點 2.能獲取父節點 3.能獲取兄弟節點 4.能獲取祖先節點 5.能獲取子孫節點 7 */ 8 class MyTree { 9 private Map<String, String> map;// 基礎Map 10 private Map<String, List<String>> map2;// 爲了查找子節點方便創造的Map 11 public MyTree() { 12 this.map = new HashMap<String, String>(); 13 this.map2 = new HashMap<String, List<String>>(); 14 } 15 /** 16 * 添加數據到Map中 17 * 18 * @param child 19 * 子節點 20 * @param parent 21 * 父節點 22 */ 23 public void add(String child, String parent) { 24 this.map.put(child, parent); 25 List<String> list = this.map2.get(parent); 26 if (list == null) {// 若是list爲空,說明map2沒有parent這個key值 27 list = new ArrayList<String>(); 28 this.map2.put(parent, list); 29 } 30 list.add(child); 31 } 32 /** 33 * 經過傳遞child,獲得map中的元素 34 * 35 * @param child 36 * 子節點 37 * @return 父節點 38 */ 39 public String getParent(String child) { 40 return this.map.get(child); 41 } 42 /** 43 * 經過傳遞過來的父節點,獲得map2中的子節點集合 44 * 45 * @param parent 46 * 父節點 47 * @return 子節點集合 48 */ 49 public List<String> getChild(String parent) { 50 return this.map2.get(parent); 51 } 52 /** 53 * 經過傳遞的節點,先找到該節點的父節點,而後找到父節點的全部子節點集合,最後在集合中remove該節點 54 * @param node 節點 55 * @return 兄弟節點集合 56 */ 57 public List<String> getBrother(String node) { 58 if("root".equals(node)) return null; 59 String parent = this.getParent(node); 60 List<String> node2 = new ArrayList<String>(); 61 node2.add(node); 62 List<String> child = this.getChild(parent); 63 List<String> brother = new ArrayList<String>(child); 64 brother.removeAll(node2); 65 return brother; 66 } 67 /** 68 * 經過傳遞過來的子節點,迭代查找父節點,獲得這個子節點的全部祖先集合 69 * 70 * @param child 71 * 子節點 72 * @return 祖先集合 73 */ 74 public List<String> getAncestor(String child) { 75 // ancestor 祖先 76 List<String> list = new ArrayList<String>(); 77 String parent = getParent(child); 78 if (parent == null) { 79 return list; 80 } 81 list = getAncestor(parent); 82 list.add(parent); 83 return list; 84 } 85 /** 86 * 經過傳遞過來的父節點,迭代查找子節點,構成子節點集合 87 * 88 * @param parent 89 * 父節點 90 * @return 子孫節點集合 91 */ 92 public List<String> getDescendant(String parent) { 93 // descendant 子孫,後裔 94 List<String> list = new ArrayList<String>(); 95 List<String> child = getChild(parent); 96 if (child == null) 97 return list; 98 for (int i = 0; i < child.size(); i++) { 99 List<String> temp = getDescendant(child.get(i)); 100 list.add(child.get(i)); 101 list.addAll(temp); 102 } 103 return list; 104 } 105 } 106 public class Demo02 { 107 public static void main(String[] args) { 108 MyTree mt = new MyTree(); 109 mt.add("互聯網", "root"); 110 mt.add("百度", "互聯網"); 111 mt.add("阿里巴巴", "互聯網"); 112 mt.add("騰訊", "互聯網"); 113 mt.add("百度一下", "百度"); 114 mt.add("百度糯米", "百度"); 115 mt.add("百度雲", "百度"); 116 mt.add("淘寶", "阿里巴巴"); 117 mt.add("天貓", "阿里巴巴"); 118 mt.add("支付寶", "阿里巴巴"); 119 mt.add("QQ", "騰訊"); 120 mt.add("微信", "騰訊"); 121 mt.add("騰訊遊戲", "騰訊"); 122 mt.add("雲PC", "百度雲"); 123 mt.add("雲手機", "百度雲"); 124 mt.add("餘額寶", "支付寶"); 125 mt.add("天弘基金", "支付寶"); 126 mt.add("CF", "騰訊遊戲"); 127 mt.add("毒奶粉", "騰訊遊戲"); 128 mt.add("警察", "CF"); 129 mt.add("匪徒", "CF"); 130 mt.add("暴風騎兵", "毒奶粉"); 131 mt.add("黑暗帝君", "毒奶粉"); 132 mt.add("劍皇", "毒奶粉"); 133 mt.add("鏗鏘玫瑰", "毒奶粉"); 134 mt.add("復仇者", "毒奶粉"); 135 System.out.println("獲得毒奶粉的父節點:"); 136 System.out.println(mt.getParent("毒奶粉")); 137 System.out.println("----------------------------"); 138 System.out.println("獲得毒奶粉的子節點:"); 139 List<String> list = mt.getChild("毒奶粉"); 140 for (String string : list) { 141 System.out.println(string); 142 } 143 System.out.println("----------------------------"); 144 145 System.out.println("獲得毒奶粉的兄弟:"); 146 List<String> list2 = mt.getBrother("毒奶粉"); 147 for (String string : list2) { 148 System.out.println(string); 149 } 150 System.out.println("----------------------------"); 151 System.out.println("獲得毒奶粉的祖先:"); 152 List<String> list3 = mt.getAncestor("毒奶粉"); 153 for (String string : list3) { 154 System.out.println(string); 155 } 156 System.out.println("----------------------------"); 157 String str = "騰訊"; 158 System.out.println("獲得" + str + "的子孫:"); 159 // 首先獲得節點的全部子孫節點 160 List<String> list4 = mt.getDescendant(str); 161 // 獲得該節點的祖先深度 162 int length = mt.getAncestor(str).size(); 163 for (String string : list4) { 164 // 獲得每個節點的祖先深度,而後減去起始節點的祖先深度,for循環輸出--。構成樹型結構 165 List<String> temp = mt.getAncestor(string); 166 for (int i = 0; i < temp.size() - length; i++) { 167 System.out.print("--"); 168 } 169 System.out.println(string); 170 } 171 System.out.println("----------------------------"); 172 } 173 }
----------------------------------this