二叉樹基本操作—Java實現

package muyanmoyang.data.BTree;

/**
 * 二叉樹的節點
 * @author hadoop
 *
 */
class BtNode {
	String data ;
	BtNode lchild ;
	BtNode rchild ;
	public BtNode(String data, BtNode lchild, BtNode rchild) {
		super();
		this.data = data;
		this.lchild = lchild;
		this.rchild = rchild;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
	public BtNode getLchild() {
		return lchild;
	}
	public void setLchild(BtNode lchild) {
		this.lchild = lchild;
	}
	public BtNode getRchild() {
		return rchild;
	}
	public void setRchild(BtNode rchild) {
		this.rchild = rchild;
	}
}
<pre name="code" class="java">package muyanmoyang.data.BTree;

import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Stack;

public class BTree {
	static BtNode root = initTree() ; 
	public static void main(String[] args) {
		//初始化
		BtNode bt = initTree() ;
		//先序遍歷二叉樹
		System.out.print("\n先序遍歷:");
		preOrder(bt) ;                  // 先序遍歷
		System.out.print("\n先序遍歷(非遞歸):");
		preOrderWithoutRecursion(bt) ;  // 先序遍歷(非遞歸)
		System.out.print("\n中序遍歷:");
		inOrderWithoutRecursion(bt);    // 中序遍歷
		System.out.print("\n後序遍歷:");
		postOrder(bt) ;					// 後序遍歷
		System.out.print("\n層次遍歷:");
		levelOrder(bt) ;                // 層次遍歷
		System.out.print("\n二叉樹的深度:" + getDepthOfBTree(bt)); 
		System.out.print("\n二叉樹的節點個數:" + getNumOfNodes(bt));

		// 求指定節點的parent節點
		System.out.print("\n查詢雙親節點,請輸入以下節點中的一個: ");
		levelOrder(bt) ;
		System.out.println();
		try{
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String str = br.readLine() ;
			BtNode parent = parent(str);
			System.out.println("所求雙親爲:" + parent.data);
		}catch(NullPointerException e){
			System.out.println("沒有這個節點");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace(); 
		}
		
		// 查找指定的節點
		BtNode findNode = findNode(bt,"G") ;
		BtNode parent2 = parent(findNode.data);
		System.out.println("所求雙親爲:" + parent2.data);
	}
	/*
	 * 初始化樹
	 */
	private static BtNode initTree(){
		BtNode aBtNode = new BtNode("A", null, null);
		BtNode bBtNode = new BtNode("B", null, null);
		BtNode cBtNode = new BtNode("C", null, null);
		BtNode dBtNode = new BtNode("D", null, null);
		BtNode eBtNode = new BtNode("E", null, null);
		BtNode fBtNode = new BtNode("F", null, null);
		BtNode gBtNode = new BtNode("G", null, null);
		BtNode hBtNode = new BtNode("H", null, null);
		
		aBtNode.setLchild(bBtNode);
		aBtNode.setRchild(cBtNode);
		bBtNode.setLchild(dBtNode);
		bBtNode.setRchild(eBtNode);
		cBtNode.setLchild(fBtNode);
		cBtNode.setRchild(gBtNode);
		fBtNode.setRchild(hBtNode); 
		return  aBtNode ;
	}
	/*
	 *  先序遍歷
	 */
	private static void preOrder(BtNode bt){
		if(bt == null){
			return ; 
		}else{
			System.out.print(bt.data + "、") ;
			if(bt.lchild != null){
				preOrder(bt.lchild) ;
			}
			if(bt.rchild != null){
				preOrder(bt.rchild) ;
			}
		}
	}
	
