二叉树 前序查询 思路(中序和后续思路类似):
首先,定义一个返回Node的方法,传送一个要查询的参数public Node preOrderSearch(int value){......}
1、判断根节点的value是否是与要查询的value相等,如果是则返回——if(this.value == value){return this;}
2、若根节点判断不等,定义一个 类变量 用于判断左右子树是否查询到要查找的值,同时为了定义的方法返回一个值.Node resNode = null;
3、判断左子数是否为空,不为空则递归调用左子树查找 如果左子树递归找到就将查找的结果 赋值 给 resNode
4、判断类变量resNode是否为空,不为空则表示左子树找到了,返回即可
5、若左子树没找到并且resNode没有返回,说明 根节点和左子树都没有查询到要查询的值value。
判断右子树若不为空则递归查找,查找到就将结果 赋值 给resNode
6、若右子树找到要查找的值,则resNode会接受到返回即可;若根节点、左子树、右子树都没有找到要查询的值则直接返回定义的类变量即可(因为为类变量赋值为 null)
二叉树 删除节点 思路:
首先,先判断根节点是否为空,为空则直接返回“二叉树为空,不能删除”;若不为空则判断根节点是否是要删除的节点,是则将根节点置空 root = null;否则就调用递归删除方法。
1、若左子树不为空 且 左子树的值等于要删除的值,则将左子树置为空,然后返回
2、若右子树不为空 且 右子树的值等于要删除的值,则将右子树置为空,然后返回
3、若第1和第2步都还没有删除值,则递归左子树删除
3、若第3步都还没有删除值,则递归右子树删除
实现代码:
package BinaryTree;
/*
* 1、二叉树的前中后遍历
* 2、二叉树的前中后查找
* 3、二叉树的前中后删除
*/
public class BinaryTreeDemo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree binaryTree = new BinaryTree();
HereNode root = new HereNode(1,"宋江");
HereNode node2 = new HereNode(2,"吴用");
HereNode node3 = new HereNode(3,"卢俊义");
HereNode node4 = new HereNode(4,"周德");
HereNode node5 = new HereNode(5,"林海宇");
//手动添加二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node5);
node3.setRight(node4);
binaryTree.setRoot(root);//设置父节点
//
// binaryTree.preOrder(); //调用前序遍历,输出 1、2、3、5、4
// binaryTree.infixOrder();//中序遍历:输出2、1、5、3、4、
// binaryTree.postOrder();//后序遍历:输出2、5、4、3、1
//
// //————————————————————————————————————————————————————————————————
// HereNode resNode = binaryTree.preOrderSearch(5);
// //HereNode resNdoe = new HereNode(); //需要调用HereNode中的查找方法,所以要实例化该方法
// //resNode = binaryTree.preOrderSearch(no); //调用Here中的前序查找,需要传一个要查找的参数
// if(resNode != null) {
// System.out.printf("找到了,信息为 no = %d name = %s", resNode.getNo() , resNode.getName());
// } else {
// System.out.printf("没找到,信息为 no = %d",5);
// }
//________________________________________________________________
binaryTree.preOrder();//删除前的前序遍历输出的结点为 1、2、3、5、4
binaryTree.delOrder(3);//将二叉树相应结点删除
binaryTree.preOrder();//删除二叉树相应节点后的前序遍历输出结果为 1、2
}
}
//创建二叉树
class BinaryTree{
private HereNode root; //创建父节点
public void setRoot(HereNode root2) { //获取传入的父节点
this.root = root2;
}
public void preOrder() { //若父节点不为空调用前序遍历
if(root != null) {
this.root.preOrder();
}
}
public void infixOrder() { //若父节点不为空调用前序遍历
if(root != null) {
this.root.infixOrder();
}
}
public void postOrder() { //若父节点不为空调用前序遍历
if(root != null) {
this.root.postOrder();
}
}
//——————————————————————————————————————————————————————————————————————
public HereNode preOrderSearch(int no) { //前序查找
if(root != null) {
return this.root.preOrderSearch(no);
} else {
return null;
}
}
public HereNode infixOrderSearch(int no) { //中序查找
if(root != null) {
return this.root.infixOrderSearch(no);
} else {
return null;
}
}
public HereNode postOrderSearch(int no) { //后序查找
if(root != null) {
return this.root.postOrderSearch(no);
} else {
return null;
}
}
//————————————————————————————————————————————————————————————————————
public void delOrder(int no) {
if(root != null) { //1、如果根节点不为空
if(root.getNo() == no) {//立刻判断根节点是否是要删除的节点
root = null;
} else { //如果根节点不是要删除的节点,则递归调用删除方法去判断左右子树
this.root.delNode(no);
}
} else {
System.out.println("二叉树为空,不能执行删除操作");
}
}
}
//创建HereNode结点
class HereNode{
private int no;
private String name;
private HereNode left; //默认为null
private HereNode right; //默认为null
public HereNode(int no, String name) {
this.no = no;
this.name = name;
}
//getset方法
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 HereNode getLeft() {
return left;
}
public void setLeft(HereNode left) {
this.left = left;
}
public HereNode getRight() {
return right;
}
public void setRight(HereNode right) {
this.right = right;
}
@Override
public String toString() { //重写toString方法
return "HereNode [no=" + no + ", name=" + name + "]";
}
//实现前中后序遍历______________________________________________________________________________
//前序遍历
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 HereNode preOrderSearch(int no) {
if(this.no == no) { //父节点判断
return this;
}
HereNode 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; //若最后都没找都就返回null
}
//中序查找
public HereNode infixOrderSearch(int no) {
if(this.no == no) { //父节点判断
return this;
}
HereNode resNode = null;
if(this.left != null) {
resNode = this.left.infixOrderSearch(no);
}
if(resNode != null) { //说明左子树找到了,返回即可
return resNode;
}
if(this.right != null) { //若左子树没有找到,则右子树继续查找
resNode = this.right.infixOrderSearch(no);
}
return resNode; //若最后都没找都就返回null
}
//后序查找
public HereNode postOrderSearch(int no) {
if(this.no == no) { //父节点判断
return this;
}
HereNode 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);
}
return resNode; //若最后都没找都就返回null
}
//实现 删除 ________________________________________________________________________________
public void delNode(int no) {
if(this.left != null && this.left.no == no) { //2、若左子节点不为空,且左子节点的no是要删除的,则将this.left = null置空
this.left = null;
return;
}
if(this.right != null & this.right.no == no) { //3、若左子节点不为空,且左子节点的no是要删除的,则将this.left = null置空
this.right = null;
return;
}
if(this.left != null) { //4、若23步没删除,则左子树进行递归删除
this.left.delNode(no);
}
if(this.right != null) { //5、若4步没删除,则进行右子树删除
this.right.delNode(no);
}
}
}
来源:oschina
链接:https://my.oschina.net/u/4432600/blog/4287015