Java--算法--树结构
- 数的基本介绍:
- 二叉树的基本介绍:
-
-
二叉树的遍历:
-
-
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 = new Node(3, "张紫韩3"); Node node4 = new Node(4, "张紫韩4"); root.setLeft(node2); root.setRight(node3); root.getRight().setRight(node4); BinaryTree tree = new BinaryTree(root); tree.perOrder(); tree.infixOrder(); tree.postOrder(); // System.out.println(tree.preFind(node2)); } } 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(Node node){ return root.preFind(node); } } 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(Node node){ if (this.id==node.id){ return this; } if (this.left!=null){ return this.left.preFind(node); } if (this.right!=null){ return this.right.preFind(node); } return null; } }
-
二叉树的查找:
-
-
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); } }