/**
* @author Drug
* @create 2020-05-01 15:50
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
HeroNode heroNode = new HeroNode(1, "宋江");
HeroNode heroNode2 = new HeroNode(2, "吴用");
HeroNode heroNode3 = new HeroNode(3, "卢俊义");
HeroNode heroNode4 = new HeroNode(4, "林冲");
HeroNode heroNode5 = new HeroNode(5, "关胜");
heroNode.setLeft(heroNode2);
heroNode.setRight(heroNode3);
heroNode3.setLeft(heroNode5);
heroNode3.setRight(heroNode4);
binaryTree.setRoot(heroNode);
//前序遍历//12345
System.out.println("前序遍历");
binaryTree.preOrder();
//中序遍历
// System.out.println("中序遍历");
// binaryTree.infixOrder();
//后序遍历
// System.out.println("后序遍历");
// binaryTree.postOrder();
//删除子树
System.out.println("======================");
binaryTree.deleteNode(3);
binaryTree.preOrder();
//查找
// System.out.println("前序查找");
// HeroNode resNode = binaryTree.preSearch(5);
// System.out.println(resNode);
// System.out.println("中序查找");
// HeroNode resNode2 = binaryTree.infixSearch(5);
// System.out.println(resNode2);
// System.out.println("后序查找");
// HeroNode resNode3 = binaryTree.postSearch(5);
// System.out.println(resNode3);
}
}
class BinaryTree {
//根节点
private HeroNode root;
public void setRoot(HeroNode root) {
this.root = root;
}
/**
* 前序遍历
*/
public void preOrder() {
root.preOrder();
}
/**
* 中序遍历
*/
public void infixOrder() {
root.infixOrder();
}
/**
* 后序遍历
*/
public void postOrder() {
root.postOrder();
}
/**
* 前序查找
*/
public HeroNode preSearch(int no) {
if (root != null) {
return root.preSearch(no);
} else {
return null;
}
}
/**
* 中序查找
*/
public HeroNode infixSearch(int no) {
if (root != null) {
return root.infixSearch(no);
} else {
return null;
}
}
/**
* 后序查找
*/
public HeroNode postSearch(int no) {
if (root != null) {
return root.postSearch(no);
} else {
return null;
}
}
/**
* 删除子树
* @param no
*/
public void deleteNode(int no){
if(root == null){
System.out.println("要删除的树为空树");
}else{
if(root.getNo() == no){
root = null;
return;
}else{
root.deleteNode(no);
}
}
}
}
class HeroNode {
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
public HeroNode() {
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", 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 HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
/**
* 前序遍历
*/
public void preOrder() {
System.out.println(this);
if (left != null) {
this.left.preOrder();
}
if (right != null) {
this.right.preOrder();
}
}
/**
* 中序遍历
*/
public void infixOrder() {
if (left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (right != null) {
this.right.infixOrder();
}
}
/**
* 后序遍历
*/
public void postOrder() {
if (left != null) {
this.left.postOrder();
}
if (right != null) {
this.right.postOrder();
}
System.out.println(this);
}
/**
* 前序查找
*
* @return
*/
public HeroNode preSearch(int no) {
// System.out.println("进入前续查找");
if (this.no == no) {
return this;
}
HeroNode heroNode = null;
//查询左子树
if (this.left != null) {
heroNode = this.left.preSearch(no);
}
//左子树找到返回
if (heroNode != null) {
return heroNode;
}
//查询右子树
if (this.right != null) {
heroNode = this.right.preSearch(no);
}
return heroNode;
}
/**
* 中序查找
*
* @return
*/
public HeroNode infixSearch(int no) {
HeroNode heroNode = null;
//查询左子树
if (this.left != null) {
heroNode = this.left.infixSearch(no);
}
//左子树找到返回
if (heroNode != null) {
return heroNode;
}
// System.out.println("进入中续查找");
//查询自己
if (this.no == no) {
return this;
}
//查询右子树
if (this.right != null) {
heroNode = this.right.infixSearch(no);
}
return heroNode;
}
/**
* 后序查找
*
* @return
*/
public HeroNode postSearch(int no) {
HeroNode heroNode = null;
//查询左子树
if (this.left != null) {
heroNode = this.left.postSearch(no);
}
//左子树找到返回
if (heroNode != null) {
return heroNode;
}
//查询右子树
if (this.right != null) {
heroNode = this.right.postSearch(no);
}
if (heroNode != null) {
return heroNode;
}
// System.out.println("进入后续查找");
//查询自己
if (this.no == no) {
return this;
}
return heroNode;
}
/**
* 删除节点子树
* @param no
*/
public void deleteNode(int no) {
//判断左子树
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.deleteNode(no);
}
//递归右子树
if(this.right != null){
this.right.deleteNode(no);
}
}
}
java二叉树前中后序遍历添加删除
最新推荐文章于 2024-10-17 19:11:48 发布