1. 程式人生 > >求二叉樹的深度,前序遍歷,中序遍歷,後序遍歷,節點個數,是否為空,查詢某一個節點,測試方式

求二叉樹的深度,前序遍歷,中序遍歷,後序遍歷,節點個數,是否為空,查詢某一個節點,測試方式

package com.bjsxt.tree;
 
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
 
/**
 * 
 * @author Administrator
 *
 */
public class BinaryTree implements BinaryInterface{
	Node root;
 
	public Node getRoot() {
		return root;
	}
 
	public void setRoot(Node root) {
		this.root = root;
	}
 
	public BinaryTree(Node root) {
		super();
		this.root = root;
	}
 
	public BinaryTree() {
		super();
		// TODO Auto-generated constructor stub
	}
 
	@Override
	public boolean isEmpty() {
		
		return root==null;
	}
 
	@Override
	public int size() {
		if(root==null) {
			return 0;
		}else if(root.leftChild==null&&root.rightChild==null) {
			return 1;
		}else {
			BinaryTree lefttree=new BinaryTree(root.leftChild);
			BinaryTree righttree=new BinaryTree(root.rightChild);
			int i=lefttree.size();
			int j=righttree.size();
			return i+j+1;
		}		
	}
 
	@Override
	public int getheight() {
		
		return this.getHeight(root);
	}
 
	private int getHeight(Node root) {
		if(root==null) {
			return 0;
		}else if(root.leftChild==null&&root.rightChild==null) {
			return 1;
		}
		else {
			int i=this.getHeight(root.leftChild);
			int j=this.getHeight(root.rightChild);
			return (i<j)?(i+1):(j+1);
		}		
	}
 
	@Override
	public Node findKey(int value) {
		
		return findKey(value,root);
	}
 
	private Node findKey(int value, Node root2) {
		if(root==null) {
			return null;
		}else if(root!=null&&root.value==value) {
			return root;
		}else {
			Node node1=this.findKey(value, root2.leftChild);
			Node node2=this.findKey(value, root2.rightChild);
			if(node1!=null&&node1.value==value) {
				return node1;
			}else if(node2!=null&&node2.value==value) {
				return node2;
			}else {
				return null;
			}
		}
		
	}
 
	@Override
	public void preOrderTraverse() {
//		System.out.println("前序遍歷");
		if(root!=null) {
			System.out.println(root.value+"");
			if(root.leftChild!=null) {
				BinaryTree lefttree=new BinaryTree(root.leftChild);
				lefttree.preOrderTraverse();
			}
			if(root.rightChild!=null) {
				BinaryTree righttree=new BinaryTree(root.rightChild);
				righttree.preOrderTraverse();
			}
		}		
	}
 
	@Override
	public void midOrderTraverse() {
		System.out.println("中序遞迴遍歷二叉樹");
		this.MidOrderTraverse(root);
		
	}
 
	private void MidOrderTraverse(Node root2) {
		
		this.MidOrderTraverse(root.leftChild);
		System.out.println(root.value);
		this.MidOrderTraverse(root2.rightChild);
	}
 
	@Override
	public void postOrderTraverse() {
		System.out.println("後序遞迴遍歷二叉樹");
		this.postOrderTraverse(root);
		System.out.println();
	}
 
	private void postOrderTraverse(Node node) {
		if(root!=null) {
			this.postOrderTraverse(node.leftChild);
			this.postOrderTraverse(node.rightChild);
			System.out.println(node.value+" ");
		}else {
			System.out.println("zhiweikong");
		}
		
	}
 
	
 
	
	//中序遍歷非遞迴
	@Override
	public void inOrderByStack() {
		System.out.println("中序遍歷非遞迴操作");
		//建立棧
		Deque<Node> stack=new LinkedList<Node>();
		Node current=root;
		while(current!=null||!stack.isEmpty()) {
			while(current!=null) {
				stack.push(current);
				current=current.leftChild;			
			}
			if(!stack.isEmpty()) {
				current=stack.pop();
				System.out.println(current.value+"");
				current=current.rightChild;
			}
		}
		System.out.println();		
	}
	//中序遍歷二叉樹
	@Override
	public void preOrderByStack() {
		System.out.println("中序遍歷二叉樹非遞迴操作");
		//建立棧
		Deque<Node> stack=new LinkedList<Node>();
		Node current=root;
		while(current!=null||!stack.isEmpty()) {
			while(current!=null) {
				stack.push(current);
				current=current.leftChild;
			}
			if(!stack.isEmpty()) {
				current=stack.pop();
				System.out.println(current.value+"");
				current=current.rightChild;
			}
		}
		
	}
 
