java - Trouble with sorting in a Binary Search tree -


i've been having trouble making binary search tree work. idea person put in , sorted based on name.

the class using person is:

package tree;  public class person {     private int age;     private string name;     private string gender;      public person( string name, string gender,int age) {         this.age = age;         this.name = name;         this.gender = gender;     }      public int getage() {         return age;     }      public void setage(int age) {         this.age = age;     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public string getgender() {         return gender;     }      public void setgender(string gender) {         this.gender = gender;     }      @override     public string tostring() {         return "person [age=" + age + ", name=" + name + ", gender="                 + gender + "]";     } } 

and search tree is:

package tree;  public class binarysearchperson {  private boolean empty; private person person; private static binarysearchperson left; private static binarysearchperson right;  public binarysearchperson(person person, binarysearchperson left,         binarysearchperson right) {     this.empty = false;     this.person = person;     this.left = left;     this.right = right; }  public binarysearchperson() {     this.empty = true; }  public boolean isempty() {     return empty; }  public person getperson() {     if (isempty()) {         throw new illegalstateexception(                 "trying access root of empty tree");     }     return person; }  public void setperson(person person) {     this.person = person; }   public binarysearchperson getleft() {     if (isempty()) {         throw new illegalstateexception(                                         "trying access subtree of empty tree");     }     return left; }   public void setleft(binarysearchperson left) {     this.left = left; }   /**  * gets right subtree of node  */ public binarysearchperson getright() {     if (isempty()) {         throw new illegalstateexception(                                         "trying access subtree of empty tree");     }     return right; }   public void setright(binarysearchperson right) {     this.right = right; }    public static binarysearchperson insert(person person, binarysearchperson bt){     int n = person.getname().compareto(bt.person.getname());       if (n<0){         if(bt.getleft().isempty() == true){             bt.setleft(new binarysearchperson(person,new binarysearchperson(),new binarysearchperson()));             return bt;         }         else{             return insert(person, bt.getleft());          }     }      if (n>0){         if(bt.getright().isempty() == true){             bt.setright(new binarysearchperson(person,new binarysearchperson(),new binarysearchperson()));             return bt;         }         else{             return insert(person, bt.getright());         }     }     else return bt;   }    } 

the problem getting sorting method called insert, near bottom. reason makes endless number of branches left or right depending on name going sorted. can't see going wrong here great.

remove static modifier from

private static binarysearchperson left; private static binarysearchperson right;

each node in tree should have own left , right.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -