package com.数据结构;
import javax.xml.soap.Node;
public class 二叉树 {
public static void main(String[] args){
Node01 n1 = new Node01("老大",1);
Node01 n2 = new Node01("老二",2);
Node01 n3 = new Node01("老三",3);
Node01 n4 = new Node01("老四",4);
Node01 n5 = new Node01("老五",5);
n1.setLift(n2);
n1.setRight(n3);
n3.setRight(n4);
n3.setLift(n5);
Bintree b1 = new Bintree();
b1.setRoot(n1);
b1.qianbian();
System.out.println();
b1.delNode(5);
b1.qianbian();
}
}
class Bintree{
private Node01 root;
public Node01 getRoot() {
return root;
}
public void setRoot(Node01 root) {
this.root = root;
}
public void qianbian(){
if(this.root!=null){
root.qianbian();
}else{
System.out.println("二叉树为空,无法遍历");
}
}
public void zhongbian(){
if(this.root!=null){
root.zhongbian();
}else{
System.out.println("二叉树为空,无法遍历");
}
}
public void houbian(){
if(this.root!=null){
root.houbian();
}else{
System.out.println("二叉树为空,无法遍历");
}
}
public void qianzhao(int no){
if(this.root!=null){
Node01 ans=root.qianzhao(no);
if(ans!=null){
System.out.println(ans);
}else{
System.out.println("不存在");
}
}else{
System.out.println("二叉树为空,无法查找");
}
}
public void zhongzhao(int no){
if(this.root!=null){
Node01 ans=root.zhongzhao(no);
if(ans!=null){
System.out.println(ans);
}else{
System.out.println("不存在");
}
}else{
System.out.println("二叉树为空,无法查找");
}
}
public void houzhao(int no){
if(this.root!=null){
Node01 ans=root.houzhao(no);
if(ans!=null){
System.out.println(ans);
}else{
System.out.println("不存在");
}
}else{
System.out.println("二叉树为空,无法查找");
}
}
public void delNode(int no){
if(this.root==null){
System.out.println("空树 无法删除");
}else{
if(this.root.getNo()==no){
root=null;
}else{
this.root.delNode(no);
}
}
}
}
class Node01{
private String name;
private int no ;
private Node01 lift;
private Node01 right;
public Node01() {
}
public Node01(String name, int no) {
this.name = name;
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Node01 getLift() {
return lift;
}
public void setLift(Node01 lift) {
this.lift = lift;
}
public Node01 getRight() {
return right;
}
public void setRight(Node01 right) {
this.right = right;
}
@Override
public String toString() {
return "Node01{" +
"name='" + name + '\'' +
", no=" + no +
'}';
}
public void qianbian(){
System.out.println(this);
if(this.lift!=null){
this.lift.qianbian();
}
if(this.right!=null){
this.right.qianbian();
}
}
public void zhongbian(){
if(this.lift!=null){
this.lift.zhongbian();
}
System.out.println(this);
if(this.right!=null){
this.right.zhongbian();
}
}
public void houbian(){
if(this.lift!=null){
this.lift.houbian();
}
if(this.right!=null){
this.right.houbian();
}
System.out.println(this);
}
public Node01 qianzhao(int no){
if(this.no==no){
return this;
}
Node01 temp = null;
if(this.lift!=null){
temp=this.lift.qianzhao(no);
}
if(temp!=null){
return temp;
}
if(this.right!=null){
temp=this.right.qianzhao(no);
}
return temp;
}
public Node01 zhongzhao(int no){
Node01 temp = null;
if(this.lift!=null){
temp = this.lift.zhongzhao(no);
}
if(temp!=null){
return temp;
}
if(this.no==no){
return this;
}
if(this.right!=null){
temp=this.right.zhongzhao(no);
}
return temp;
}
public Node01 houzhao(int no){
Node01 temp = null;
if(this.lift!=null){
temp=this.lift.houzhao(no);
}
if(temp!=null){
return temp;
}
if(this.right!=null){
temp=this.right.houzhao(no);
}
if(temp!=null){
return temp;
}
if(this.no==no){
return this;
}
return temp;
}
public void delNode(int no){
if(this.lift!=null){
if(this.lift.no==no){
this.lift=null;
return;
}
}
if(this.right!=null){
if(this.right.no==no){
this.right=null;
return ;
}
}
if(this.lift!=null){
this.lift.delNode(no);
}
if(this.right!=null){
this.right.delNode(no);
}
}
}