前序中序求后序java_JAVA代码实现二叉树的前序、中序、后序遍历

二叉树的遍历

二叉树的遍历分为前序、中序和后序。可以通过遍历父节点的顺序来区别。前序遍历的顺序是父节点–左子节点–右子节点;中序遍历的顺序是左子节点–父节点–右子节点;后序遍历的顺序是左子节点–右子节点–父节点。通过递归的方式可以将其实现。我主要写了以下几个方法

二叉树的前序、中序和后序遍历;

按节点编号顺序插入节点;

根据编号前序、中序和后序查找对应的节点

package com.tree.binnaryTree;

/*

* 二叉树的前序、中序、后序遍历

* 二叉树根据id前序、中序、后序查找

* 二叉树的删除节点

* 二叉树的存储节点

*/

public class BinnaryTreeDemo2 {

public static void main(String[] args) {

// 创建一个二叉树

BinnaryTree binnaryTree = new BinnaryTree();

// 创建节点

Node node1 = new Node(1, "AAA");

Node node2 = new Node(2, "BBB");

Node node3 = new Node(3, "CCC");

Node node4 = new Node(4, "DDD");

Node node5 = new Node(5, "EEE");

Node node6 = new Node(6, "FFF");

Node node7 = new Node(7, "GGG");

Node node8 = new Node(8, "HHH");

// 向二叉树添加节点

binnaryTree.add(node5);

binnaryTree.add(node1);

binnaryTree.add(node8);

binnaryTree.add(node4);

binnaryTree.add(node7);

binnaryTree.add(node3);

binnaryTree.add(node6);

binnaryTree.add(node2);

// 前序遍历

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

binnaryTree.preList();

// 中序遍历

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

binnaryTree.infixList();

// 后序遍历

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

binnaryTree.postList();

// 根据id编号前序查找

System.out.println("根据id编号前序查找");

binnaryTree.preListById(3);

// 根据id编号中序查找

System.out.println("根据id编号中序查找");

binnaryTree.infixListById(3);

// 根据id编号后序查找

System.out.println("根据id编号后序查找");

binnaryTree.postListById(3);

}

}

/**

* 定义二叉树

*

* @author DXQ

*

*/

class BinnaryTree {

private Node root;// 根节点

public void setRoot(Node node) {

this.root = node;

}

public Node getRoot() {

return root;

}

/**

* 按顺序添加节点

*

* @param node

*/

public void add(Node node) {

// 判断是否是空树,如果是空树,直接将节点赋给根节点

if (root == null) {

root = node;

return;

}

// 如果不是空树

root.add(node);

}

/**

* 二叉树的前序遍历

*/

public void preList() {

// 首先判断是否为空树

if (root == null) {

System.out.println("空树,无法前序遍历");

} else {

root.preList();

}

}

/**

* 二叉树的中序遍历

*/

public void infixList() {

// 首先判断是否为空树

if (root == null) {

System.out.println("空树,无法中序遍历");

} else {

root.infixList();

}

}

/**

* 二叉树的后序遍历

*/

public void postList() {

// 首先判断是否为空树

if (root == null) {

System.out.println("空树,无法中序遍历");

} else {

root.postList();

}

}

/**

* 根据id前序查找

*

* @param id

* @return

*/

public void preListById(int id) {

if (root == null) {

System.out.println("空树,无法遍历");

} else {

Node resNode = root.preListById(id);

if (resNode == null) {

System.out.printf("没有找打编号为%d的节点\n", id);

} else {

System.out.printf("编号为%d的节点姓名是%s\n", id, resNode.getName());

}

}

}

/**

* 根据id中序查找

*

* @param id

* @return

*/

public void infixListById(int id) {

if (root == null) {

System.out.println("空树,无法遍历");

} else {

Node resNode = root.infixListById(id);

if (resNode == null) {

System.out.printf("没有找打编号为%d的节点\n", id);

} else {

System.out.printf("编号为%d的节点姓名是%s\n", id, resNode.getName());

}

}

}

/**

* 根据id后序查找

*

* @param id

* @return

*/

public void postListById(int id) {

if (root == null) {

System.out.println("空树,无法遍历");

} else {

Node resNode = root.postListById(id);

if (resNode == null) {

System.out.printf("没有找打编号为%d的节点\n", id);

} else {

System.out.printf("编号为%d的节点姓名是%s\n", id, resNode.getName());

}

}

}

}

/**

* /定义节点类

*

* @author DXQ

*

*/

