二叉树深度遍历(DFS)与广度遍历(BFS)代码实现

思路文章参考

Java实现深度优先遍历和广度优先遍历

代码

深度遍历(DFS)

package tree;

import java.util.Stack;

/**
 * 深度优先遍历二叉树
 * @author wangdongyu
 *
 */
public class DFSIterator implements IteratorMethodBase {
	
	public void doIterator(Node node) {
		if(node == null) {
			return;
		}
		Stack<Node> nodeStack = new Stack<>();
		nodeStack.push(node);
		System.out.println("深度优先遍历结果为:");
		while(nodeStack.size() > 0) {
			node = nodeStack.pop();
			if(node == null) {
				continue;
			}
			System.out.print(node.getData()+" ");
			if(node.getRightNode() != null) {
				nodeStack.push(node.getRightNode());
			}
			if(node.getLeftNode() != null) {
				nodeStack.push(node.getLeftNode());
			}
		}
	}
	
}

广度遍历(BFS)

package tree;

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

/**
 * 广度优先遍历二叉树
 * @author wangdongyu
 *
 */
public class BFSIterator implements IteratorMethodBase {

	public void doIterator(Node node) {
		if(node == null) {
			return;
		}
		Queue<Node> nodeQueue = new LinkedList<>();
		nodeQueue.add(node);
		System.out.println("广度优先遍历结果为:");
		while(nodeQueue.size() > 0) {
			node = nodeQueue.poll();
			if(node == null) {
				continue;
			}
			System.out.print(node.getData() + " ");
			if(node.getLeftNode()!= null) {
				nodeQueue.add(node.getLeftNode());
			}
			if(node.getRightNode() != null) {
				nodeQueue.add(node.getRightNode());
			}
		}
		
	}
	
}

Node类

package tree;

/**
 * 树节点类
 * @author wangdongyu
 *
 */
public class Node {

	private int data;
	private Node leftNode;
	private Node rightNode;
	
	public Node(int data, Node leftNode, Node rightNode) {
		this.data = data;
		this.leftNode = leftNode;
		this.rightNode = rightNode;
	}
	
	public int getData() {
		return data;
	}
	public void setData(int data) {
		this.data = data;
	}
	public Node getLeftNode() {
		return leftNode;
	}
	public void setLeftNode(Node leftNode) {
		this.leftNode = leftNode;
	}
	public Node getRightNode() {
		return rightNode;
	}
	public void setRightNode(Node rightNode) {
		this.rightNode = rightNode;
	}
	
	
	
}

遍历方法接口

package tree;

public interface IteratorMethodBase {
	public void doIterator(Node node);
}

main方法类

package tree;


public class TestMain {
	public static void main(String[] args) {
		// 传入BFS则为广度遍历,传入DFS则为深度遍历
		IteratorMethodBase methodBase = createIteratorMethod("BFS");
		methodBase.doIterator(initTree());
		
	}
	public static Node initTree() {
		Node node4 = new Node(4, null, null);
		Node node5 = new Node(5, null, null);
		Node node6 = new Node(6, null, null);
		Node node7 = new Node(7, null, null);
		Node node2 = new Node(2, node4, node5);
		Node node3 = new Node(3, node6, node7);
		Node node = new Node(1, node2, node3);
		return node;
	}
	
	public static IteratorMethodBase createIteratorMethod(String type) {
		if("BFS".equals(type)) {
			return new BFSIterator();
		} else if("DFS".equals(type)) {
			return new DFSIterator();
		} else {
			return null;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值