java宋江,Java编程内功-数据结构与算法「树」

package com.xie.tree;

publicclass BinaryTreeDemo {

publicstaticvoid main(String[] args) {

BinaryTree binaryTree = new BinaryTree();

HeroNode root = new HeroNode(1, "宋江");

HeroNode node2 = new HeroNode(2, "吴用");

HeroNode node3 = new HeroNode(3, "卢俊义");

HeroNode node4 = new HeroNode(4, "林冲");

HeroNode node5 = new HeroNode(5, "关胜");

//先手动创建该二叉树,后面用递归方式

root.setLeft(node2);

root.setRight(node3);

node3.setRight(node4);

node3.setLeft(node5);

binaryTree.setRoot(root);

//前序遍历

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

binaryTree.preOrder();

//中序遍历

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

binaryTree.infixOrder();

//后续遍历

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

binaryTree.postOrder();

//前序遍历查找

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

HeroNode resultNode = binaryTree.preOrderSearch(5);

if (resultNode != null) {

System.out.printf("找到了,信息为no=%d,name=%s\n", resultNode.getNo(), resultNode.getName());

System.out.println("遍历次数:"+ HeroNode.preCount);

} else{

System.out.println("没有找到");

}

//中序遍历查找

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

HeroNode resultNode1 = binaryTree.infixOrderSearch(5);

if (resultNode1 != null) {

System.out.printf("找到了,信息为no=%d,name=%s\n", resultNode1.getNo(), resultNode1.getName());

System.out.println("遍历次数:"+ HeroNode.infoxCount);

} else{

System.out.println("没有找到");

}

//后序遍历查找

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

HeroNode resultNode2 = binaryTree.postOrderSearch(5);

if (resultNode2 != null) {

System.out.printf("找到了,信息为no=%d,name=%s\n", resultNode2.getNo(), resultNode2.getName());

System.out.println("遍历次数:"+ HeroNode.postCount);

} else{

System.out.println("没有找到");

}

System.out.println("删除3号节点");

binaryTree.delNo(3);

System.out.println("删除后的节点");

binaryTree.preOrder();

/**

* 前序遍历

* HeroNode{no=1,name=宋江}

* HeroNode{no=2,name=吴用}

* HeroNode{no=3,name=卢俊义}

* HeroNode{no=5,name=关胜}

* HeroNode{no=4,name=林冲}

* 中序遍历

* HeroNode{no=2,name=吴用}

* HeroNode{no=1,name=宋江}

* HeroNode{no=5,name=关胜}

* HeroNode{no=3,name=卢俊义}

* HeroNode{no=4,name=林冲}

* 后续遍历

* HeroNode{no=2,name=吴用}

* HeroNode{no=5,name=关胜}

* HeroNode{no=4,name=林冲}

* HeroNode{no=3,name=卢俊义}

* HeroNode{no=1,name=宋江}

* 前序遍历查找~~

* 找到了,信息为no=5,name=关胜

* 遍历次数:4

* 中序遍历查找~~

* 找到了,信息为no=5,name=关胜

* 遍历次数:3

* 后序遍历查找~~

* 找到了,信息为no=5,name=关胜

* 遍历次数:2

* 删除3号节点

* 删除后的节点

* HeroNode{no=1,name=宋江}

* HeroNode{no=2,name=吴用}

*/

}

}

class BinaryTree {

private HeroNode root;

publicvoid setRoot(HeroNode root) {

this.root = root;

}

//前序遍历

publicvoid preOrder() {

if (this.root != null) {

this.root.preOrder();

}

}

//中序遍历

publicvoid infixOrder() {

if (this.root != null) {

this.root.infixOrder();

}

}

//删除节点

publicvoid delNo(intno) {

if (this.root != null) {

if (this.root.getNo() == no) {

this.root = null;

} else{

this.root.delNo(no);

}

}

return;

}

//后序遍历

publicvoid postOrder() {

if (this.root != null) {

this.root.postOrder();

}

}

//前序遍历查找

publicHeroNode preOrderSearch(intno) {

if (root != null) {

returnroot.preOrderSearch(no);

} else{

returnnull;

}

}

//中序遍历查找

publicHeroNode infixOrderSearch(intno) {

if (root != null) {

returnroot.infixOrderSearch(no);

} else{

returnnull;

}

}

//后序遍历查找

publicHeroNode postOrderSearch(intno) {

if (root != null) {

returnroot.postOrderSearch(no);

} else{

returnnull;

}

}

}

