java二叉树层次遍历二叉树_Java实现二叉树先序,中序,后序,层次遍历

一、以下是我要解析的一个二叉树的模型形状。本文实现了以下方式的遍历:

1、用递归的方法实现了前序、中序、后序的遍历;

2、利用队列的方法实现层次遍历;

3、用堆栈的方法实现前序、中序、后序的遍历。

13625a9b23824bee993153d741329166.png

二、遍历

1、首先创建节点类

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

public classNode {private intdata;privateNode leftNode;privateNode rightNode;public Node(intdata, Node leftNode, Node rightNode){this.data =data;this.leftNode =leftNode;this.rightNode =rightNode;

}public intgetData() {returndata;

}public void setData(intdata) {this.data =data;

}publicNode getLeftNode() {returnleftNode;

}public voidsetLeftNode(Node leftNode) {this.leftNode =leftNode;

}publicNode getRightNode() {returnrightNode;

}public voidsetRightNode(Node rightNode) {this.rightNode =rightNode;

}

}

View Code

2、递归方式实现前序、中序、后续遍历

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

public classBinaryTree {/***@authoryaobo

* 二叉树的先序中序后序排序*/

public Node init() {//注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错

Node J = new Node(8, null, null);

Node H= new Node(4, null, null);

Node G= new Node(2, null, null);

Node F= new Node(7, null, J);

Node E= new Node(5, H, null);

Node D= new Node(1, null, G);

Node C= new Node(9, F, null);

Node B= new Node(3, D, E);

Node A= new Node(6, B, C);return A; //返回根节点

}public voidprintNode(Node node){

System.out.print(node.getData());

}public void theFirstTraversal(Node root) { //先序遍历

printNode(root);if (root.getLeftNode() != null) { //使用递归进行遍历左孩子

theFirstTraversal(root.getLeftNode());

}if (root.getRightNode() != null) { //递归遍历右孩子

theFirstTraversal(root.getRightNode());

}

}public void theInOrderTraversal(Node root) { //中序遍历

if (root.getLeftNode() != null) {

theInOrderTraversal(root.getLeftNode());

}

printNode(root);if (root.getRightNode() != null) {

theInOrderTraversal(root.getRightNode());

}

}public void thePostOrderTraversal(Node root) { //后序遍历

if (root.getLeftNode() != null) {

thePostOrderTraversal(root.getLeftNode());

}if(root.getRightNode() != null) {

thePostOrderTraversal(root.getRightNode());

}

printNode(root);

}public static voidmain(String[] args) {

BinaryTree tree= newBinaryTree();

Node root=tree.init();

System.out.println("先序遍历");

tree.theFirstTraversal(root);

System.out.println("");

System.out.println("中序遍历");

tree.theInOrderTraversal(root);

System.out.println("");

System.out.println("后序遍历");

tree.thePostOrderTraversal(root);

System.out.println("");

}

}

View Code

3、借助队列实现层次遍历

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

//层次遍历

public voidtheLeverTraversal(Node root) {if (root == null) {return;

}//新建一个队列,LinkedList实现了Quene接口,可以直接当作队列来用

LinkedList queue = new LinkedList();

Node current;//当前节点

queue.offer(root);//根节点入队列

while (!queue.isEmpty()) {

current= queue.poll(); //取出队列的头节点

System.out.print(current.val + " ");//输出队列的头节点的值

if (current.left != null) {

queue.offer(current.left);//如果当前节点的左节点不为空,则左节点入队列

}if (current.right != null) {

queue.offer(current.right);//如果当前节点的右节点不为空,则右节点入队列

}

}

}

View Code

4、堆栈方式实现前序、中序、后续遍历

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

public classBinaryTree1 {public Node init() {//注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错

Node J = new Node(8, null, null);

Node H= new Node(4, null, null);

Node G= new Node(2, null, null);

Node F= new Node(7, null, J);

Node E= new Node(5, H, null);

Node D= new Node(1, null, G);

Node C= new Node(9, F, null);

Node B= new Node(3, D, E);

Node A= new Node(6, B, C);return A; //返回根节点

}public voidprintNode(Node node){

System.out.print(node.getData());

}public void theFirstTraversal_Stack(Node root) { //先序遍历

Stack stack = new Stack();

Node node=root;while (node != null || stack.size() > 0) { //将所有左孩子压栈

if (node != null) { //压栈之前先访问

printNode(node);

stack.push(node);

node=node.getLeftNode();

}else{

node=stack.pop();

node=node.getRightNode();

}

}

}public void theInOrderTraversal_Stack(Node root) { //中序遍历

Stack stack = new Stack();

Node node=root;while (node != null || stack.size() > 0) {if (node != null) {

stack.push(node);//直接压栈

node =node.getLeftNode();

}else{

node= stack.pop(); //出栈并访问

printNode(node);

node=node.getRightNode();

}

}

}public void thePostOrderTraversal_Stack(Node root) { //后序遍历

Stack stack = new Stack();

Stack output = new Stack();//构造一个中间栈来存储逆后序遍历的结果

Node node =root;while (node != null || stack.size() > 0) {if (node != null) {

output.push(node);

stack.push(node);

node=node.getRightNode();

}else{

node=stack.pop();

node=node.getLeftNode();

}

}

System.out.println(output.size());while (output.size() > 0) {

printNode(output.pop());

}

}public static voidmain(String[] args) {

BinaryTree1 tree= newBinaryTree1();

Node root=tree.init();

System.out.println("先序遍历");

tree.theFirstTraversal_Stack(root);

System.out.println("");

System.out.println("中序遍历");

tree.theInOrderTraversal_Stack(root);

System.out.println("");

System.out.println("后序遍历");

tree.thePostOrderTraversal_Stack(root);

System.out.println("");

}

}

View Code

-------------------------------------------------------------------------------------------------------------------------

参考链接:

http://www.cnblogs.com/yaobolove/p/6213936.html

二叉树遍历(前序、中序、后序、层次、深度优先、广度优先遍历):https://blog.csdn.net/yimingsilence/article/details/54783208

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值