递归——二叉树

结点类:

package hbtx.Btree;

public class Node {

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

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

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

实现类:

package hbtx.Btree;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

public class LinkedBinaryTree implements BinaryTree{

	private Node root;
	
	public LinkedBinaryTree() {
		super();
	}

	public LinkedBinaryTree(Node root) {
		super();
		this.root = root;
	}

	@Override
	public boolean isEmpty() {		
		return root == null;
	}
	
	@Override
	public int getHeight() {
		System.out.println("树的高度:");
		return this.getHeight(root);
	}
	
	private int getHeight(Node root){
		if(root == null){
			return 0;
		}else{			
			int nl = this.getHeight(root.leftChild);
			int nr = this.getHeight(root.rightChild);			
			return nl > nr ? nl+1:nr+1;
		}
	}
	//查询
	@Override
	public Node findKey(Object value) {
		System.out.println("根据数据查询结点:");
		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 preOrderTraverse() {
		System.out.println("先序递归:");
		this.preOrderTraverse(root);
		System.out.println();
	}
	
	private void preOrderTraverse(Node root) {		
		if(root != null){
			System.out.print(root.value+"  ");
			this.preOrderTraverse(root.leftChild);
			this.preOrderTraverse(root.rightChild);
		}		
	}

	@Override
	public void inOrderTraverse() {
		System.out.println("中序递归:");
		this.inOrderTraverse(root);
		System.out.println();
	}
	
	private void inOrderTraverse(Node root) {//node7
		if(root != null){
			this.inOrderTraverse(root.leftChild);//null
			System.out.print(root.value+"  ");//7
			this.inOrderTraverse(root.rightChild);//null
		}		
	}

	@Override
	public void postOrderTraverse() {
		System.out.println("后序递归:");
		this.postOrderTraverse(root);
		System.out.println();
		
	}

	private void postOrderTraverse(Node node) {
		if(node != null){
			this.postOrderTraverse(node.leftChild);
			this.postOrderTraverse(node.rightChild);
			System.out.print(node.value+"  ");
		}
		
	}

	@Override
	public void preOrderByStack() {
		System.out.println("先序栈:");
		Deque<Node> stack=new LinkedList<>();
		Node current=new Node();
		stack.push(root);
		while(!stack.isEmpty()) {
			current =stack.pop();
			System.out.print(current.value+"  ");
			if(current.rightChild!=null)
				stack.push(current.rightChild);
			if(current.leftChild!=null)
				stack.push(current.leftChild);
		}
		//方法二
//		Deque<Node> stack=new LinkedList<>();
//		Node current=new Node(root);
//		while(current!=null||!stack.isEmpty()) {
//			while(current!=null) {
//				stack.push(current);
//				System.out.print(current.value+"  ");
//				current=current.leftChild;
//			}
//			if(!stack.isEmpty()) {
//				current=stack.pop();
//				   current=current.rightChild;
//			}
//		}
	}
	
	@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.print(current.value + " ");
				current = current.rightChild;
			}
		}
		System.out.println();	
	}

	@Override
	public void postOrderByStack() {
		    System.out.println("后序栈:");
//		    //方法一比较复杂不推荐
//		    Node current, pre = null;
//	        Deque<Node> stack = new LinkedList<>();
//	        stack.push(root);
//	        while (!stack.isEmpty()) {
//	            current = stack.peek();
//	            if ((current.leftChild == null && current.rightChild == null) || 
//	            		(pre != null && (pre == current.leftChild || pre == current.rightChild))) {
//	                System.out.print(current.value + "  ");
//	                stack.pop();
//	                pre = current;
//	            } else {
//	                if (current.rightChild != null)
//	                    stack.push(current.rightChild);
//	                if (current.leftChild != null)
//	                    stack.push(current.leftChild);
//	            }
//	        }
	        //方法二借助两个栈
	        Deque<Node> stack1 = new LinkedList<>();
	        Deque<Node> stack2 = new LinkedList<>();
	        Node node= new Node();
	        stack1.push(root);
	        while(!stack1.isEmpty()){
	        	
	        		node=stack1.pop();
	        		stack2.push(node);
	        	
	        		if(node.leftChild!=null)
	        			stack1.push(node.leftChild);
	        		if(node.rightChild!=null)
	        			stack1.push(node.rightChild);
	        }
	        while(!stack2.isEmpty()) {
	        	System.out.print(stack2.pop().value+"  ");
	        }
	        System.out.println();
	}

	@Override
	public void levelOrderByStack() {
		if(root==null)
			return;
		Queue<Node> queue=new LinkedList<>();
		queue.offer(root);
		Node node=new Node();;
		while(queue.size()!=0) {
			node=queue.poll();
			System.out.print(node.value+"  ");
			if(node.leftChild!= null) 
				queue.offer(node.leftChild);
			if(node.rightChild!= null)
				queue.offer(node.rightChild);
		}
	}

	@Override
	public int size() {
		System.out.println("树的元素的个数:");
		return this.size(root);
	}
	
	private int size(Node root) {
		if(root==null) {
			return 0;
		}
		int nl=this.size(root.leftChild);
		int nr=this.size(root.rightChild);
		return nl+nr+1;
	}
}

总结,二叉树是练习递归算法很好模块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值