有人可以帮我修复Java中的二叉树吗?

时间:2014-03-14 10:22:32

标签: java

用户必须向节点输入密钥,然后选择三个订单选项中的一个。他们可以按指示的数字来执行操作。我需要尽快使用此代码,因为决赛即将来临:(

import java.io.*;
import java.util.*;
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.key) {



                focusNode = focusNode.leftChild;



                if (focusNode == null) {


                    parent.leftChild = newNode;
                    return;
                }

            } else {

                focusNode = focusNode.rightChild;


                if (focusNode == null) {



                    parent.rightChild = newNode;
                    return;

                }

            }

        }
    }

}



public void inOrderTraverseTree(Node focusNode) {

    if (focusNode != null) {



        inOrderTraverseTree(focusNode.leftChild);



        System.out.println(focusNode);



        inOrderTraverseTree(focusNode.rightChild);

    }

}

public void preorderTraverseTree(Node focusNode) {

    if (focusNode != null) {

        System.out.println(focusNode);

        preorderTraverseTree(focusNode.leftChild);
        preorderTraverseTree(focusNode.rightChild);

    }

}

public void postOrderTraverseTree(Node focusNode) {

    if (focusNode != null) {

        postOrderTraverseTree(focusNode.leftChild);
        postOrderTraverseTree(focusNode.rightChild);

        System.out.println(focusNode);

    }

}

public Node findNode(int key) {



    Node focusNode = root;



    while (focusNode.key != key) {



        if (key < focusNode.key) {



            focusNode = focusNode.leftChild;

        } else {



            focusNode = focusNode.rightChild;

        }



        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);
}
}

2 个答案:

答案 0 :(得分:0)

主要问题是你肯定没有定义Node课程,至少不是你提供的代码,尽管你可能会想到。

如果您将该代码放入IDE(集成开发环境),例如Eclipse(我强烈建议您这样做,因为您不熟悉Java并且时间紧迫),您会发现它显示了很多错误,因为它可以找不到Node的定义。使用这些错误消息可以帮助您解决代码出现的问题。

提示:如果您的班级定义位于同一档案BinaryTree.java中,则会以如下方式开头:

class Node {
    /* Your code defining the node class */
}

您可能还想清理代码的格式,因为目前它很难阅读,这将使您从长远来看更难以调试并使代码按需运行。

这里的每个人都想确保你通过决赛,但你必须真正理解你的代码发生了什么,以及它为什么不起作用。没有捷径,有条不紊地通过它,检查你在SO或其他网站上得到的任何错误并通过你的决赛!

答案 1 :(得分:0)

节点类: - &gt;

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();
}
}

BinaryTree类: - &gt;

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);
}
}