	/*
	 *  先序遍歷 (非遞歸)
	 */
	private static void preOrderWithoutRecursion(BtNode bt){
		if(bt == null){
			return ;
		}
		Stack<BtNode> stack = new Stack<BtNode>() ;
		BtNode p = bt ;
		while(p!=null || !stack.isEmpty()){
			if(p!=null){
				System.out.print(p.data + "、");
				stack.push(p) ;
				p = p.lchild ;
			}else{
				p = stack.pop() ;
				p = p.rchild ;
			}
		}
	}
	/*
	 *  中序遍歷 (非遞歸實現)
	 */
	private static void inOrderWithoutRecursion(BtNode bt){
		if(bt == null){
			return ;
		}
		Stack<BtNode> stack = new Stack<BtNode>() ;
		BtNode p = bt ;
		while(p!=null || !stack.isEmpty()){
			if(p != null){
				stack.push(p) ;
				p = p.lchild ;
			}else
			{
				p = stack.pop() ;
				System.out.print(p.data + "、");
				p = p.rchild ;
			}
		}
	}
	/*
	 *  後序遍歷 (雙棧實現)
	 */
	private static void postOrder(BtNode bt){
		if(bt == null){
			return ;
		}
		Stack<BtNode> stack = new Stack<BtNode>() ;
		Stack<BtNode> out = new Stack<BtNode>() ;
		BtNode p = bt ;
		while(p!=null || !stack.isEmpty()){
			if(p != null){
				stack.push(p); 
				out.push(p);
				p = p.rchild ;
			}else{
				p = stack.pop().lchild ;
			}
		}
		while (!out.isEmpty()) {
			p = out.pop() ;
			System.out.print(p.data + "、");	
		}
	}
	
	/*
	 * 層次遍歷
	 */
	private static void levelOrder(BtNode bt){
		if(bt == null){
			return ;
		}
		LinkedList queue = new LinkedList() ;
		queue.add(bt) ;
		while(!queue.isEmpty()){
			BtNode tmpNode = (BtNode) queue.remove(0) ;
			System.out.print(tmpNode.data + "、");
			if(tmpNode.lchild != null){
				queue.add(tmpNode.lchild) ;
			}
			if(tmpNode.rchild != null){
				queue.add(tmpNode.rchild);
			}
		}
	}
	
	/*
	 * 求二叉樹的深度(遞歸實現)
	 */
	private static int getDepthOfBTree(BtNode bt){
		if(bt == null){
			return 0 ;
		}
		int a , b ;
		a = getDepthOfBTree(bt.lchild) ;
		b = getDepthOfBTree(bt.rchild) ;
		return (a > b ? a : b) + 1 ;
	}
	
	/*
	 *  求二叉樹的節點個數(遞歸實現)
	 */
	private static int getNumOfNodes(BtNode bt){
		if(bt == null){
			return 0 ;
		}
		return getNumOfNodes(bt.lchild) + getNumOfNodes(bt.rchild) + 1 ;
	}
	
	/*
	 *  求雙親節點
	 */
	private static BtNode parent(String elem){
		return (root==null || root.data.equals(elem))? null : parent(root,elem) ;
	}
	private static BtNode parent(BtNode subTree, String elem) {
		if(subTree == null){
			return null ;
		}
		if((subTree.lchild!=null && subTree.lchild.data.equals(elem)) || (subTree.rchild!= null && subTree.rchild.data.equals(elem))){
			return subTree ;
		}
		BtNode p ;
		if((p=parent(subTree.lchild, elem))!=null){
			return p ;
		}else
			return parent(subTree.rchild, elem) ;
	}
	/*
	 * 在釋放某個結點時,該結點的左右子樹都已經釋放,  所以應該採用後續遍歷,當訪問某個結點時將該結點的存儲空間釋放
	 */
	private static void removeSubTree(BtNode subTree){
		if(subTree != null){
			removeSubTree(subTree.lchild);
			removeSubTree(subTree.rchild); 
			subTree = null ;
		}
	}
	
	/*
	 *  查找指定節點,輸入節點的data,返回節點的BtNode對象
	 */
	private static BtNode findNode(BtNode bt,String findData){
		BtNode findResultNode = null ;
		if(bt == null){
			return null ;
		}
		if(bt.data.equals(findData)){
			return bt ;
		}
		if(bt.lchild != null){
			findResultNode = findNode(bt.lchild, findData) ;
			if(findResultNode!=null) 
				return findResultNode ;
		}
		if(bt.rchild != null){
			findResultNode = findNode(bt.rchild, findData) ;
			if(findResultNode!=null) 
				return findResultNode ;
		}
		return null ;
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章