//定义树
class BinaryTree{
private Hero root;
public void setRoot(Hero root) {
this.root = root;
}
//前序遍历
public void perOrder(){
if(this.root!=null){
this.root.perOrder();
}
else {
System.out.println("当前树为空");
}
}
//中序遍历
public void infixOrder(){
if(this.root!=null){
this.root.infixOrder();
}
else {
System.out.println("当前树为空");
}
}
//后序遍历
public void postOrder(){
if(this.root!=null){
this.root.postOrder();
}
else {
System.out.println("当前树为空");
}
}
//前序查找
public Hero perOrderSearch(int no){
if(root==null){
return null;
}else{
return root.perOrderSearch(no);
}
}
//中序查找
public Hero infixOrderSearch(int no){
if(root==null){
return null;
}else{
return root.infixOrderSearch(no);
}
}
//后序查找
public Hero postOrderSearch(int no){
if(root==null){
return null;
}else{
return root.postOrderSearch(no);
}
}
//删除节点
public void delNo(int no){
if(root!=null){
if(root.getNo() == no){
root = null;
}else {
root.del(no);
}
}else {
System.out.println("空树无法删除");
}
}
}
//定义节点
class Hero{
private int no;
private String name;
private Hero left;
private Hero right;
public Hero(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 Hero getLeft() {
return left;
}
public void setLeft(Hero left) {
this.left = left;
}
public Hero getRight() {
return right;
}
public void setRight(Hero right) {
this.right = right;
}
@Override
public String toString() {
return "Hero{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
//前序遍历
public void perOrder(){
System.out.println(this);//先输出-节点
//递归向左子树遍历
if(this.left!=null){
this.left.perOrder();
}
//递归向右子树遍历
if(this.right!=null){
this.right.perOrder();
}
}
//中序遍历
public void infixOrder(){
//递归向左子树遍历
if(this.left!=null){
this.left.infixOrder();
}
System.out.println(this);//输出当前节点
//递归向右子树遍历
if(this.right!=null){
this.right.infixOrder();
}
}
//后续遍历
public void postOrder(){
//递归向左子树遍历
if(this.left!=null){
this.left.postOrder();
}
//递归向右子树遍历
if(this.right!=null){
this.right.postOrder();
}
System.out.println(this);//输出当前节点
}
//前序遍历查找
public Hero perOrderSearch(int no){
if(this.no == no){
return this;
}
Hero hero = null;
if(this.left != null){
hero = this.left.perOrderSearch(no);
}
if(hero!=null){
//说明左子树上找到了
return hero;
}
if(this.right!=null){
hero = this.right.perOrderSearch(no);
}
return hero;
}
//中序遍历查找
public Hero infixOrderSearch(int no){
Hero hero = null;
if(this.left != null){
hero = this.left.infixOrderSearch(no);
}
if(hero!=null){
return hero;
}
//如果找到就返回,如果没有找到,就和当前节点比较
if(this.no == no){
return this;
}
//如果左节点和当前节点都不是 则向右递归
if(this.right!=null){
hero = this.right.infixOrderSearch(no);
}
return hero;
}
//后序查找
public Hero postOrderSearch(int no){
Hero hero = null;
if(this.left != null){
hero = this.left.postOrderSearch(no);
}
if(hero != null){
return hero;
}
if(this.right!=null){
hero = this.right.postOrderSearch(no);
}
if(hero!=null){
return hero;
}
if(this.no == no){
return this;
}
return hero;
}
//删除节点
public void del(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.del(no);
}
if(this.right!=null){
this.right.del(no);
}
}
}
二叉树的遍历、查找、节点删除
最新推荐文章于 2024-07-06 22:34:06 发布