1、二叉树
二叉树的遍历:
代码实现:
package com.treedemo; public class BinaryTree { } //定义二叉树类 class BinaryTreeDemo { private Node root; public void setRoot(Node root) { this.root = root; } //前序遍历 public void preOrder() { if (this.root != null) { this.root.postOrder(); } 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 Node preOrderSearch(int no) { if (this.root != null) { return this.root.preOrderSearch(no); } else { return null; } } //中序遍历查找 public Node infixOrderSearch(int no) { if (this.root != null) { return this.root.infixOrderSearch(no); } else { return null; } } //后序遍历查找 public Node postOrderSearch(int no) { if (this.root != null) { return this.root.postOrderSearch(no); } else { return null; } } //删除节点 public void delNode(int no) { if (root != null) { if (root.no == no) { root = null; } else { root.delNode(no); } } else { System.out.println("空数,不能删除"); } } } class Node { public int no; public String name; public Node left; public Node right; public Node(int no, String name) { this.no = no; this.name = name; } @Override public String toString() { return "Node{" + "no=" + no + ", 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.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 Node preOrderSearch(int no) { if (this.no == no) { return this; } Node resNode = null;//临时变量,保存找到的node //递归查找左节点 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 Node infixOrderSearch(int no) { Node 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 Node postOrderSearch(int no) { Node 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; } //删除节点 /** * 如果删除的是叶子节点,则删除该节点 * 如果删除的是非叶子节点,则删除该子数 * * @param no */ public void delNode(int no) { //当前节点的左节点不为空,且就是要删除的节点,this.left= null if (this.left != null && this.left.no == no) { this.left = null; return; } //当前节点的右节点不为空,且就是要删除的节点,this.right= null 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); } } }