遍历二叉树(前序/中序/后续)
1.前序:父节点-->左子节点-->右子节点
2.中序:左子节点-->父节点-->右子节点
3.后续:左子节点-->右子节点-->父节点
注:关键看父节点的顺序
遍历思路:都是从根节点开始,并使用递归
前序:1.先返回当前节点
2.判断左子节点是否为空,不为空则递归前序遍历
3.判断右子节点是否为空,不为空则递归前序遍历
中序:1.判断当前节点的左子节点是否为空,不为空则递归前序遍历
2.返回当前节点
3.判断右子节点是否为空,不为空则递归前序遍历
后序:1.判断当前节点的左子节点是否为空,不为空则递归前序遍历
2.判断当前节点的右子节点是否为空,不为空则递归前序遍历
3.返回当前节点
图示
前序遍历
中序
后序
Java代码
//创建二叉树
class BinaryTree {
private Family root;//二叉树从根节点出发 只需要根节点
public void setRoot(Family root) {
this.root = root;
}
//前序遍历
public void preOrder() {
if (this.root != null) {
System.out.println("前序遍历=>");
this.root.preOrder();
} else {
System.out.println("树为空");
return;
}
}
//中序遍历
public void midOrder() {
if (this.root != null) {
System.out.println("中序遍历=>");
this.root.midOrder();
} else {
System.out.println("树为空");
return;
}
}
//后序遍历
public void postOrder() {
if (this.root != null) {
System.out.println("后序遍历=>");
this.root.postOrder();
} else {
System.out.println("树为空");
return;
}
}
//前序查找
public void preFind(int no) {
Family resFamily = this.root.preFind(no);
if (resFamily != null) {
System.out.printf("【no=%d,name=%s】", resFamily.getNo(), resFamily.getName());
} else {
System.out.println("查无此人");
}
}
//中序查找
public void midFind(int no) {
Family resFamily = this.root.midFind(no);
if (resFamily != null) {
System.out.printf("【no=%d,name=%s】", resFamily.getNo(), resFamily.getName());
} else {
System.out.println("查无此人");
}
}
//后序查找
public void postFind(int no) {
Family resFamily = this.root.postFind(no);
if (resFamily != null) {
System.out.printf("【no=%d,name=%s】", resFamily.getNo(), resFamily.getName());
} else {
System.out.println("查无此人");
}
}
//删除一个节点,树形不能进行自删除,如果整棵树只有root,并且root即为目标 则整棵树置空
public void delFamilyByNo(int no) {
if (this.root == null) {
System.out.println("树为空");
return;
}
if (this.root.getNo() == no) {
this.root = null;
return;
}
this.root.delFamilyByNo(no);
}
}
class Family {
private int no;
private String name;
private Family left;//左子树
private Family right;//右子树
//初始化左右子树默认为null
public Family(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 Family getLeft() {
return left;
}
public void setLeft(Family left) {
this.left = left;
}
public Family getRight() {
return right;
}
public void setRight(Family right) {
this.right = right;
}
//前序遍历
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 Family preFind(int no) {
if (this.no == no) {
return this;
}
Family resFamily = null;
if (this.left != null) {
resFamily = this.left.preFind(no);
}
if (resFamily != null) {
return resFamily;
}
if (this.right != null) {
resFamily = this.right.preFind(no);
}
return resFamily;
}
//中序查找
public Family midFind(int no) {
Family resFamily = null;
if (this.left != null) {
resFamily = this.left.preFind(no);
}
if (resFamily != null) {
return resFamily;
}
if (this.no == no) {
return this;
}
if (this.right != null) {
resFamily = this.right.preFind(no);
}
return resFamily;
}
//后序查找
public Family postFind(int no) {
Family resFamily = null;
if (this.left != null) {
resFamily = this.left.preFind(no);
}
if (resFamily != null) {
return resFamily;
}
if (this.right != null) {
resFamily = this.right.preFind(no);
}
if (this.no == no) {
return this;
}
return resFamily;
}
//删除
//树形不能进行自删除,只能删除当前节点的子节点
public void delFamilyByNo(int no) {
//判断左子节点,如果满足就把左子节点=null,并return结束递归
if (this.left != null && this.left.no == no) {
this.left = null;
return;
}
//判断右子节点,如果满足就把右子节点=null,并return结束递归
if (this.right != null && this.right.no == no) {
this.right = null;
return;
}
//递归左子节点删除
if (this.left != null) {
this.left.delFamilyByNo(no);
}
//递归右子节点删除
if (this.right != null) {
this.right.delFamilyByNo(no);
}
}
@Override
public String toString() {
return "Family [no=" + this.no + ",name=" + this.name + "]";
}
}