0

私は現在、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 を再度入力すると、ツリーに挿入されず、「ツリーは空です」と表示されます。

ありがとう

4

1 に答える 1

2

Insert メソッドでは、ルートはメソッド内の単なるローカル変数です。リーフ レベルでは null が渡されるため、myList との接続が失われます。Insert(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);
    }
}
于 2012-07-09T23:00:21.083 に答える