简体   繁体   English

递归二进制搜索树不用Java打印数据

[英]Recursive Binary Search Tree not printing data in Java

I am currently learning java by writing my programs from C++ to Java. 我目前正在通过编写从C ++到Java的程序来学习java。 I am trying to print the data using recursive binary search tree, but its not printing 我试图使用递归二进制搜索树打印数据,但它不打印

Here is my code: 这是我的代码:

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

When I type 1, i insert a bribe amount example: 23 when i type 2 again fro the menu its not being inserted in my tree it says, "tree is empty" 当我输入1时,我插入一个贿赂金额示例:23当我再次从菜单中键入2时,它没有插入我的树中,它说“树是空的”

Thanks 谢谢

In you Insert method, root is just a local variable inside the method. 在Insert方法中,root只是方法中的局部变量。 And since at leaf level, null is passed, it has lost the connection with your myList. 因为在叶级别,传递null,它已失去与myList的连接。 You have to create instance for lchild before move on to Insert(root.lchild, aBribe). 在继续插入(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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM