JAVA数据结构与算法之二叉树查找(前中后三种)

本文介绍了如何使用JAVA实现二叉树的前、中、后序遍历,作为作者复习数据结构与算法的一部分,适合有一定基础的读者。博客提供了代码实现,并鼓励读者一起学习探讨。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

最近面临毕业就业,在复习数据结构与算法,为了更好地掌握,加深印象,所以决定写一些博客来知识复现。
温馨提示:这篇博客可能不适合刚学数据结构的新手。

代码实现

package sjjg;

public class BinarySearchTree {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BinaryTree3 binaryTree = new BinaryTree3();
		HeroNode3 root = new HeroNode3(1, "宋江");
		HeroNode3 node2 = new HeroNode3(2, "吴用");
		HeroNode3 node3 = new HeroNode3(3, "卢俊义");
		HeroNode3 node4 = new HeroNode3(4, "林冲");
		HeroNode3 node5 = new HeroNode3(5, "关胜");

		root.setLeft(node2);
		root.setRight(node3);
		node3.setRight(node4);
		node3.setLeft(node5);
		binaryTree.setRoot(root);

		System.out.println("前序查找-----");
		HeroNode3 resNode = binaryTree.preSearch(3);
		if (resNode != null) {
			System.out.printf("找到了,信息为 no=%d name=%s", resNode.getNo(), resNode.getName());
		} else {
			System.out.printf("没有找到 no = %d 的英雄", 5);
		}

		HeroNode3 resNode2 = binaryTree.infixSearch(3);
		System.out.println("\n中序查找-----");
		if (resNode2 != null) {
			System.out.printf("找到了,信息为 no=%d name=%s", resNode2.getNo(), resNode2.getName());
		} else {
			System.out.printf("没有找到 no = %d 的英雄", 5);
		}

		HeroNode3 resNode3 = binaryTree.preSearch(3);
		System.out.println("\n后序查找-----");
		if (resNode3 != null) {
			System.out.printf("找到了,信息为 no=%d name=%s", resNode3.getNo(), resNode3.getName());
		} else {
			System.out.printf("没有找到 no = %d 的英雄", 5);
		}

	}

}

//先创建HeroNode 结点
class HeroNode3 {
	private int no;
	private String name;
	private HeroNode3 left; // 默认null
	private HeroNode3 right; // 默认null

	public HeroNode3(int no, String name) {
		this.no = no;
		this.name = name;
	}

	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 HeroNode3 getLeft() {
		return left;
	}

	public void setLeft(HeroNode3 left) {
		this.left = left;
	}

	public HeroNode3 getRight() {
		return right;
	}

	public void setRight(HeroNode3 right) {
		this.right = right;
	}

	@Override
	public String toString() {
		return "HeroNode [no=" + no + ", name=" + name + "]";
	}

	// 前序查找
	public HeroNode3 preSearch(int no) {
		if (this.no == no) {
			return this;
		}
		HeroNode3 resNode = null;
		if (this.left != null) {
			resNode = this.left.preSearch(no);
		}
		if (this.right != null) {
			resNode = this.right.preSearch(no);
		}
		return resNode;
	}

	// 中序查找
	public HeroNode3 infixSearch(int no) {

		HeroNode3 resNode = null;
		if (this.left != null) {
			resNode = this.left.infixSearch(no);
		}
		if (this.no == no) {
			return this;
		}
		if (this.right != null) {
			resNode = this.right.infixSearch(no);
		}
		return resNode;
	}

	// 后序查找
	public HeroNode3 postSearch(int no) {

		HeroNode3 resNode = null;
		if (this.left != null) {
			resNode = this.left.postSearch(no);
		}

		if (this.right != null) {
			resNode = this.right.postSearch(no);
		}
		if (this.no == no) {
			return this;
		}
		return resNode;
	}
}

class BinaryTree3 {
	private HeroNode3 root;

	public HeroNode3 getRoot() {
		return root;
	}

	public void setRoot(HeroNode3 root) {
		this.root = root;
	}

	//前序查找
	public HeroNode3 preSearch(int no) {
		if (root != null) {
			return root.preSearch(no);
		} else {
			return null;
		}
	}

	//中序查找
	public HeroNode3 infixSearch(int no) {
		if (root != null) {
			return root.infixSearch(no);
		} else {
			return null;
		}
	}

	//后序查找
	public HeroNode3 postSearch(int no) {
		if (root != null) {
			return root.postSearch(no);
		} else {
			return null;
		}
	}

}

结束语

欢迎访问我的博客,一起学习,一起进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值