使用具有键和名称的每个节点创建BST

时间:2014-03-20 13:42:27

标签: java

我的任务是使用姓名和姓氏 toSearchIn.txt  为了 理货和 打印, 每个名字的流行度和 Bob的朋友列表中的姓氏 。 维护两个单独的BST(一个用于名称,一个用于姓氏)。 名称BST中的节点存储名称,num的计数器 该名称的出现次数,密钥(名称的字符总和)以及对其左右儿童的引用 。 姓氏BST中的节点存储姓氏,该计数的出现次数为计数器 姓氏,一把钥匙(一个人的总和 (姓氏)及其左右儿童的引用。 产量 : 遍历名称树以打印出每个键,名称和该名称的出现次数。遍历姓氏树邮政订单,以打印出该姓氏的密钥,姓氏和出现次数。

所以我有3个课程,我的代码应该打印所有的名字,他们的密钥和流行度,但它只打印4个名字。 任何人都可以帮助我吗?

这是我的课程:

import java.util.*;
import java.io.*;
public class BSTMain{
    public static ArrayList<String> names;
    public static ArrayList<String> surNames;
    public static  ArrayList<Integer> keys;
    public static ArrayList<Integer> keysNames;
    public static ArrayList<Integer> keysSurnames;
    public static void main(String args[]){
       keysNames = new ArrayList<Integer>();
       keysSurnames = new ArrayList<Integer>();
       BSTMethods treeName = new BSTMethods();
       BSTMethods treeSurname = new BSTMethods();
       BSTNode node = new BSTNode();
       names = new ArrayList<String>();
       surNames = new ArrayList<String>();
                //open textfiel and read names
        try {
            BufferedReader reader = new BufferedReader(new FileReader("toSearchIn.txt"));
            //iterate through the textfile to read fullnames
            while((reader.readLine()) != null){
                  String fullName = reader.readLine();
              //string becomes an array of 2 elements
              String[] array_fullName = new String[2];
              if(fullName != null)
              array_fullName = fullName.split(" ");
              else break;
              //first element in array is the name 
                  String name  = array_fullName[0];
              //adding the name to the array of names

              names.add(name);
              //2nd element in array becomes the surname
              String surname  = array_fullName[1];
              //adding the surname to the array of surnames
              surNames.add(surname);



                   }
         } 
        //File not a textfile
        catch (Exception e) {
                  e.printStackTrace();
           }
        keysNames = BSTMethods.computeKey(names);
        keysSurnames = BSTMethods.computeKey(surNames);
        for(int i =0; i <(names.size()); i++) {
            treeName.addNode(keysNames.get(i), names.get(i));
        // System.out.println( names.get(i) );
            treeSurname.addNode(keysSurnames.get(i), surNames.get(i));
        // System.out.println( surNames.get(i) );

        }
        System.out.println("Name BST Traversed In Order");
        treeName.inOrder(treeName.root);
        System.out.println("**************************************************************\n");
        System.out.println("Surname BST Traversed Post Order");
        treeSurname.postOrder(treeSurname.root);











    }
}


import java.util.*;
import java.io.*;


    public class BSTMethods{
    private static  ArrayList<String> names;
    private static ArrayList<String> surNames;
    private static ArrayList<Integer> keys = new ArrayList<Integer>();
    public BSTNode root;
    public BSTNode parent;
    public BSTNode current;
    public BSTNode temp = root;

           BSTNode counter = new BSTNode();
          //keys ;  
      //adding keys method for names
      public static ArrayList<Integer> computeKey(ArrayList<String> names){
        int sum = 0;
        int char1 = 0;
         //determining keys for names and surnames
        for(int i = 0; i < names.size(); i++){
            //for each string name, process its characters and
             //work out int values
           for(int j = 1; j < (names.get(i)).length(); j++){
               //char at position 0
             char1 = (int)(names.get(i)).charAt(0);
                //summing the character int values
             sum = (char1) + (int)(names.get(i)).charAt(j); 
           }
             //and add to the keys' list
           keys.add(sum);
             //resetting sum
           sum = 0;

        }

        return keys;


      }
      public boolean addNode(int key, String name) {

         if(root == null) {
                root = new BSTNode(key, name); 
                temp = root;
                return true;
            }
            else if (key == temp.key){
                          temp.setCount();
                     return false;
         }
         else if (key <temp.key) {
                if(temp.left == null){
                    temp.left = new BSTNode(key, name);
                    return true;
                   } 
                   else {
                    temp = temp.left;
                        return addNode(key, name);
                 }
         }
         else if (key > temp.key) {
                 if (temp.right == null) {
                    temp.right = new BSTNode(key, name);
                    return true;
                  } 
                  else
                    return true;//addNode(key, name);

                    }
          else
              return false;

      } 

      public void inOrder (BSTNode x) {
        if(x == null) return ;
        if(x.left != null){
          inOrder(x.left);
        }
         System.out.println("key:"+ x.key + ", name:" + x.name + ", count:" + temp.count );
        if(x.right != null){
          inOrder(x.right);
        } 

      }

      public void postOrder (BSTNode y) {
        if(y.left != null){
         postOrder(y.left);
        }

        if(y.right != null){
          postOrder(y.right);
        }
        System.out.println("key:"+ y.key + ", surname:" + y.name + ", count:" + counter.getCount());  
      }

}

import java.util.*;
import java.io.*;


public class BSTNode{

   public int count, key;
   public String name, surname;
   public  BSTNode left;
   public  BSTNode right;

    public BSTNode()
    {
        this.left = null;
        this.right = null;
    }

    public BSTNode(int key, String name) {

    this.key = key;
    this.name = name;

    }
    public void setCount()
    {
         count += 1;
    }
    public int getCount()
    {
        return this.count;
    }



    public BSTNode getLeft()
    {
        return left;
    }

    public void setLeft(BSTNode left)
    {
        this.left = left;
    }

    public BSTNode getRight()
    {
        return right;
    }

    public void setRight(BSTNode right)
    {
        this.right = right;
    }  
}

0 个答案:

没有答案