构建一个二叉树
//二叉树
class BinaryTree {
private HeroNode root;
public void setRoot(HeroNode root) {
this.root = root;
}
public void delNode(int no){
if (root != null) {
if (root.getNo() == no) {
root = null;
}else {
root.delNode(no);
}
}else {
System.out.println("空树无法删除");
}
}
public void preOrder() {
if (this.root != null) {
this.root.preOrder();
} 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 HeroNode preOrderSearch(int no) {
if (root != null) {
return root.preOrderSearch(no);
} else {
return null;
}
}
//前序遍历查找
public HeroNode infixOrderSearch(int no) {
if (root != null) {
return root.infixOrderSearch(no);
} else {
return null;
}
}
//前序遍历查找
public HeroNode postOrderSearch(int no) {
if (root != null) {
return root.postOrderSearch(no);
} else {
return null;
}
}
}
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 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 + '\'' +
'}';
}
//递归删除节点,如果是叶子节点就删除该节点,如果是非叶子节点就删除该子树
public void delNode(int no) {
//思路
/**
* 因为二叉树是单向的,所以我们判断当前节点的子节点是否是需要删除的节点,
* 如果当前节点的左子节点不为空,并且左子节点就是要删除节点,就将左边置空
* 如果右子节点不为空,并且右子节点就是,将右子节点置空
* 如果上面都不是就向左子树递归,
* 右子树递归
*/
if (this.left != null && this.left.no == no) {
this.left = null;
return;
}
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);
}
}
//节点前序遍历
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.infixOrder();
}
}
//后序遍历
public void postOrder() {
if (this.left != null) {
this.left.postOrder();
}
if (this.right != null) {
this.right.postOrder();
}
System.out.println(this);
}
//前序遍历查找
public HeroNode preOrderSearch(int no) {
//比较当前节点是不是
if (this.no == no) {
return this;
}
HeroNode result = null;
if (this.left != null) {
result = this.left.preOrderSearch(no);
}
if (result != null) {
return result;
}
if (this.right != null) {
result = this.right.preOrderSearch(no);
}
return result;
}
//中序遍历查找
public HeroNode infixOrderSearch(int no) {
HeroNode result = null;
if (this.left != null) {
result = this.left.infixOrderSearch(no);
}
if (result != null) {
return result;
}
if (this.no == no) {
return this;
}
if (this.right != null) {
result = this.right.infixOrderSearch(no);
}
return result;
}
//后序查找
public HeroNode postOrderSearch(int no) {
HeroNode result = null;
if (this.left != null) {
result = this.left.postOrderSearch(no);
}
if (result != null) {
return result;
}
if (this.right != null) {
result = this.right.postOrderSearch(no);
}
if (result != null) {
return result;
}
//左右子树都没有找到,就比较当前节点是不是
if (this.no == no) {
return this;
}
return result;
}
}