Recursive Binary Search Tree not printing data in Java -


I am currently learning to write Java from C ++ to my programs. I am trying to print the data using recursive binary search tree, but not printing it

Here is my code:

  public class PersonRec {int bribe ; Person lchild; Person rchild; } Import java.util.Scanner; Public Square Tree {Private Personalized Route; Public tree () {root = null; } Add Public Zero () {int aBribe; Scanner scan = new scanner (System.in); System.out.println ("Contribute the person:"); ABribe = scan.nextInt (); Insert (root, abrib); } Insert public null (person root, int aibib) {if (root == null) {root = new PersonRec (); Root.rchild = null; Root.lchild = null; Root.bribe = aBribe; } And if (aBribe & lt; root.bribe) {Enter (root.lchild, aBribe); } Else {Insert (root.rchild, aBribe); }} Public Zero View () {if (root == null) {System.out.println ("tree is empty" + "\ n"); } Else DisplayTree (root); } Public Zero DisplayTree (personRec root) {if (root == null) Return; DisplayTree (root.lchild); System.out.println ("" + root.bribe); Println ("\ n"); DisplayTree (root.rchild); } Public static zero main (string rule []) {tree myList = new tree (); Int likes; Do {Scanner Scan = New Scanner (System.in); Println ("\ nMenu \ n"); Println ("============================== \ n \ n"); System.out.println ("1. Add student to waiting list \ n"); System.out.println ("2. Check the waiting list \ n"); System.out.println ("3. Exit Program \ n_"); System.out.println ("Please enter option:"); Likes = scan Switch (Preferred) {Case 1: myList.Add (); break; Case 2: myList.view (); break; }} While (option! = 3); }}   

When I type 1, then I insert the bribe amount example: 23 When I type 2 again in the menu my tree does not put, then it says "tree free In the method you inserted, the root is just a local within the method. The variable is And since the leaf level, the nonsensical passes, it has lost the connection with my list. You must create an example for lchild before proceeding to insert (root.lchild, aBribe).

  import java.util.Scanner; Square person's line {IT bribe; String name; Person lchild; Person rchild; } Public square tree {Private personalized root; Public tree () {root = null; } Add Public Zero () {Scanner Scan = New Scanner (System.in); System.out.println ("Enter the person's name:"); String name = scan. System.out.println ("Contribute the person:"); Int aBribe = scan.nextInt (); This.Add (name, aBribe); } Add Public Zero (String Name, Int Abibrate) {if (this.root == null) {root = this.createRecord (name, aBribe); } And (this.Interert (Route, Name, Abrib);}} record of creating a personal person (string name, int aribb) {Person Reich REC = new personrecord; Rec.bribe = aBribe; Rec.name = name ; Rec.rchild = null; Rec.lchild = null; return rec;} private zeros insert (person reic rick, string name, int aubib) {if (abbrib & lt; rec.bribe) {if (rec.lchild == null) {rec.lchild = this.createRecord (name, aBribe);} Else {Insert (rec.lchild, name, aBribe);}} and {if (Rick Arllille == empty) {rec.rchild = This.createRecord (name, aBribe);} Else {Insert (rec.rchild, name, aBribe);}}} Summary Public Zero View () {if (root == null} {System.out.println ("tree is empty" + "\ n");} Else DisplayTree (root);} Public Zero DisplayTree (personRec root) {if ( Root == null Return; DisplayTree (root.lchild); System.out.println ("+ + root.name +": "+ root.bribe); Println (" \ n "); DisplayTree (root.rchild) ;} Public Static Zero Main (String Array []) {tree myList = new tree (;); Int choice; Scan {scanner scan = new scanner (System.in); Println ("\ nMenu \ n"); Println ("============================== \ n \ n"); System.out.println ("1. Add student to waiting list \ n"); System.out.println ("2. Check the waiting list \ n"); System.out.println ("3. Exit Program \ n_"); System.out.println ("Please enter option:"); Likes = scan Switch (Preferred) {Case 1: myList.Add (); break; Case 2: myList.view (); break; }} While (option! = 3); }}    

Comments