自主实现二叉树的递归遍历,非递归遍历,结点数量,树的高度,查询树的结点值

本文主要讲怎么实现二叉树的相关功能:

树结点类:

public class Node {
	Node leftChild;
	Object value;
	Node rightChild;
	
	public Node(Node leftChild, Object value, Node rightChild) {
		super();
		this.leftChild = leftChild;
		this.value = value;
		this.rightChild = rightChild;
	}

	public Node(Object value) {
		super();
		this.value = value;
	}

	@Override
	public String toString() {
		return "Node [leftChild=" + leftChild + ", value=" + value + ", rightChild=" + rightChild + "]";
	}
	
	
	
}

实现代码:

public class LinkedBinaryTree implements BinaryTree{
	Node root;//二叉树的根
	
	
	public LinkedBinaryTree(Node root) {
		super();
		this.root = root;
	}
	/**
	 * 是否为空树
	 * 
	 */
	@Override
	public boolean isEmpty() {
		
		return root == null;
		
	}
	/**
	 * 树的结点数量
	 */
	@Override
	public int size() {
		
		return this.size(root);
	}
	
	private int size(Node root){
		if(root == null){
			return 0;
		}else{
			int nl = this.size(root.leftChild);
			int nr = this.size(root.rightChild);
			return nl+nr+1;
		}
	}
	/**
	 * 获取二叉树的高度
	 */
	@Override
	public int getHeight() {
		
		return getHeight(root);
	}
	private int getHeight(Node root){
		if(root==null){
			return 0;
		}else{
			int nl = getHeight(root.leftChild);
			int nr = getHeight(root.rightChild);
			return nl>nr?nl+1:nr+1;
		}
	}
	/**
	 * 查询指定的结点
	 */
	@Override
	public Node findKey(int value) {
		
		return this.findKey(value,root);
	}
	
	private Node findKey(Object value,Node root){
		if(root == null){
			return null;
		}else if(root != null && root.value == value){
			return root;
		}else{
			Node node1 = this.findKey(value,root.leftChild);
			Node node2 = this.findKey(value, root.rightChild);
			if(node1 != null && node1.value==value){
				return node1;
			}else if(node2 != null && node2.value==value){
				return node2;
			}else{
				return null;
			}
		}
			}
	/**
	 * 前序递归遍历
	 */

	@Override
	public void preOrderTravese() {
		System.out.print("先序遍历:");
		this.preOrderTravese(root);
		System.out.println();
	}
	
	private void preOrderTravese(Node root){
		if(root != null){
			System.out.print(root.value+" ");
			this.preOrderTravese(root.leftChild);
			this.preOrderTravese(root.rightChild);
		}
	}
	/**
	 * 中序递归遍历
	 */
	@Override
	public void inOrderTraverse() {
		System.out.print("中序遍历:");
		this.inOrderTraverse(root);
		System.out.println();
	}
	
	private void inOrderTraverse(Node root){
		if(root != null){
			this.inOrderTraverse(root.leftChild);
			System.out.print(root.value+" ");
			this.inOrderTraverse(root.rightChild);
		}
	}
	/**
	 * 后续递归遍历
	 */
	@Override
	public void postOrderTraverse() {
		System.out.print("后序遍历:");
		this.postOrderTraverse(root);
		System.out.println();
	}
	
	private void postOrderTraverse(Node root){
		if(root != null){
			this.postOrderTraverse(root.leftChild);
			this.postOrderTraverse(root.rightChild);
			System.out.print(root.value+" ");
		}
	}
	/**
	 * 前序非递归遍历
	 */
	@Override
	public void preOrderByStack() {
		System.out.print("前序非递归遍历:");
		Deque<Node> stack = new LinkedList<Node>();
		while(root != null || !stack.isEmpty()){
			
			while(root != null){
				System.out.print(root.value+" ");
				stack.push(root);
				root = root.leftChild;
			}
			
			if(!stack.isEmpty()){
				root =  stack.pop();
				root = root.rightChild;
			}
		}
		System.out.println();
		
	}
	/**
	 * 中序非递归遍历
	 */
	@Override
	public void inOrderByStack() {
		System.out.print("中序非递归遍历:");
		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.print(current.value+" ");
				current = current.rightChild;
			}
		}
		System.out.println();
	}
	/**
	 * 后序非递归遍历
	 */
	@Override
	public void postOrderByStack() {
		System.out.print("后序非递归遍历");
		Deque<Node> stack1 = new LinkedList<>();
		Deque<Integer> stack2 = new LinkedList<>();
		Node current = root;
		stack1.push(current);
		stack2.push(0);
		do{
		while(current!= null){
			current = current.leftChild;
				if(current!=null){
				stack1.push(current);
				stack2.push(0);
				}
		}
		
		while(!stack1.isEmpty()){
		if(stack2.peek()==1){
			System.out.print(stack1.pop().value+" ");
			stack2.pop();
		}else{
			stack2.pop();
			stack2.push(1);
			current = stack1.peek().rightChild;
			if(current!=null){
			stack1.push(current);
			stack2.push(0);
			}
			break;
		}
			}
		}while(!stack1.isEmpty());
	}
	/**
	 * 按照层次遍历二叉树
	 */
	@Override
	public void levelOrderByStack() {
		System.out.print("层次遍历");
		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 temp = queue.poll();
				System.out.print(temp.value+" ");
				if(temp.leftChild != null)	queue.add(temp.leftChild);
				if(temp.rightChild != null) queue.add(temp.rightChild);
			}
		}
		System.out.println();
	}
}

猜你喜欢

转载自blog.csdn.net/jx1234562/article/details/82827262