字符串的二进制搜索树

时间:2013-04-25 00:24:12

标签: java string tree binary-tree

我不知道在使用CompareTo方法为字符串创建二进制搜索树时出错了。我有节点文件和二进制树文件我不知道在哪里gofrom,以及它有什么问题。

节点文件

public class Node_String
{
    private String key;
    private Node parent;
    private Node leftChild;
    private Node rightChild;
    public String value = " ";

     public Node(String key, Node leftChild, Node rightChild)
     {
              this.setKey(key);
              this.setLeftChild(leftChild);
              this.setRightChild(rightChild);
           }

    public void setKey(String key) 
    {
        this.key = key;
    }

    public int getKey() 
    {
        return key;
    }

    public void setParent(Node parent) 
    {
        this.parent = parent;
    }

    public Node getParent() 
    {
        return parent;
    }

    public void setLeftChild(Node leftChild) 
    {
        this.leftChild = leftChild;
    }

    public Node getLeftChild() 
    {
        return leftChild;
    }

    public void setRightChild(Node rightChild) 
    {
        this.rightChild = rightChild;
    }

    public Node getRightChild() 
    {
        return rightChild;
    }
}

二叉树文件

public class BinarySearchTree_String 
{
     private Node root;

     public void insert(int key)
    {
        insert(new Node(key, null, null));
    }

    public void insert(Node z) 
    {

      Node y = null;
      Node x = root;


    while (x != null) 
          {
            y = x;
    int name = word.compareTo(x.key);       
    if (name < 0) 
    {
                x = x.getLeftChild();
            } 
          else 
    {
              x = x.getRightChild();
          }
        }

        //make y the parent of z     
          z.setParent(y);

        //checking if there is something inside of y and where to set the vaule that is stroed in y        
          if (y == null) 
          {
            root = z;
        } 
          //if y is not empty compare again to find which child it must be 
          else if 
          (z.getKey().equals(y.getKey()))
          {
            y.setLeftChild(z);
        }
          else
          {
            y.setRightChild(z);
        }
    }


      public void preorderTraversal() 
    {
        preorderTraversal(root);
    }

    public void preorderTraversal(Node node)
      {
              if (node != null)
            {
            //preorder method         
      System.out.print(node.getKey() + " ");
            preorderTraversal(node.getLeftChild());
            preorderTraversal(node.getRightChild());            
        }
    }


     public void inorderTraversal() 
        {
          inorderTraversal(root);
        }

    private void inorderTraversal(Node node)
      {
        if (node != null)
         {
           //the inorder
               inorderTraversal(node.getLeftChild());
            System.out.print(node.getKey() + " ");
            inorderTraversal(node.getRightChild());
        }
    }

    //to make root show b/c it is hidden  
     public void postorderTraversal() 
     {
        postorderTraversal(root);
    }

    private void postorderTraversal(Node node) 
     {
        if (node != null)
           {
           //post order 
               postorderTraversal(node.getLeftChild());
            postorderTraversal(node.getRightChild());
            System.out.print(node.getKey() + " ");
        }
    }
}

0 个答案:

没有答案