package com.atguigu.Tree;
/**
* 二叉树的前中后序遍历
*
*
* 1、宋江
* · ·
* · ·
* 2、吴用 3、卢俊义
* ·
* ·
* 4、林冲
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
HeroNode node = new HeroNode(1,"宋江");
HeroNode node2 = new HeroNode(2,"吴用");
HeroNode node3 = new HeroNode(3,"卢俊义");
HeroNode node4 = new HeroNode(4,"林冲");
tree.setRoot(node);
node.setLeft(node2);
node.setRight(node3);
node3.setRight(node4);
System.out.println("前序遍历结果为");//1,2,3,4
tree.preOrder();
System.out.println("中序遍历结果为");//2,1,3,4
tree.infixOrder();
System.out.println("后序遍历结果为");//2,4,3,1
tree.postOrder();
int no = 4;
System.out.println("查找编号为" + no + "的前序查找的结果为" + tree.preOrderSearch(no));
System.out.println("查找编号为" + no + "的中序查找的结果为" + tree.infixOrderSearch(no));
System.out.println("查找编号为" + no + "的后序查找的结果为" + tree.postOrderSearch(no));
int delno = 4;
System.out.println("删除节点" + delno + "前的二叉树为");
tree.preOrder();
Boolean result = tree.delHeroNode(delno);
System.out.println(result?"删除成功":"删除失败");
System.out.println("删除节点" + delno + "后的二叉树为");
tree.preOrder();
}
}
class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode() {
}
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "HeroNode{" +
"排名" + no +
"名字'" + 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 (this.left != null){
this.left.preOrder();
}
if (this.right != null){
this.right.preOrder();
}
}
//中序遍历
public void infixOrder(){
if (this.left != null){
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null){
this.right.infixOrder();
}
}
//后序遍历
public void postfixOrder(){
if (this.left != null){
this.left.postfixOrder();
}
if (this.right != null){
this.right.postfixOrder();
}
System.out.println(this);
}
//前序遍历查找
public HeroNode preOederSearch(int no){
System.out.println("前序比较次数");
if (this.no == no){
return this;
}
HeroNode resHeronode = null;
if (this.left != null){
resHeronode = this.left.preOederSearch(no);
}
if (resHeronode != null){
return resHeronode;
}
if (this.right != null){
resHeronode = this.right.preOederSearch(no);
}
return resHeronode;
}
//中序遍历查找
public HeroNode infixOederSearch(int no){
HeroNode resHeronode = null;
if (this.left != null){
resHeronode = this.left.infixOederSearch(no);
}
if (resHeronode != null){
return resHeronode;
}
System.out.println("中序比较次数");
if (this.no == no){
return this;
}
if (this.right != null){
resHeronode = this.right.infixOederSearch(no);
}
return resHeronode;
}
//后序遍历查找
public HeroNode postOederSearch(int no){
HeroNode resHeronode = null;
if (this.left != null){
resHeronode = this.left.postOederSearch(no);
}
if (resHeronode != null){
return resHeronode;
}
if (this.right != null){
resHeronode = this.right.postOederSearch(no);
}
if (resHeronode != null){
return resHeronode;
}
System.out.println("后序比较次数");
return this.no == no? this: null;
}
//删除节点
public boolean delHeroNode(int no){
boolean flag = false;
if (this.left != null && this.left.getNo() == no ){
this.left = null;
return true;
}
if (this.right != null && this.right.getNo() == no ){
this.right = null;
return true;
}
if (this.left != null){
flag = this.left.delHeroNode(no);
}
if (this.right != null){
flag = this.right.delHeroNode(no);
}
return flag;
}
}
class BinaryTree{
private HeroNode root;
public BinaryTree() {
}
public void setRoot(HeroNode root) {
this.root = root;
}
public BinaryTree(HeroNode root) {
this.root = root;
}
public void preOrder(){
root.preOrder();
}
public void infixOrder(){
root.infixOrder();
}
public void postOrder(){
root.postfixOrder();
}
public HeroNode preOrderSearch(int no){
return root.preOederSearch(no);
}
public HeroNode infixOrderSearch(int no) {
return root.infixOederSearch(no);
}
public HeroNode postOrderSearch(int no) {
return root.postOederSearch(no);
}
public boolean delHeroNode(int no){
if (root.getNo() == no){
root = null;
return true;
}else {
return root.delHeroNode(no);
}
}
}
二叉树的前中后序遍历java实现
最新推荐文章于 2024-11-09 12:43:52 发布