1.说明
这里删除某个节点时,不考虑规则,删除以这个节点为跟的整个子树
2.代码实现
1.节点
public class BinaryTreeNodes {
private int id;
private String name;
private BinaryTreeNodes left;
private BinaryTreeNodes right;
public BinaryTreeNodes() {
}
public BinaryTreeNodes(int id, String name) {
this.id = id;
this.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 BinaryTreeNodes getLeft() {
return left;
}
public void setLeft(BinaryTreeNodes left) {
this.left = left;
}
public BinaryTreeNodes getRight() {
return right;
}
public void setRight(BinaryTreeNodes right) {
this.right = right;
}
@Override
public String toString() {
return "BinaryTreeNodes{" +
"id=" + id +
", 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 MidOrder() {
if (this.left != null) {
this.left.MidOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.MidOrder();
}
}
public void PostOrder() {
if (this.left != null) {
this.left.PostOrder();
}
if (this.right != null) {
this.right.PostOrder();
}
System.out.println(this);
}
public void DelNodes(int id){
if(this.left != null && this.left.id == id){
this.left = null;
}
if(this.right != null && this.right.id == id){
this.right = null;
}
if(this.left != null){
this.left.DelNodes(id);
}
if(this.right != null){
this.right.DelNodes(id);
}
}
public BinaryTreeNodes PreOrderSearch(int id) {
if (this.id == id) {
return this;
}
BinaryTreeNodes resNodes = null;
if (this.left != null) {
resNodes = this.left.PreOrderSearch(id);
}
if (resNodes != null) {
return resNodes;
}
if (this.right != null) {
resNodes = this.right.PreOrderSearch(id);
}
return resNodes;
}
public BinaryTreeNodes MidOrderSearch(int id) {
BinaryTreeNodes resNodes = null;
if (this.left != null) {
resNodes = this.left.MidOrderSearch(id);
}
if (resNodes != null) {
return resNodes;
}
if (this.id == id) {
return this;
}
if (this.right != null) {
resNodes = this.right.MidOrderSearch(id);
}
return resNodes;
}
public BinaryTreeNodes PostOrderSearch(int id) {
BinaryTreeNodes resNodes = null;
if (this.left != null) {
resNodes = this.left.PostOrderSearch(id);
}
if (resNodes != null) {
return resNodes;
}
if (this.right != null) {
resNodes = this.right.PostOrderSearch(id);
}
if (resNodes != null) {
return resNodes;
}
if (this.id == id) {
return this;
}
return resNodes;
}
}
2.二叉树
public class BinaryTree {
public BinaryTreeNodes root;
public BinaryTree() {
}
public BinaryTree(BinaryTreeNodes root) {
this.root = root;
}
public void setRoot(BinaryTreeNodes root) {
this.root = root;
}
public void PreOrderTraverse() {
if (this.root != null) {
this.root.PreOrder();
} else {
System.out.println("二叉树为空,无法先序遍历");
}
}
public void MidOrderTraverse() {
if (this.root != null) {
this.root.MidOrder();
} else {
System.out.println("二叉树为空,无法中序遍历");
}
}
public void PostOrderTraverse() {
if (this.root != null) {
this.root.PostOrder();
} else {
System.out.println("二叉树为空,无法后序遍历");
}
}
public void DelNode(int id) {
if (root != null) {
if (root.getId() == id) {
root = null;
}else {
root.DelNodes(id);
}
} else {
System.out.println("树为空,无法删除");
}
}
public BinaryTreeNodes PreOrderSearch(int id) {
if (root != null) {
return root.PreOrderSearch(id);
} else {
return null;
}
}
public BinaryTreeNodes MidOrderSearch(int id) {
if (root != null) {
return root.MidOrderSearch(id);
} else {
return null;
}
}
public BinaryTreeNodes PostOrderSearch(int id) {
if (root != null) {
return root.PostOrderSearch(id);
} else {
return null;
}
}
}
3.入口
package binaryTreeTraverse;
public class input {
public static void main(String[] args) {
BinaryTree Tree = new BinaryTree();
BinaryTreeNodes root = new BinaryTreeNodes(1, "宋江");
BinaryTreeNodes N1 = new BinaryTreeNodes(2, "林冲");
BinaryTreeNodes N2 = new BinaryTreeNodes(3, "鲁智深");
BinaryTreeNodes N3 = new BinaryTreeNodes(4, "武松");
BinaryTreeNodes N4 = new BinaryTreeNodes(5, "李逵");
Tree.setRoot(root);
root.setLeft(N1);
root.setRight(N2);
N1.setLeft(N3);
N1.setRight(N4);
System.out.println("先序遍历的结果:");
Tree.PreOrderTraverse();
Tree.DelNode(3);
System.out.println("删除后:");
Tree.PreOrderTraverse();
}
}