class HeroNode {

staticintpreCount = 0;

staticintinfoxCount = 0;

staticintpostCount = 0;

private intno;

private String name;

private HeroNode left;

private HeroNode right;

publicHeroNode(intno, Stringname) {

this.no=no;

this.name=name;

}

publicintgetNo() {

returnno;

}

publicvoid setNo(intno) {

this.no=no;

}

publicString getName() {

returnname;

}

publicvoid setName(Stringname) {

this.name=name;

}

publicHeroNode getLeft() {

returnleft;

}

publicvoid setLeft(HeroNodeleft) {

this.left=left;

}

publicHeroNode getRight() {

returnright;

}

publicvoid setRight(HeroNoderight) {

this.right=right;

}

@Override

publicString toString() {

return"HeroNode{"+

"no="+no+

", name="+name+

'}';

}

//前序遍历

publicvoid preOrder() {

System.out.println(this);

//递归向左子树前序遍历

if (this.left!=null) {

this.left.preOrder();

}

//递归向右子树前序遍历

if (this.right!=null) {

this.right.preOrder();

}

}

//中序遍历

publicvoid infixOrder() {

//递归向左子树中序遍历

if (this.left!=null) {

this.left.infixOrder();

}

System.out.println(this);

//递归向右子树中序遍历

if (this.right!=null) {

this.right.infixOrder();

}

}

//后序遍历

publicvoid postOrder() {

//递归向左子树后序遍历

if (this.left!=null) {

this.left.postOrder();

}

//递归向右子树后序遍历

if (this.right!=null) {

this.right.postOrder();

}

System.out.println(this);

}

//递归删除节点

//1.如果删除的节点是叶子节点,则删除该节点。

//2.如果删除的节点是非叶子节点,则删除该树。

publicvoid delNo(intno) {

/**

* 1.因为我们的二叉树是单向的,所以我们是判断当前节点的子节点是否是需要删除的节点,而不能去判断当前节点是否是需要删除的节点。

* 2.如果当前节点的左子节点不为空,并且左子节点就是需要删除的节点,就将this.left=null;并且返回(结束递归)。

* 3.如果当前节点的右子节点不为空,并且右子节点就是需要删除的节点,将将this.right=null;并且返回(结束递归)。

* 4.如果第2步和第3步没有删除节点,那么就要向左子树进行递归删除。

* 5.如果第4步也没有删除节点,则应当向右子树进行递归删除。

*/

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

this.left=null;

return;

}

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

this.right=null;

return;

}

if (this.left!=null) {

this.left.delNo(no);

}

if (this.right!=null) {

this.right.delNo(no);

}

}

//前序遍历查找

publicHeroNode preOrderSearch(intno) {

HeroNode res = null;

preCount++;//这里必须放在this.no==no判断之前,才进行实际的比较

//若果找到,就返回

if (this.no==no) {

returnthis;

}

//没有找到,向左子树递归进行前序查找

if (this.left!=null) {

res = this.left.preOrderSearch(no);

}

//如果res != null就直接返回

if (res != null) {

returnres;

}

//如果左子树没有找打,向右子树进行前序查找

if (this.right!=null) {

res = this.right.preOrderSearch(no);

}

//如果找到就返回

if (res != null) {

returnres;

}

returnres;

}

//中序遍历查找

publicHeroNode infixOrderSearch(intno) {

HeroNode res = null;

if (this.left!=null) {

res = this.left.infixOrderSearch(no);

}

if (res != null) {

returnres;

}

infoxCount++;//这里必须放在this.no==no判断之前,才进行实际的比较

if (this.no==no) {

returnthis;

}

if (this.right!=null) {

res = this.right.infixOrderSearch(no);

}

if (res != null) {

returnres;

}

returnres;

}

//后序遍历查找

publicHeroNode postOrderSearch(intno) {

HeroNode res = null;

if (this.left!=null) {

res = this.left.postOrderSearch(no);

}

if (res != null) {

returnres;

}

if (this.right!=null) {

res = this.right.postOrderSearch(no);

}

if (res != null) {

returnres;

}

postCount++;//这里必须放在this.no==no判断之前,才进行实际的比较

if (this.no==no) {

returnthis;

}

returnres;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值