依据二叉树的遍历方式,查找二叉树中的指定的节点,也有三种方式:
按照前序遍历的顺序查找:
正确代码:
public Node preOrderSearch(int num) {
System.out.println("当前的节点数值为:" + this.num);
Node res = null;
if (this.num == num) {
return this;
}
if (this.left != null) {
res = this.left.preOrderSearch(num);
}
if (res != null) {
return res;
}
if (this.right != null) {
res = this.right.preOrderSearch(num);
}
return res;
}
在刚开始的时候,我认为没必要设置一个res的变量记录this.left.preOrderSearch(num)或者this.right.preOrderSearch(num)的值,思路和前序遍历的一样,只要判断该节点的值是不是要找的那个值即可,但是在运行代码以后,发现了问题。代码中构建的二叉树如下图所示。
(个人觉得:弄清楚递归程序最好的方式就是debug)
假设,要找到值为5的节点
递归程序的运行是这样的:【如果看不下去,可以直接设断点调试】
节点1,判断1和5的值相不相等,结果是不相等,判断左节点不为空,则进入左节点,左节点2与5不相等,则判断节点2有无左节点,节点2没有左节点,这时res=null,判断节点2有没有右节点,节点2没有右节点,所以回退到节点1判断左节点的时候,res的值为null,接着判断节点1的右节点是否为空,右节点不为空,进入右节点3,3不等于5,则继续判断节点3的左节点,节点3的左节点是5,这时,节点5就是我们要找的节点,则会把该节点返回,注意,返回到的节点是节点3的左节点判断语句,这时res被赋值为节点5,跳出if,进入判断res是否为空if语句,这时返回res,返回的值是到了节点1的右节点判断语句,res被赋值为节点5,这时节点1的左节点和右节点都判断完毕了,返回res,就可以输出了。
中序遍历和后序遍历也都是一样的理解方式。
单从要查找节点5这个具体的例子来说,后续遍历能够最快找到。
按照中序遍历的顺序查找
public Node infixOrderSearch(int num) {
Node res = null;
if (this.left != null) {
res = this.left.infixOrderSearch(num);
}
if (res != null) {
return res;
}
System.out.println("当前的节点数值为:" + this.num);
if (this.num == num) {
return this;
}
if (this.right != null) {
res = this.right.infixOrderSearch(num);
}
return res;
}
按照后序遍历的顺序查找
public Node postOrderSearch(int num) {
Node res = null;
if (this.left != null) {
res = this.left.postOrderSearch(num);
}
if (res != null) {
return res;
}
if (this.right != null) {
res = this.right.postOrderSearch(num);
}
if (res != null) {
return res;
}
System.out.println("当前的节点数值为:" + this.num);
if (this.num == num) {
return this;
}
return res;
}
完整的代码(可直接运行):
package tree;
public class BinaryTreeDemo {
public static void main(String[] args) {
//创建一棵二叉树
BinaryTree binaryTree = new BinaryTree();
//创建需要的节点
Node root = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3);
Node n4 = new Node(4);
//新增加的节点
Node n5 = new Node(5);
//暂时手动创建二叉树
root.setLeft(n2);
root.setRight(n3);
n3.setRight(n4);
n3.setLeft(n5);
//测试
//前序遍历 :1 2 3 4
//添加节点之后 : 1 2 3 5 4
System.out.println("前序遍历");
binaryTree.setRoot(root);
binaryTree.preOrder();
//中序遍历 :2 1 3 4
//添加节点之后 :2 1 5 3 4
System.out.println("中序遍历");
binaryTree.infixOrder();
//后序遍历 2 4 3 1
//添加节点之后 :2 5 4 3 1
System.out.println("后序遍历");
binaryTree.postOrder();
//前序遍历查找
System.out.println("前序遍历查找:");
System.out.println(binaryTree.preOrderSearch(5));
// System.out.println("中序遍历查找:");
// System.out.println(binaryTree.infixOrderSearch(5));
// System.out.println("后序遍历查找:");
// System.out.println(binaryTree.postOrderSearch(5));
}
}
class BinaryTree {
private Node root;
public void setRoot(Node root) {
this.root = root;
}
//前序遍历
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 Node preOrderSearch(int num) {
if (root != null) {
return root.preOrderSearch(num);
}
return null;
}
public Node infixOrderSearch(int num) {
if (root != null) {
return root.infixOrderSearch(num);
}
return null;
}
public Node postOrderSearch(int num) {
if (root != null) {
return root.postOrderSearch(num);
}
return null;
}
}
class Node {
private int num;
private Node left;
private Node right;
public Node(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node{" +
"num=" + num +
'}';
}
//前序遍历
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 Node preOrderSearch(int num) {
System.out.println("当前的节点数值为:" + this.num);
Node res = null;
if (this.num == num) {
return this;
}
if (this.left != null) {
res = this.left.preOrderSearch(num);
}
if (res != null) {
return res;
}
if (this.right != null) {
res = this.right.preOrderSearch(num);
}
return res;
}
//中序遍历查找指定的节点
public Node infixOrderSearch(int num) {
Node res = null;
if (this.left != null) {
res = this.left.infixOrderSearch(num);
}
if (res != null) {
return res;
}
System.out.println("当前的节点数值为:" + this.num);
if (this.num == num) {
return this;
}
if (this.right != null) {
res = this.right.infixOrderSearch(num);
}
return res;
}
//后序遍历查找指定的节点
public Node postOrderSearch(int num) {
Node res = null;
if (this.left != null) {
res = this.left.postOrderSearch(num);
}
if (res != null) {
return res;
}
if (this.right != null) {
res = this.right.postOrderSearch(num);
}
if (res != null) {
return res;
}
System.out.println("当前的节点数值为:" + this.num);
if (this.num == num) {
return this;
}
return res;
}
}
运行部分结果:
前序遍历查找:
当前的节点数值为:1
当前的节点数值为:2
当前的节点数值为:3
当前的节点数值为:5
Node{num=5}