博客說明java
文章所涉及的資料來自互聯網整理和我的總結,意在於我的學習和經驗彙總,若有什麼地方侵權,請聯繫本人刪除,謝謝!node
能提升數據存儲,讀取的效率, 好比利用 二叉排序樹(Binary Sort Tree),既能夠保證數據的檢索速度,同時也能夠保證數據的插入,刪除,修改的速度網絡
每一個節點最多隻能有兩個子節點的一種形式稱爲二叉樹數據結構
二叉樹的子節點分爲左節點和右節點ide
若是該二叉樹的全部葉子節點都在最後一層,而且結點總數= 2^n -1 , n 爲層數,則咱們稱爲滿二叉樹。post
若是該二叉樹的全部葉子節點都在最後一層或者倒數第二層,並且最後一層的葉子節點在左邊連續,倒數第二層的葉子節點在右邊連續,咱們稱爲徹底二叉樹學習
package cn.guizimo.tree; /** * @author guizimo * @date 2020/7/29 8:03 下午 */ public class TreeDemo { public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(); HeroNode root = new HeroNode(1, "宋江"); HeroNode node2 = new HeroNode(2, "李逵"); HeroNode node3 = new HeroNode(3, "盧俊義"); HeroNode node4 = new HeroNode(4, "吳用"); HeroNode node5 = new HeroNode(5, "林沖"); HeroNode node6 = new HeroNode(6, "魯智深"); //建立二叉樹 root.setLeft(node2); root.setRight(node3); node2.setLeft(node4); node3.setLeft(node5); node3.setRight(node6); binaryTree.setRoot(root); //前序遍歷 // HeroNode heroNode = binaryTree.preOrderSearch(5); // System.out.println(heroNode); } } /** * 二叉樹 */ class BinaryTree { //根節點 private HeroNode root; public void setRoot(HeroNode root) { this.root = root; } //刪除二叉樹的節點 public void delNode(int no) { if (root != null) { if (root.getNo() == no) { root = null; } else { root.delNode(no); } } else { System.out.println("二叉樹爲空"); } } //前序 public void preOrder() { if (this.root != null) { this.root.preOrder(); } else { System.out.println("二叉樹爲空"); } } //中序 public void infixOrder() { if (this.root != null) { this.root.infixOrder(); } else { System.out.println("二叉樹爲空"); } } //後序 public void postOrder() { if (this.root != null) { this.root.postOrder(); } else { System.out.println("二叉樹爲空"); } } //前序查找 public HeroNode preOrderSearch(int no) { if (root != null) { return root.preOrderSearch(no); } else { return null; } } //中序查找 public HeroNode infixOrderSearch(int no) { if (root != null) { return root.infixOrderSearch(no); } else { return null; } } //後序查找 public HeroNode postOrderSearch(int no) { if (root != null) { return this.root.postOrderSearch(no); } else { return null; } } } /** * 節點 */ class HeroNode { private int no; private String name; private HeroNode left; private HeroNode right; public HeroNode(int no, String name) { this.no = no; this.name = name; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public HeroNode getLeft() { return left; } public void setLeft(HeroNode left) { this.left = left; } public HeroNode getRight() { return right; } public void setRight(HeroNode right) { this.right = right; } @Override public String toString() { return "HeroNode{" + "no=" + no + ", name='" + name + '\'' + '}'; } //刪除節點 public void delNode(int no) { //判讀左節點是否爲空,找到 if (this.left != null && this.left.no == no) { this.left = null; return; } //判斷右節點,找到 if (this.right != null && this.right.no == no) { this.right = null; return; } //判斷左節點,未找到,遞歸 if (this.left != null) { this.left.delNode(no); } //判斷右節點,未找到,遞歸 if (this.right != null) { this.right.delNode(no); } } //前序 public void preOrder() { System.out.println(this); if (this.left != null) { this.left.preOrder(); } if (this.right != null) { this.right.preOrder(); } } //中序 public void infixOrder() { if (this.left != null) { this.left.infixOrder(); } System.out.println(this); if (this.right != null) { this.right.infixOrder(); } } //後序 public void postOrder() { if (this.left != null) { this.left.postOrder(); } if (this.right != null) { this.right.postOrder(); } System.out.println(this); } //前序遍歷查找 public HeroNode preOrderSearch(int no) { if (this.no == no) { return this; } HeroNode resNode = null; //判斷左子樹 if (this.left != null) { resNode = this.left.preOrderSearch(no); } if (resNode != null) { return resNode; } //判斷右子樹 if (this.right != null) { resNode = this.right.preOrderSearch(no); } return resNode; } //中序遍歷查找 public HeroNode infixOrderSearch(int no) { HeroNode resNode = null; if (this.left != null) { resNode = this.left.infixOrderSearch(no); } if (resNode != null) { return resNode; } if (this.no == no) { return this; } if (this.right != null) { resNode = this.right.infixOrderSearch(no); } return resNode; } //後序遍歷查找 public HeroNode postOrderSearch(int no) { HeroNode resNode = null; if (this.left != null) { resNode = this.left.postOrderSearch(no); } if (resNode != null) { return resNode; } if (this.right != null) { resNode = this.right.postOrderSearch(no); } if (resNode != null) { return resNode; } if (this.no == no) { return this; } return resNode; } }
感謝ui
尚硅谷this
萬能的網絡3d
以及勤勞的本身
關注公衆號: 歸子莫,獲取更多的資料,還有更長的學習計劃