7.3.1 前序查找
- 先判断当前节点的no是否等于要查找的
- 如果是相等,则返回当前节点
- 如果不等,则判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
- 如果做地柜前序查找,找到节点,则返回,否继续判断,当前的节点的有自己额点是否为空,如果不空,则继续向右递归前序查找
public HeroNode preOrderSearch(int no) {
if (this.no == no) return this;
HeroNode resNode = null;
if (this.left != null) resNode = this.left.preOrderSearch(no);
if (resNode != null) return resNode;
if (this.right != null) resNode = this.right.preOrderSearch(no);
return resNode;
}
7.3.2 中序查找
- 判断当前节点的子节点是否为空,如果不为空,则递归中序查找
- 如果找到,则返回,如果没找到,就和当前节点对比,如果是则返回当前节点,否则继续右递归中序查找
- 如果油底柜中序查找,找到这返回,否则返回null
public HeroNode infixOrderSearch(int no) {
HeroNode resNode = null;
if(this.left != null) resNode = this.left.infixOrderSearch(no);
if(resNode != null) return resNode;
if(this.no == no) return this;
if(this.right != null) resNode = this.right.infixOrderSearch(no);
return resNode;
}
7.3.3 后续查找
- 判断当前节点左子节点是否为空如果不为空,则递归后续查找
- 如果找到,则返回,如果没有找到,就判断当前节点的右子节点是否为空,如果不为空,则向右递归进行后续查找,如果找到,则返回
- 就和当前节点进行,比如,如果是则返回,否则返回Null
public HeroNode postOrderSearch(int no) {
HeroNode resNode = null;
if (this.left != null) resNode = this.left.postOrderSearch(no);
if (resNode != null) return resNode;
if (this.right != null) resNode = this.right.postOrderSearch(no);
if (resNode != null) return resNode;
if (this.no == no) return this;
return resNode;
}