一、思路
- 遍历
- 删除
①. 因为我们的二叉树是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点.
②. 如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将this.left = null; 并且就返回(结束递归删除)
③. 如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将this.right= null ;并且就返回(结束递归删除)
④. 如果第2和第3步没有删除结点,那么我们就需要向左子树进行递归删除
⑤. 如果第4步也没有删除结点,则应当向右子树进行递归删除.二、总结
三、代码
package com.tree;
/**
* @Description 二叉树
* @Author Meng
* @Versions
* @Date 2021-07-22-14:23
*/
public class BinaryTreeDome {
public static void main(String[] args) {
//先需要创建一颗二叉树
BinaryTree binaryTree = new BinaryTree();
//创建需要的结点
HeroNode root = new HeroNode(1, "宋江");
HeroNode node2 = new HeroNode(2, "吴用");
HeroNode node3 = new HeroNode(3, "卢俊义");
HeroNode node4 = new HeroNode(4, "林冲");
HeroNode node5 = new HeroNode(5, "关胜");
//说明,我们先手动创建该二叉树,后面我们学习递归的方式创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
// 将根节点赋给binaryTree的root
binaryTree.setRoot(root);
// 前序遍历
// binaryTree.prefixOrder();
// 中序遍历
// binaryTree.infixOrder();
// 后序遍历
binaryTree.suffixOrder();
// 前序遍历查找
// binaryTree.prefixOrderFind(5);
// 中序遍历查找
// binaryTree.infixOrderFind(5);
// 后序遍历查找
// binaryTree.suffixOrderFind(8);
/*System.out.println("删除前。。。。");
binaryTree.prefixOrder();
binaryTree.delNode(4);
System.out.println("删除后。。。。");
binaryTree.prefixOrder();*/
}
}
class BinaryTree{
private HeroNode root;
public HeroNode getRoot() {
return root;
}
public void setRoot(HeroNode root) {
this.root = root;
}
/**
* 删除节点
*/
public void delNode(int no){
if (root.getNo() == no){
root = null;
return;
}else{
root.delNode(no);
}
}
/**
* 前序遍历
*/
public void prefixOrder(){
root.prefixOrder();
}
/**
* 中序遍历
*/
public void infixOrder(){
root.infixOrder();
}
/**
* 后序遍历
*/
public void suffixOrder(){
root.suffixOrder();
}
/**
* 前序遍历查找
*/
public void prefixOrderFind(int no){
root.prefixOrderFind(no);
}
/**
* 中序遍历查找
*/
public void infixOrderFind(int no){
root.infixOrderFind(no);
}
/**
* 后序遍历查找
*/
public void suffixOrderFind(int no){
root.suffixOrderFind(no);
}
}
class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
public void delNode(int id){
/*
1. 因为我们的二叉树是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点.
2. 如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将this.left = null; 并且就返回(结束递归删除)
3. 如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将this.right= null ;并且就返回(结束递归删除)
4. 如果第2和第3步没有删除结点,那么我们就需要向左子树进行递归删除
5. 如果第4步也没有删除结点,则应当向右子树进行递归删除.*/
if (this.left != null && this.left.no == id){
this.left = null;
return;
}
if (this.right != null && this.right.no == id){
this.right = null;
return;
}
if (this.left != null) {
if (this.left.no != id){
this.left.delNode(id);
}
}
if (this.right != null) {
if (this.right.no != id){
this.right.delNode(id);
}
}
}
/**
* 前序遍历查找
*/
public void prefixOrderFind(int id){
System.out.println("查找~~~~~");
// 判断根节点的no是否相等
if (this.getNo() == id){
System.out.println(this);
return;
}
// 先判断节点的左子树是否为null
if (this.left != null){
// 若不为空在判断no是否相等
if (this.left.getNo() != id){
// 若不相等则继续遍历
this.left.prefixOrderFind(id);
}else {
System.out.println(this.left);
return;
}
}
// 先判断节点的右子树是否为null
if (this.right != null){
// 若不为空在判断no是否相等
if (this.right.getNo() != id){
// 若不相等则继续遍历
this.right.prefixOrderFind(id);
}else {
System.out.println(this.right);
return;
}
}
}
/**
* 中序遍历查找
*/
public void infixOrderFind(int no){
System.out.println("查找~~~~~");
// 先判断节点的左子树是否为null
if (this.left != null){
// 若不为空在判断no是否相等
if (this.left.no != no){
// 若不相等则继续遍历
this.left.infixOrderFind(no);
}else {
System.out.println(this.left);
return;
}
}
// 判断根节点的no是否相等
if (this.no == no){
System.out.println(this);
return;
}
// 先判断节点的右子树是否为null
if (this.right != null){
// 若不为空在判断no是否相等
if (this.right.no != no){
// 若不相等则继续遍历
this.right.infixOrderFind(no);
}else {
System.out.println(this.right);
return;
}
}
}
/**
* 后序遍历查找
*/
public void suffixOrderFind(int no){
System.out.println("查找~~~~~");
// 先判断节点的左子树是否为null
if (this.left != null){
// 若不为空在判断no是否相等
if (this.left.no != no){
// 若不相等则继续遍历
this.left.suffixOrderFind(no);
}else {
System.out.println(this.left);
return;
}
}
// 先判断节点的右子树是否为null
if (this.right != null){
// 若不为空在判断no是否相等
if (this.right.no != no){
// 若不相等则继续遍历
this.right.suffixOrderFind(no);
}else {
System.out.println(this.right);
return;
}
}
// 判断根节点的no是否相等
if (this.no == no){
System.out.println(this);
return;
}
}
/**
* 前序遍历
*/
public void prefixOrder(){
if (this != null){
System.out.println(this);
}
if (this.left != null){
this.left.prefixOrder();
}
if (this.right != null){
this.right.prefixOrder();
}
}
/**
* 中序遍历
*/
public void infixOrder(){
if (this.left != null){
this.left.infixOrder();
}
if (this != null){
System.out.println(this);
}
if (this.right != null){
this.right.infixOrder();
}
}
/**
* 后序遍历
*/
public void suffixOrder(){
if (this.left != null){
this.left.suffixOrder();
}
if (this.right != null){
this.right.suffixOrder();
}
if (this != null){
System.out.println(this);
}
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
}