class Node {

private int no;

private String name;

private Node left;// 左子节点,默认为空

private Node right;// 右子节点,默认为空

public int getNo() {

return no;

}

public void setNo(int no) {

this.no = no;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Node getLeft() {

return left;

}

public void setLeft(Node left) {

this.left = left;

}

public Node getRight() {

return right;

}

public void setRight(Node right) {

this.right = right;

}

// 构造方法

public Node(int no, String name) {

super();

this.no = no;

this.name = name;

}

@Override

public String toString() {

return "Node [no=" + no + ", name=" + name + "]";

}

/**

* 按顺序添加节点

*

* @param node

*/

public void add(Node node) {

if (this.left == null && node.no < this.no) {

this.left = node;

return;

}

if (this.right == null && node.no > this.no) {

this.right = node;

return;

}

if (this.left != null && node.no < this.no) {

this.left.add(node);

}

if (this.right != null && node.no > this.no) {

this.right.add(node);

}

}

/**

* 前序遍历

*/

public void preList() {

// 首先将父节点输出

System.out.println(this);

// 判断当前节点左子节点是否为空,如果不为空,继续向左递归

if (this.left != null) {

this.left.preList();

}

// 左子树遍历完毕后遍历右子树

if (this.right != null) {

this.right.preList();

}

}

/**

* 中序遍历

*/

public void infixList() {

// 首先判断当前节点的左子节点是否为空,不为空的话就继续向左递归

if (this.left != null) {

this.left.infixList();

}

// 然后输出父节点

System.out.println(this);

// 最后判断当前节点的右子节点是否为空,不为空的话继续向右递归

if (this.right != null) {

this.right.infixList();

}

}

/**

* 后序遍历

*/

public void postList() {

// 首先判断当前节点的左子节点是否为空,不为空的话就继续向左递归

if (this.left != null) {

this.left.postList();

}

// 然后判断当前节点的右子节点是否为空,不为空的话继续向右递归

if (this.right != null) {

this.right.postList();

}

// 最后输出父节点

System.out.println(this);

}

/**

* 根据id前序遍历查找

*

* @param id

*/

public Node preListById(int id) {

// 首先判断当前节点是否就是要查的节点

if (this.no == id) {

return this;

}

// 如果当前节点不是,并且它的左子节点不为空,那就左递归继续找

Node resNode = null;

if (this.left != null) {

resNode = this.left.preListById(id);

}

// 如果左递归结束返回的值不为空,说明找到了,直接返回即可

if (resNode != null) {

return resNode;

}

// 如果左递归没有找到,判断当前节点的右子节点是否为空,如果不为空,开始右递归查找

if (this.right != null) {

resNode = this.right.preListById(id);

}

// 不管找没找到都要返回结果了

return resNode;

}

/**

* 根据id中序遍历查找

*

* @param id

* @return

*/

public Node infixListById(int id) {

// 首先判断当前节点的左子节点是否为空,如果不为空,则左递归查找

Node resNode = null;

if (this.left != null) {

resNode = this.left.infixListById(id);

}

// 然后判断当前节点是否就是要查找的节点,如果是就返回

if (this.no == id) {

return this;

}

// 如果左递归返回的值不为空,说明找到了,直接返回

if (resNode != null) {

return resNode;

}

// 如果左递归返回的值为空,判断当前节点的右子节点是否为空,如果不为空,开始右递归查找

if (this.right != null) {

resNode = this.right.infixListById(id);

}

// 右递归结束后,无论找没找到,都要将结果返回

return resNode;

}

/**

* 根据id后序遍历查找

*

* @param id

* @return

*/

public Node postListById(int id) {

// 首先判断当前节点的左子节点是否为空,如果不为空,则左递归查找

Node resNode = null;

if (this.left != null) {

resNode = this.left.postListById(id);

}

// 如果左递归返回的值不为空,说明找到了,直接返回

if (resNode != null) {

return resNode;

}

// 如果左递归返回的值为空,判断当前节点的右子节点是否为空,如果不为空,开始右递归查找

if (this.right != null) {

resNode = this.right.postListById(id);

}

// 如果右递归返回的值不为空,说明找到了,直接返回

if (resNode != null) {

return resNode;

}

// 最后比较当前节点

if (this.no == id) {

return this;

}

// 最后将结果返回

return resNode;

}

}

9cc77729854bc000f97ca908fb97b80d.png

4a668feaa3018a2e1706f07e5002bcc8.png

爱学习的程序猿dxq

发布了14 篇原创文章 · 获赞 0 · 访问量 412

私信

关注

标签:Node,遍历,JAVA,resNode,前序,节点,二叉树,null,id

来源: https://blog.csdn.net/qq_41665523/article/details/104617405

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值