问题描述
可是总是输出第一个数字,我查了好久就是不懂哪里出了问题?下面是我的代码:Javacodeimportjava.util.*;publicclassBTree{privatestaticTreeNodetreeNode;publicBTree(TreeNodetreeNode){this.treeNode=treeNode;}publicTreeNodegetRootNode(){returntreeNode;}privatestaticclassTreeNode{privateintelement;//每个节点的值privateTreeNodeleft_node;//左节点privateTreeNoderight_node;//右节点publicTreeNode(){}publicTreeNode(intelement){this(element,null,null);}publicTreeNode(intelement,TreeNodelt,TreeNodert){this.element=element;this.left_node=lt;this.right_node=rt;}publicintgetElement(){returnelement;}privatevoidsetElement(intelement){this.element=element;}publicvoidsetLeft_node(TreeNodeleft_node){this.left_node=left_node;}publicTreeNodegetLeft_node(){returnleft_node;}publicvoidsetRight_node(TreeNoderight_node){this.right_node=right_node;}publicTreeNodegetRight_node(){returnright_node;}}publicstaticTreeNodecreateTree(TreeNoderoot){intelement;Scannersc=newScanner(System.in);element=sc.nextInt();if(element==-1)root=null;else{root=newTreeNode();root.setElement(element);createTree(root.getLeft_node());createTree(root.getRight_node());}returnroot;}//先根遍历树publicstaticvoidpreOrder(TreeNoderoot){if(root!=null){System.out.print(root.getElement());preOrder(root.getLeft_node());preOrder(root.getRight_node());}}//主函数入口publicstaticvoidmain(String[]args){System.out.println("请输入数据:");TreeNodetreeNode=newTreeNode();BTreetree=newBTree(createTree(treeNode));preOrder(tree.getRootNode());}}