二叉树前序、中序、后序查询 和 二叉树删除(同时包含二叉树前中后序 遍历)

二叉树   前序查询   思路(中序和后续思路相似):java

首先,定义一个返回Node的方法,传送一个要查询的参数public Node preOrderSearch(int value){......}node

一、判断根节点的value是不是与要查询的value相等,若是是则返回——if(this.value == value){return this;}ide

二、若根节点判断不等,定义一个  类变量  用于判断左右子树是否查询到要查找的值,同时为了定义的方法返回一个值.Node resNode = null;post

三、判断左子数是否为空,不为空则递归调用左子树查找  若是左子树递归找到就将查找的结果 赋值 给 resNodethis

四、判断类变量resNode是否为空,不为空则表示左子树找到了,返回便可code

五、若左子树没找到而且resNode没有返回,说明 根节点和左子树都没有查询到要查询的值value。递归

判断右子树若不为空则递归查找,查找到就将结果 赋值 给resNodeget

六、若右子树找到要查找的值,则resNode会接受到返回便可;若根节点、左子树、右子树都没有找到要查询的值则直接返回定义的类变量便可(由于为类变量赋值为 null)class

 

 

二叉树  删除节点  思路:变量

首先,先判断根节点是否为空,为空则直接返回“二叉树为空,不能删除”;若不为空则判断根节点是不是要删除的节点,是则将根节点置空 root = null;不然就调用递归删除方法。

一、若左子树不为空 且 左子树的值等于要删除的值,则将左子树置为空,而后返回

二、若右子树不为空 且 右子树的值等于要删除的值,则将右子树置为空,而后返回

三、若第1和第2步都尚未删除值,则递归左子树删除

三、若第3步都尚未删除值,则递归右子树删除

 

实现代码:

package BinaryTree;
/*
 * 一、二叉树的前中后遍历
 * 二、二叉树的前中后查找
 * 三、二叉树的前中后删除
 */
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();	//调用前序遍历,输出 一、二、三、五、4
//		binaryTree.infixOrder();//中序遍历:输出二、一、五、三、四、
//		binaryTree.postOrder();//后序遍历:输出二、五、四、三、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();//删除前的前序遍历输出的结点为 一、二、三、五、4
		binaryTree.delOrder(3);//将二叉树相应结点删除
		binaryTree.preOrder();//删除二叉树相应节点后的前序遍历输出结果为 一、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) {			//一、若是根节点不为空
			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) {	//二、若左子节点不为空,且左子节点的no是要删除的,则将this.left = null置空
			this.left = null;
			return;
		}
		if(this.right != null & this.right.no == no) {	//三、若左子节点不为空,且左子节点的no是要删除的,则将this.left = null置空
			this.right = null;
			return;
		}
		if(this.left  != null) {						//四、若23步没删除,则左子树进行递归删除
			this.left.delNode(no);
		}
		if(this.right != null) {						//五、若4步没删除,则进行右子树删除
			this.right.delNode(no);
		}
	}
}
相关文章
相关标签/搜索