Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有人能帮我用Java修复这个二叉树吗?_Java - Fatal编程技术网

有人能帮我用Java修复这个二叉树吗?

有人能帮我用Java修复这个二叉树吗?,java,Java,用户必须输入节点的键,并从三个订单选项中选择一个。他们可以按指示的数字执行操作。我需要尽快使用此代码,因为期末考试即将来临:( import java.io.*; 导入java.util.*; 公共类二叉树{ 公共静态int选项,列表长度; 节根; public void addNode(int键,字符串名称){ Node newNode=新节点(键、名称); if(root==null){ 根=新节点; }否则{ 节点focusNode=root; 节点父节点; while(true){ 父节

用户必须输入节点的键,并从三个订单选项中选择一个。他们可以按指示的数字执行操作。我需要尽快使用此代码,因为期末考试即将来临:(

import java.io.*;
导入java.util.*;
公共类二叉树{
公共静态int选项,列表长度;
节根;
public void addNode(int键,字符串名称){
Node newNode=新节点(键、名称);
if(root==null){
根=新节点;
}否则{
节点focusNode=root;
节点父节点;
while(true){
父节点=焦点节点;
if(键//对于(int i=0;i而言,主要问题是您肯定没有定义
节点
类,至少没有从您提供的代码中定义,不管您怎么想

如果您将该代码放入Eclipse等IDE(集成开发环境)(我强烈建议您这样做,因为您是Java新手,而且时间很短),您将看到它显示了很多错误,因为它找不到您对
节点的定义

提示:如果您的类定义位于同一个文件
BinaryTree.java
,则它的开头如下:

class Node {
    /* Your code defining the node class */
}
您可能还希望清理代码的格式,因为目前的情况很难阅读,这将使您从长远来看更难调试代码并使其按需工作

这里的每个人都想确保你通过期末考试,但你必须真正了解你的代码是怎么回事以及为什么不起作用。没有捷径,要有条不紊地完成它,检查你在SO或其他网站上遇到的任何错误,然后通过期末考试!

节点类:->

package com.shi.tree;

public class Node {
private int key;
private String name;
private Node leftChild,rightChild;
public Node(int key, String name) {
    this.key = key;
    this.name=name;
    // TODO Auto-generated constructor stub

}
public int getKey() {
    return key;
}
public void setKey(int key) {
    this.key = key;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Node getLeftChild() {
    return leftChild;
}
public void setLeftChild(Node leftChild) {
    this.leftChild = leftChild;
}
public Node getRightChild() {
    return rightChild;
}
public void setRightChild(Node rightChild) {
    this.rightChild = rightChild;
}
@Override
public String toString() {
System.out.println("key :"+this.getKey()+" Name :"+this.getName());
// TODO Auto-generated method stub
return super.toString();
}
}
package com.shi.tree;

import java.io.*;
import java.util.*;
import com.shi.tree.Node;
public class BinaryTree {

public static int choice,listLength;



Node root;

public void addNode(int key, String name) {



Node newNode = new Node(key, name);



if (root == null) {

    root = newNode;

} else {




    Node focusNode = root;



    Node parent;

    while (true) {




        parent = focusNode;




        if (key < focusNode.getKey()) {



            focusNode = focusNode.getLeftChild();



            if (focusNode == null) {


                parent.setLeftChild(newNode);
                return;
            }

        } else {

            focusNode = focusNode.getRightChild();


            if (focusNode == null) {



                parent.setRightChild(newNode);
                return;

            }

        }

    }
}

}



public void inOrderTraverseTree(Node focusNode) {

if (focusNode != null) {



    inOrderTraverseTree(focusNode.getLeftChild());



    System.out.println(focusNode.toString());



    inOrderTraverseTree(focusNode.getRightChild());

}

}

public void preorderTraverseTree(Node focusNode) {

if (focusNode != null) {

    System.out.println(focusNode);

    preorderTraverseTree(focusNode.getLeftChild());
    preorderTraverseTree(focusNode.getRightChild());

}

}

public void postOrderTraverseTree(Node focusNode) {

if (focusNode != null) {

    postOrderTraverseTree(focusNode.getLeftChild());
    postOrderTraverseTree(focusNode.getRightChild());

    System.out.println(focusNode);

}

}

public Node findNode(int key) {



Node focusNode = root;



while (focusNode.getKey() != key) {



    if (key < focusNode.getKey()) {



        focusNode = focusNode.getLeftChild();

    } else {



        focusNode = focusNode.getRightChild();

    }



    if (focusNode == null)
        return null;

}

return focusNode;

}

 public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String x;
int y;

BinaryTree theTree = new BinaryTree();





 theTree.inOrderTraverseTree(theTree.root);









do
{
    try
        {
System.out.println("Choose: ");
System.out.println("[1]Insert ");
System.out.println("[2]Delete ");
System.out.println("[3]Display ");
System.out.println("[4]Search");
System.out.println("[5]Exit");
System.out.println("====================");
choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1 :    System.out.println("INSERT");
            System.out.println("Enter a key: ");
            y = Integer.parseInt(br.readLine());


            System.out.println("Enter a name: ");
            x = br.readLine();
            theTree.addNode(y,x);
            System.out.println("Tree: " + theTree.findNode(y));
            break;

case 2 :    System.out.println("DELETE");
            System.out.println("Enter the node to remove: ");
            x = br.readLine();

case 3 :    System.out.println("DISPLAY");

            System.out.println("In Order");
            theTree.inOrderTraverseTree(theTree.root);
            System.out.println("");

            System.out.println("PreOrder");
            theTree.preorderTraverseTree(theTree.root);
            System.out.println("");

            System.out.println("PostOrder");
            theTree.postOrderTraverseTree(theTree.root);
            System.out.println("");




case 4 :    System.out.println("SEARCH");
            System.out.println("Enter a key to find: ");
            int z = Integer.parseInt(br.readLine());
            //for(int i = 0; i<listLength;i++)
            //{

                if(theTree.findNode(z)!= null){

System.out.println("Found it! : " +     theTree.findNode(z) );

                }
                else{

                      System.out.println("ERROR: Not found!");

                }
                    break;




case 5 :    System.out.println("Bye");
            break;

  default : System.out.println("Try Again");
            break;
}
    }catch(NumberFormatException e)
    {
        System.out.println("Try again");
    }

}while(choice != 5);
}
}
二进制树类:->

package com.shi.tree;

public class Node {
private int key;
private String name;
private Node leftChild,rightChild;
public Node(int key, String name) {
    this.key = key;
    this.name=name;
    // TODO Auto-generated constructor stub

}
public int getKey() {
    return key;
}
public void setKey(int key) {
    this.key = key;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Node getLeftChild() {
    return leftChild;
}
public void setLeftChild(Node leftChild) {
    this.leftChild = leftChild;
}
public Node getRightChild() {
    return rightChild;
}
public void setRightChild(Node rightChild) {
    this.rightChild = rightChild;
}
@Override
public String toString() {
System.out.println("key :"+this.getKey()+" Name :"+this.getName());
// TODO Auto-generated method stub
return super.toString();
}
}
package com.shi.tree;

import java.io.*;
import java.util.*;
import com.shi.tree.Node;
public class BinaryTree {

public static int choice,listLength;



Node root;

public void addNode(int key, String name) {



Node newNode = new Node(key, name);



if (root == null) {

    root = newNode;

} else {




    Node focusNode = root;



    Node parent;

    while (true) {




        parent = focusNode;




        if (key < focusNode.getKey()) {



            focusNode = focusNode.getLeftChild();



            if (focusNode == null) {


                parent.setLeftChild(newNode);
                return;
            }

        } else {

            focusNode = focusNode.getRightChild();


            if (focusNode == null) {



                parent.setRightChild(newNode);
                return;

            }

        }

    }
}

}



public void inOrderTraverseTree(Node focusNode) {

if (focusNode != null) {



    inOrderTraverseTree(focusNode.getLeftChild());



    System.out.println(focusNode.toString());



    inOrderTraverseTree(focusNode.getRightChild());

}

}

public void preorderTraverseTree(Node focusNode) {

if (focusNode != null) {

    System.out.println(focusNode);

    preorderTraverseTree(focusNode.getLeftChild());
    preorderTraverseTree(focusNode.getRightChild());

}

}

public void postOrderTraverseTree(Node focusNode) {

if (focusNode != null) {

    postOrderTraverseTree(focusNode.getLeftChild());
    postOrderTraverseTree(focusNode.getRightChild());

    System.out.println(focusNode);

}

}

public Node findNode(int key) {



Node focusNode = root;



while (focusNode.getKey() != key) {



    if (key < focusNode.getKey()) {



        focusNode = focusNode.getLeftChild();

    } else {



        focusNode = focusNode.getRightChild();

    }



    if (focusNode == null)
        return null;

}

return focusNode;

}

 public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String x;
int y;

BinaryTree theTree = new BinaryTree();





 theTree.inOrderTraverseTree(theTree.root);









do
{
    try
        {
System.out.println("Choose: ");
System.out.println("[1]Insert ");
System.out.println("[2]Delete ");
System.out.println("[3]Display ");
System.out.println("[4]Search");
System.out.println("[5]Exit");
System.out.println("====================");
choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1 :    System.out.println("INSERT");
            System.out.println("Enter a key: ");
            y = Integer.parseInt(br.readLine());


            System.out.println("Enter a name: ");
            x = br.readLine();
            theTree.addNode(y,x);
            System.out.println("Tree: " + theTree.findNode(y));
            break;

case 2 :    System.out.println("DELETE");
            System.out.println("Enter the node to remove: ");
            x = br.readLine();

case 3 :    System.out.println("DISPLAY");

            System.out.println("In Order");
            theTree.inOrderTraverseTree(theTree.root);
            System.out.println("");

            System.out.println("PreOrder");
            theTree.preorderTraverseTree(theTree.root);
            System.out.println("");

            System.out.println("PostOrder");
            theTree.postOrderTraverseTree(theTree.root);
            System.out.println("");




case 4 :    System.out.println("SEARCH");
            System.out.println("Enter a key to find: ");
            int z = Integer.parseInt(br.readLine());
            //for(int i = 0; i<listLength;i++)
            //{

                if(theTree.findNode(z)!= null){

System.out.println("Found it! : " +     theTree.findNode(z) );

                }
                else{

                      System.out.println("ERROR: Not found!");

                }
                    break;




case 5 :    System.out.println("Bye");
            break;

  default : System.out.println("Try Again");
            break;
}
    }catch(NumberFormatException e)
    {
        System.out.println("Try again");
    }

}while(choice != 5);
}
}
package com.shi.tree;
导入java.io.*;
导入java.util.*;
导入com.shi.tree.Node;
公共类二叉树{
公共静态int选项,列表长度;
节根;
public void addNode(int键,字符串名称){
Node newNode=新节点(键、名称);
if(root==null){
根=新节点;
}否则{
节点focusNode=root;
节点父节点;
while(true){
父节点=焦点节点;
if(key