二叉树的查找思路(以前序为例):先判断是否为空树,然后判断根节点是否为目标节点,如果不是的话,判断左子节点是否为空,如果非空,则向左子树遍历查找,如果没有找到,则向右子树进行相同的步骤。
二叉树的删除思路(此处是以删除非叶子节点就是直接删除这个子树):先判断是否为空树,然后判断根节点是否为目标节点,如果不是的话,判断左子节点是否为空且左子节点是否为删除目标,如果不是,向左子树进行递归,如果没有找到,则向右子树进行相同的步骤。
class binarytrees
{
node root;
public void setroot(node root)
{
this.root =root;
}
public node preordersearch(int no)
{
if(root!=null)
{
return root.preordersearch(no);
}
else
{
System.out.println("树为空,无法查找");
return null;
}
}
public node midordersearch(int no)
{
if(root!=null)
{
return root.midordersearch(no);
}
else
{
System.out.println("树为空,无法查找");
return null;
}
}
public node postordersearch(int no)
{
if(root!=null)
{
return root.postordersearch(no);
}
else
{
System.out.println("树为空,无法查找");
return null;
}
}
public void del(int no)
{
if(root!=null)
{
if(root.no==no)
{
root=null;
}
else
{
root.del(no);
}
}
else
{
System.out.println("树为空");
}
}
}
class node
{
node left;
node right;
int no;
String name;
public node( int no, String name) {
this.no = no;
this.name = name;
}
public node getLeft() {
return left;
}
public void setLeft(node left) {
this.left = left;
}
public node getRight() {
return right;
}
public void setRight(node right) {
this.right = right;
}
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;
}
@Override
public String toString() {
return "node [no=" + no + ", name=" + name + "]";
}
public node preordersearch(int no)
{
if(this.no==no)
{
return this;
}
node resnode=null;
if(this.left!=null)
{
resnode = this.left.preordersearch(no);
}
if(resnode !=null)
{
return resnode;
}
if(this.right!=null)
{
resnode=this.right.preordersearch(no);
}
if(resnode !=null)
{
return resnode;
}
return resnode;
}
public node midordersearch(int no)
{
node resnode =null;
if(this.left!=null)
{
resnode =this.left.midordersearch(no);
}
if(resnode !=null)
{
return resnode;
}
if(this.no==no)
{
return this;
}
if(this.right!=null)
{
resnode = this.right.midordersearch(no);
}
if(resnode!=null)
{
return resnode;
}
return resnode;
}
public node postordersearch(int no)
{
node resnode = null;
if(this.left!=null)
{
resnode =this.left.postordersearch(no);
}
if(resnode !=null)
{
return resnode;
}
if(this.right!=null)
{
resnode = this.right.postordersearch(no);
}
if(resnode!=null)
{
return resnode;
}
if(this.no==no)
{
return this;
}
return resnode;
}
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);
}
}
}
public class binarytreesearch {
public static void main(String[] args) {
binarytrees tree = new binarytrees();
node root = new node(1,"tom");
node node2 = new node(2,"lee");
node node3 = new node(3,"kitty");
node node4 = new node(4,"anni");
root.left=node2;
root.right=node3;
node3.right=node4;
tree.setroot(root);
tree.del(4);
node target = tree.preordersearch(4);
if(target !=null)
{
System.out.printf("找到了,no=%d name=%s",target.getNo(),target.getName());
}
else
{
System.out.println("没找到");
}
}
}