51,409
社区成员
发帖
与我相关
我的任务
分享
/**
* 利用前序遍历的方式创建
*/
static void CreateBitTree(Binary_Tree tree){
//输入value
Scanner scanner = new Scanner(System.in);
String c;
c = scanner.next();
// 根据输入的值来判断插入的节点 输入#为null节点
if(c.equals("#")){
tree = null;
}else{
tree.setValue(c);
Binary_Tree lefTree = new Binary_Tree();
c = scanner.next();
lefTree.setValue(c);
tree.setLeftChild(lefTree);
//CreateBitTree(tree.getLeftChild());
Binary_Tree rightTree = new Binary_Tree();
c = scanner.next();
rightTree.setValue(c);
tree.setRightChild(rightTree);
//CreateBitTree(tree.getRightChild());
}
}
public static void main(String[] args) {
Binary_Tree binary_tree = new Binary_Tree();
Binary_Tree.CreateBitTree(binary_tree);
System.out.println(binary_tree.getValue());
System.out.println(binary_tree.getLeftChild().getValue());
System.out.println(binary_tree.getRightChild().getValue());
System.out.println(11111);
}