java--演算法--樹結構
阿新 • • 發佈:2021-07-13
- 數的基本介紹:
- 二叉樹的基本介紹:
-
二叉樹的遍歷:
-
package com.model.tree; /** * @Description:測試類 * @Author: 張紫韓 * @Crete 2021/7/13 18:54 * 演示二叉樹的遍歷 */ public class TreeDemo01 { public static void main(String[] args) { Node root = new Node(1, "張紫韓"); Node node2 = new Node(2, "張紫韓2"); Node node3
-
二叉樹的查詢:
-
package com.model.tree; /** * @Description:測試類 * @Author: 張紫韓 * @Crete 2021/7/13 18:54 * 演示二叉樹的遍歷 */ public class TreeDemo01 { public static void main(String[] args) { Node node1 = new Node(1, "張紫韓1"); Node node2 = new Node(2, "張紫韓2"); Node node3 = new Node(3, "張紫韓3"); Node node4 = new Node(4, "張紫韓4"); Node node5 = new Node(5, "張紫韓5"); node1.setLeft(node2); node1.setRight(node3); node3.setRight(node4); node3.setLeft(node5); BinaryTree tree = new BinaryTree(node1); // 三種遍歷 tree.perOrder(); tree.infixOrder(); tree.postOrder(); // 三種查詢 System.out.println("----------------"); System.out.println(tree.preFind(4)); System.out.println("----------------"); System.out.println(tree.infixFind(4)); System.out.println("----------------"); System.out.println(tree.postFind(5)); } } class BinaryTree { public Node root; public BinaryTree(Node root) { this.root = root; } // 遍歷這個樹 public void perOrder() { if (root != null) { root.preOrder(); } else { System.out.println("當前的樹為空"); } } public void infixOrder() { if (root != null) { root.infixOrder(); } else { System.out.println("當前的樹為空"); } } public void postOrder() { if (root != null) { root.postOrder(); } else { System.out.println("當前的樹為空"); } } public Node preFind(int id) { if (root != null) { return root.preFind(id); } else { System.out.println("該節點為空"); return null; } } public Node infixFind(int id) { if (root != null) { return root.infixFind(id); } else { System.out.println("該節點為空"); return null; } } public Node postFind(int id) { if (root != null) { return root.postFind(id); } else { System.out.println("該節點為空"); return null; } } } class Node { private int id; private String name; private Node left; private Node right; @Override public String toString() { return "Node{" + "id=" + id + ", name='" + name + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } public Node() { } public Node(int id, String name) { this.id = id; this.name = name; } // 先序遍歷 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.preOrder(); } } public void postOrder() { if (this.left != null) { this.left.postOrder(); } if (this.right != null) { this.right.postOrder(); } System.out.println(this); } // 前序查詢 public Node preFind(int id) { System.out.println("前序查詢進行了比較"); if (this.id == id) { return this; } Node temp = null; if (this.left != null) { temp = this.left.preFind(id); } if (temp != null) { return temp; } if (this.right != null) { temp = this.right.preFind(id); } return temp; } // 中序查詢 public Node infixFind(int id) { Node temp = null; if (this.left != null) { temp = this.left.infixFind(id); } if (temp != null) return temp; System.out.println("中序查詢進行比較了"); if (this.id == id) { return this; } if (this.right != null) { temp = this.right.infixFind(id); } return temp; } // 後序查詢 public Node postFind(int id) { Node temp = null; if (this.left != null) { temp = this.left.postFind(id); } if (temp != null) return temp; if (this.right != null) { temp = this.right.postFind(id); } if (temp != null) return temp; System.out.println("後序查詢進行比較了"); if (this.id == id) { return this; } return null; } }
-
二叉樹刪除節點:
-
// 遞迴刪除節點 public void delNode(int id) { if (this.left != null && this.left.id == id) { this.left = null; return; } if (this.right != null && this.right.id == id) { this.right = null; return; } if (this.left!=null){ this.left.delNode(id); } if (this.right!=null){ this.right.delNode(id); } }
// 刪除節點 public void delNode(int id){ if (root==null||root.getId()==id){ root=null; }else { root.delNode(id); } }
-
public void delNode01(int id) { if (this.left != null && this.left.id == id) { if (this.left.left!=null){ this.left = this.left.left; }else if (this.left.left==null&&this.left.right!=null){ this.left=this.left.right; }else{ this.left=null; } return; } if (this.right != null && this.right.id == id) { if (this.right.left!=null){ this.right = this.right.left; }else if (this.right.left==null&&this.right.right!=null){ this.right=this.right.right; }else{ this.right=null; } return; } if (this.left!=null){ this.left.delNode(id); } if (this.right!=null){ this.right.delNode(id); } }