二叉樹前序、中序、後序查詢 和 二叉樹刪除(同時包含二叉樹前中後序 遍歷)

二叉樹   前序查詢   思路(中序和後續思路相似):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);
		}
	}
}
相關文章
相關標籤/搜索