package textlist;
class Node1{//二叉树结点
public String data=null;
public Node1 lchild;
public Node1 rchild;
Node1(String data){//为了实例化p做准备
this.data=data;
}
}
public class tNode {
public Node1 fm;
tNode(){
this.fm=null;
}
public void creatBTNode(tNode b,String arr[]){
Node1 p=new Node1("");
Node1 f=new Node1("");
b=null;//初始为空
int size=arr.length;
Node1 st[]=new Node1[size];//栈
for(String ar:arr){
System.out.print(ar);//此处为了对比结果输出
}
System.out.println("");
String ch;
int j=0,k=0,top=0;//top做栈顶标志
ch=arr[j];
while(ch!=" "){ //扫描字符串
switch(ch){
case "(":k=1;top++;st[top]=p;break;//开始处理左孩子节点
case ")":top--;break;
case ",":k=2;break;//开始处理右孩子节点
default:
p=new Node1(ch);//这里好像必需重新实例化,请大佬解答?
p.lchild=null;
p.rchild=null;
if(fm==null){//确立根节点
fm=p;
}
else{
if(k==1){
st[top].lchild=p;//栈表示的节点是怎么连接到根节点的?
}
if(k==2){
st[top].rchild=p;
}
}
}
j++;
ch=arr[j];
}
}
public void dispbtree(Node1 b){//输出二叉树
if(b!=null){
System.out.print(b.data);
if(b.lchild!=null||b.rchild!=null){
System.out.print("(");
dispbtree(b.lchild);
if(b.rchild!=null)
{ System.out.print(",");
}
dispbtree(b.rchild);
System.out.print(")");
}
}
}
}