本文共 2987 字,大约阅读时间需要 9 分钟。
我的代码:
public HeroNode preOrderSearch(int no){ HeroNode temp = this; //同链表,根节点和头节点是不动的 //前序查找,现判断当前元素 if (temp.getHeroNo() == no){ return temp; } //如果不相等,再嘀咕比较左子节点是否相等 if (temp.left != null){ return temp.left.preOrderSearch(no); } if (temp.right != null){ return temp.right.preOrderSearch(no); } temp = null; return temp; }
教程代码:
public HeroNode preOrderSearch(int no){ //同链表,根节点和头节点是不动的 //前序查找,现判断当前元素 if (this.getHeroNo() == no){ return this; } HeroNode temp = this; //如果不相等,再嘀咕比较左子节点是否相等 if (temp.left != null){ temp = this.left.preOrderSearch(no); } if(temp != null){ return temp; } if (this.right != null){ temp = this.right.preOrderSearch(no); } return temp; }
BinaryTree类的方法public HeroNode preOrderSearch(int no){ //判断树是否为空 if(isEmpty()){ return null; }else{ return this.root.preOrderSearch(no); } }
总结分析:
代码实现:
HeroNode类: public HeroNode midOrderSearch(int no){ HeroNode temp = null; //首先判定当前节点左子节点是否为空 if (this.left != null){ temp = this.left.midOrderSearch(no); } if (temp != null){ return temp; } if (this.getHeroNo() == no){ return this; } if (this.right != null){ temp = this.right.midOrderSearch(no); } return temp; }
BinaryTree类:
public HeroNode midOrderSearch(int no){ if (isEmpty()){ return null; }else{ return this.root.midOrderSearch(no); } }
== 原理同上,不做详解==
BinaryTree类:
public HeroNode postOrderSearch(int no){ if (isEmpty()){ return null; }else{ return this.root.preOrderSearch(no); } }
HeroNode类:
public HeroNode postOrder(int no){ HeroNode temp = null; if (this.left != null){ temp = this.left.postOrder(no); } if (temp != null){ return temp; } if (this.right != null){ temp = this.right.postOrder(no); } if (temp != null){ return temp; } return this; }
转载地址:http://nqgpb.baihongyu.com/