	@Override
	public void levelOrderByStack() {
		if(root==null) {
			return;
		}
		//建立佇列
		Queue<Node> queue=new LinkedList<Node>();
		queue.add(root);
		while(queue.size()!=0) {
			int len=queue.size();
			for(int i=0;i<len;i++) {
				Node tmp=queue.poll();
				System.out.println(tmp.value);
				if(tmp.leftChild!=null) {
					queue.add(tmp.leftChild);
				}
				if(tmp.rightChild!=null) {
					queue.add(tmp.rightChild);
				}
			}
		}		
	}
	@Override
	public void postOrderByStack() {
		
		
	}
	public int getwidth(Node root) {
		if(root==null) {
			return 0;
		}
		Queue<Node> queue=new LinkedList<Node>();
		int maxWidth=1;
		queue.add(root);
		while(true) {
			int len=queue.size();
			if(len==0)
				break;
			while(len>0) {
				Node node=queue.poll();
				len--;
				if(node.leftChild!=null) 
					queue.add(node.leftChild);
				if(node.rightChild!=null)
					queue.add(node.rightChild);	
			}
			maxWidth=Math.max(maxWidth, queue.size());
		}
		return maxWidth;
	}
	//獲取二叉樹中葉子結點的個數:
	public int getLeave(Node root) {
		int count=0;
		if(root==null) 
			return 0;
			
		if(root.leftChild!=null)
			getLeave(root.leftChild);
		if(root.rightChild!=null)
			getLeave(root.rightChild);
		if(root.leftChild==null&&root.rightChild==null) 
			count++;
			
		return count;
		
	}
	
	//獲取二叉樹的最大寬度
	public int getWidth(Node root) {
		int maxWith=1;
		if(root==null) {
			return 0;
		}else if(root.leftChild==null&&root.rightChild==null) {
			return 1;
		}else {
			Queue<Node> queue=new LinkedList<Node>();
			maxWith=1;
			queue.add(root);
			while(true) {
				int len=queue.size();
				if(len==0) {
					break;
				}else {
					while(len>0) {
						Node node=queue.poll();
						len--;
						if(node.leftChild!=null) {
							queue.add(node.leftChild);
						}else if(node.rightChild!=null) {
							queue.add(node.rightChild);
						}
						
					}
				}
			}
			maxWith=Math.max(maxWith, queue.size());			
		}
		return maxWith;			
	}
	
	
}

測試方式:

package com.bjsxt.tree;
 
public class Test {
	public static void main(String[] args) {
		Node node5=new Node(5,null,null);
		Node node4=new Node(4,null,node5);
		Node node7=new Node(7,null,null);
		Node node3=new Node(3,null,null);
		Node node6=new Node(6,null,node7);
		Node node2=new Node(2,node3,node6);
		Node node1=new Node(1,node4,node2);
		
		BinaryTree btree=new BinaryTree(node1);
//		System.out.println(btree.size());
//		System.out.println(btree.isEmpty());
//		System.out.println(btree.getheight());
//		btree.preOrderTraverse();
//		btree.midOrderTraverse();
//		btree.postOrderTraverse();
//		btree.preOrderByStack();
////		btree.findKey(4);
//		btree.inOrderByStack();
//		System.out.println(btree.getwidth(node1));
		System.out.println(btree.getLeave(node1));
		
	}
 
}

原圖在這裡~