4.9w+
社区成员
//*************主方法******
public static void main(String [] args) {
String preStr = "AB#DE###C##";
BiTree T = new BiTree(preStr);
BiTreeNode x = T.searchNode(T.getRoot(),'D');
System.out.println(x.data);
}
//出的问题:Exception in thread "main" java.lang.NullPointerException
//***************二叉树结点******************************
public class BiTreeNode {
public Object data;
public BiTreeNode lchild, rchild;
public BiTreeNode() {
this(null);
}
public BiTreeNode(Object data) {
this(data, null, null);
}
public BiTreeNode(Object data, BiTreeNode lchild, BiTreeNode rchild) {
this.data = data;
this.lchild = lchild;
this.rchild = rchild;
}
}
//**********二叉树*********************
public class BiTree {
public BiTreeNode root;
public BiTree() {
this.root = null;
}
public BiTree(BiTreeNode root) {
this.root = root;
}
private static int index = 0;
public BiTree(String preStr) {
char c = preStr.charAt(index++);
if (c != '#') {
root = new BiTreeNode(c);
root.lchild=new BiTree(preStr).root;
root.rchild=new BiTree(preStr).root;
} else
root = null;
}
public BiTreeNode getRoot() {
return root;
}
public void setRoot(BiTreeNode root) {
this.root = root;
}
public BiTreeNode searchNode(BiTreeNode T,Object x) {
if(T != null) {
if(T.equals(x))
return T;
else {
BiTreeNode lresult = searchNode (T.lchild,x);
return lresult != null ? lresult : searchNode(T.rchild,x);
}
}
return null;
}
}