Java实现二叉树的前、中、后序遍历和前序查找

本文介绍了如何使用Java实现二叉树的前序、中序和后序遍历,以及前序查找功能。通过创建二叉树节点类并进行测试,展示了一棵具体的二叉树结构,并给出了不同遍历方式下的结果。例如,中序遍历得到的顺序为4 2 5 1 6 3 7,前序遍历为1 2 4 5 3 6 7,后序遍历为4 5 2 6 7 3 1。同时,举例说明了前序查找9不在树中,因此返回不存在的结果。
摘要由CSDN通过智能技术生成

二叉树定义类,通过根节点实现二叉树的索引:

package cn.tree.test;
import cn.tree.java.*;
public class BinaryTree {
	Node root;
	//设置根节点
	public void setRoot(Node root) {
		this.root = root;
	}
	//获取根节点
	public Node getRoot() {
		return root;
	}
	//前序
	public void frontShow() {
		root.frontShow();
	}
	//中序
	public void midShow() {
		// TODO Auto-generated method stub
		root.midShow();
	}
	//后序
	public void backShow(){
		root.backShow();
	}
	public Node frontSearch(int i) {
		return root.frontSearch(i);
	}
}

节点类定义:

package cn.tree.java;

public class Node {
	public int data;
	public Node lNode;
	public Node rNode;
	public Node(int data){
		this.data = data;
	}
	public Node() {
		// TODO Auto-generated constructor stub
	}
	//设置左儿子
	public void setLNode(Node lNode) {
		this.lNode = lNode;
	}
	//设置右儿子
	public void setrNode(Node rNode) {
		this.rNode = rNode;
	}
	//前序遍历
	public void frontShow() {
		//前序遍历
		System.out.print(data+" ");
		if(lNode!=null){
			lNode.frontShow();
		}
		if(rNode!=null){
			rNode.frontShow();
		}
	}
	//中序遍历
	public void midShow() {
		// TODO Auto-generated method stub
		if(lNode!=null){
			lNode.midShow();
		}
		System.out.print(data+" ");
		if(rNode!=null){
			rNode.midShow();
		}
	}
	//后序遍历
	public void backShow() {
		if(lNode!=null){
			lNode.backShow();
		}
		if(rNode!=null){
			rNode.backShow();
		}
		System.out.print(data+" ");
	}
	public Node frontSearch(int i) {
		Node targetNode = null;
		if(data==i){
			return this;
		}
		else{
			if(lNode!=null){
				targetNode = lNode.frontSearch(i);
			}
			if(targetNode!=null){
				return targetNode;
			}
			if(rNode!=null){
				targetNode = rNode.frontSearch(i);
			}
		}
		return targetNode;
	}
}

测试类:

package cn.tree.test;
import cn.tree.java.Node;

public class TestTree {
	public static void main(String[] args) {
		//空树
		BinaryTree binaryTree = new BinaryTree();
		//根节点
		Node root = new Node(1);
		//把根节点赋值给树
		binaryTree.setRoot(root);
		Node rootLNode = new Node(2);
		Node rootRNode = new Node(3);
		//设置树的左节点
		binaryTree.root.setLNode(rootLNode);
//		设置树的右节点
		binaryTree.root.setrNode(rootRNode);
		rootLNode.setLNode(new Node(4));
		rootLNode.setrNode(new Node(5));
		rootRNode.setLNode(new Node(6));
		rootRNode.setrNode(new Node(7));
		//前序遍历
		System.out.println("前序遍历为:");
		binaryTree.frontShow();
		System.out.println("");
		System.out.println("中序遍历为:");
		//中序遍历
		binaryTree.midShow();
		System.out.println("");
		System.out.println("后序遍历为:");
		//后序遍历
		binaryTree.backShow();
		System.out.println("");
		System.out.println("前序查找");
		//前序查找
		Node resNode = binaryTree.frontSearch(9);
		if(resNode!=null){
			System.out.println("存在");
		}
		else{
			System.out.println("不存在");
		}
		
	}
}

构建的树为:

运行结果为:用例查找数字9,结果应该为不存在

Depth First Traversals:
(a) Inorder (Left, Root, Right) : 4 2 5 1 6 3 7(中序)
(b) Preorder (Root, Left, Right) : 1 2 4 5 3 6 7 (前序)
(c) Postorder (Left, Right, Root) : 4 5 2 6 7 3 1(后序)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w͏l͏j͏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值