标签:Node right java 树结构 -- public null id left
- 数的基本介绍:
- 二叉树的基本介绍:
-
-
二叉树的遍历:
-
-
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); } }
标签:Node,right,java,树结构,--,public,null,id,left 来源: https://www.cnblogs.com/zzhAylm/p/15005738.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。