【问题标题】:Recursive Binary Search Tree not printing data in Java递归二叉搜索树不在Java中打印数据
【发布时间】:2012-11-07 14:24:51
【问题描述】:

我目前正在通过将我的程序从 C++ 编写到 Java 来学习 Java。 我正在尝试使用递归二叉搜索树打印数据,但它没有打印

这是我的代码:

public class PersonRec {
    int bribe;
    PersonRec lchild;
    PersonRec rchild;   
}

import java.util.Scanner;

public class Tree {
    private PersonRec root;

    public Tree()
    {
        root = null;
    }

    public void Add()
    {       
        int aBribe;
        Scanner scan = new Scanner(System.in);  

        System.out.println("Enter person's contribution: ");
        aBribe = scan.nextInt();

        Insert(root, aBribe);       
    }

    public void Insert(PersonRec root, int aBribe)
    {
        if(root == null)
        {
            root = new PersonRec();
            root.rchild = null;
            root.lchild = null;

            root.bribe = aBribe;
        }       
        else if(aBribe < root.bribe)
        {
            Insert(root.lchild, aBribe);
        }
        else
        {
            Insert(root.rchild, aBribe);
        }
    }

    public void view()
    {               
        if(root == null)
        {
            System.out.println("Tree is empty" + "\n");
        }
        else
            DisplayTree(root);
    }

    public void DisplayTree(PersonRec root)
    {               
        if(root == null)
            return;

        DisplayTree(root.lchild);
        System.out.println(" " + root.bribe);
        System.out.println("\n");   
        DisplayTree(root.rchild);

    }

    public static void main(String args[])
    {   
        Tree myList = new Tree();       
        int choice;     

        do
        {
            Scanner scan = new Scanner(System.in);

            System.out.println("\nMenu\n");
            System.out.println("==============================\n\n");
            System.out.println("1. Add student to waiting list\n");
            System.out.println("2. View waiting list\n");
            System.out.println("3. Exit program \n_");
            System.out.println("Please enter choice: ");
            choice = scan.nextInt();

            switch(choice)
            {
                case 1: myList.Add();
                break;

                case 2: myList.view();
                break;          

            }           
        }
        while(choice != 3);         
    }   
}

当我输入 1 时,我插入贿赂金额示例:23 当我再次从菜单中输入 2 时,它没有被插入到我的树中,它说“树是空的”

谢谢

【问题讨论】:

  • 这个aBribe &lt; root.bribe 是怎么做的?这甚至行得通吗?
  • 那句话有错吗> aBribe
  • 有几个想法可以帮助您避免很多挫败感:首先,使用 IDE。 Eclipse 和 Netbeans 是两个最流行的选择。 IDE 可以执行比语言更严格的规则。例如,您可以将其配置为将对方法参数的重新分配标记为警告或错误。 (这会对你有所帮助。)你会得到一个调试器。我的另一个建议是学习 Java naming conventions,特别是方法名称应该始终以小​​写字符开头。
  • 我使用 Eclipse 并了解 java 命名约定。我不知道你在说什么
  • 我说的是Add()DisplayTree()而不是add()displayTree()的方法。

标签: java data-structures recursion


【解决方案1】:

在你的 Insert 方法中,root 只是方法中的一个局部变量。 并且由于在叶级别传递了 null,因此它失去了与您的 myList 的连接。 在继续插入(root.lchild,aBribe)之前,您必须为 lchild 创建实例。

import java.util.Scanner;

class PersonRec {
    int bribe;
    String name;
    PersonRec lchild;
    PersonRec rchild;
}

public class Tree {
    private PersonRec root;

    public Tree() {
        root = null;
    }

    public void Add() {
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter person's name: ");
        String name = scan.next();
        System.out.println("Enter person's contribution: ");
        int aBribe = scan.nextInt();

        this.Add(name, aBribe);
    }

    public void Add(String name, int aBribe) {
        if (this.root == null) {
            root = this.createRecord(name, aBribe);
        } else {
            this.Insert(root, name, aBribe);
        }
    }

    private PersonRec createRecord(String name, int aBribe) {
        PersonRec rec = new PersonRec();
        rec.bribe = aBribe;
        rec.name = name;
        rec.rchild = null;
        rec.lchild = null;
        return rec;
    }

    private void Insert(PersonRec rec, String name, int aBribe) {
        if (aBribe < rec.bribe) {
            if (rec.lchild == null) {
                rec.lchild = this.createRecord(name, aBribe);
            } else {
                Insert(rec.lchild, name, aBribe);
            }
        } else {
            if (rec.rchild == null) {
                rec.rchild = this.createRecord(name, aBribe);
            } else {
                Insert(rec.rchild, name, aBribe);
            }
        }
    }

    public void view() {
        if (root == null) {
            System.out.println("Tree is empty" + "\n");
        } else
            DisplayTree(root);
    }

    public void DisplayTree(PersonRec root) {
        if (root == null)
            return;

        DisplayTree(root.lchild);
        System.out.println(" " + root.name + ":" + root.bribe);
        System.out.println("\n");
        DisplayTree(root.rchild);

    }

    public static void main(String args[]) {
        Tree myList = new Tree();
        int choice;

        do {
            Scanner scan = new Scanner(System.in);

            System.out.println("\nMenu\n");
            System.out.println("==============================\n\n");
            System.out.println("1. Add student to waiting list\n");
            System.out.println("2. View waiting list\n");
            System.out.println("3. Exit program \n_");
            System.out.println("Please enter choice: ");
            choice = scan.nextInt();

            switch (choice) {
            case 1:
                myList.Add();
                break;

            case 2:
                myList.view();
                break;

            }
        } while (choice != 3);
    }
}

【讨论】:

  • 其实真正的问题是root = new PersonRec();替换了局部变量,应该是this.root = new PersonRec();
  • 或者更准确地说,是问题之一。 :)
  • 如果你这样做.root = new PersonRec();,你的树中将永远只有一项。
  • 谢谢 biziclop。我更改为 this.root = new PersonRec() 和其余的变量,它起作用了。
  • 我运行了程序,它可以工作,但是当我向树中添加新名称时,以前的名称和贿赂被新名称取代。你知道问题出在哪里吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-13
  • 2011-07-19
  • 1970-01-01
  • 2014-01-02
  • 2021-09-01
  • 2013-04-14
相关资源
最近更新 更多