如何将整数树搜索修改为基于字符串的搜索?

时间:2019-05-07 14:20:30

标签: java search tree binary-tree

我想根据字符串输入进行搜索。整数输入很容易工作,但是我被分配给我无法管理的字符串输入。

我创建了一个searh方法来查找一定数量的某种材料的总价,但这是行不通的。

我希望println的输出为20(价格)x 30(数量),它的值为0(findTotalPrice方法)。

import java.util.ArrayList;
import java.util.List;

public class BarkTree {


    Node root;

    public void addNode(int key, String name, float price, float mass) {

    Node newNode = new Node(key,name,price,mass);

    if(root == null)
    {
        root = newNode;
    } else
    {
        Node focusNode = root;
        Node parent;
        while(true)
        {
            parent = focusNode;

            if(focusNode.key > key)
            {
                focusNode = focusNode.leftChild;

                if(focusNode == null) {

                    parent.leftChild = newNode;
                    return;
                }
            } else {
                focusNode = focusNode.rightChild;
                if(focusNode == null)
                {
                    parent.rightChild = newNode;
                    return;
                }
            }
        }
    }}public void inOrderTraverseTree(Node focusNode)
    {
        if(focusNode != null)
        {
            inOrderTraverseTree(focusNode.leftChild);

            System.out.println(focusNode);

            inOrderTraverseTree(focusNode.rightChild);
         }
    }

    public void preorderTraverseTree(Node focusNode)
    {
    if (focusNode != null)
    {
    System.out.println(focusNode);

    preorderTraverseTree(focusNode.leftChild);
    preorderTraverseTree(focusNode.rightChild);

    }

    }

    public void postorderTraverseTree(Node focusNode) {

        if (focusNode != null) 
        {
        System.out.println(focusNode);

        preorderTraverseTree(focusNode.leftChild);
        preorderTraverseTree(focusNode.rightChild);

        }
    }



    public Node findNode(int key)
    {
        Node focusNode = root;
        while(focusNode.key != key)
        {
            if(key < focusNode.key)
            {
                focusNode = focusNode.leftChild;
            }

            else {
                focusNode = focusNode.rightChild;
            }
            if(focusNode == null)
                return null;
        }
        return focusNode;
    }
    public float findTotalMass(int key,int amount)
    {
        Node focusNode = root;
        while(focusNode.key != key)
        {
            if(key < focusNode.key)
            {
                focusNode = focusNode.leftChild;
            }

            else {
                focusNode = focusNode.rightChild;
            }
            if(focusNode == null)
                return 0;
        }
        return (amount * focusNode.mass); 
    } //Why doesn't this method work?(Below)
    public float findTotalPrice(String name,int amount)
    {
        Node focusNode = root;
        while(!(focusNode.name.equals(name)))
        {
            if(name.compareTo(focusNode.name) > 0)
            {
                focusNode = focusNode.leftChild;
            }

            else {
                focusNode = focusNode.rightChild;
            }
            if(focusNode == null)
                return 0;
        }
        return (amount * focusNode.price); 
    }
    public static void main(String[] args) {

        BarkTree theTree = new BarkTree();

        theTree.addNode(12,"5.123",200f,0.1f );
        theTree.addNode(13,"235.124",250f,1f);
        theTree.addNode(14,"strr",50f,0.2f);
        theTree.addNode(21, "mera",30f,0.4f);
        theTree.addNode(22,"asd",20f,0.3f);
        theTree.addNode(23,"2sdf",15f,0.2f);
        theTree.addNode(32,"3dshdsh1",200f,0.5f);
        theTree.addNode(33,"3lale",30f,3.5f);
        theTree.addNode(34,"barran",50f,4.5f);
        theTree.addNode(42,"3g3",20f,0.01f);
        theTree.addNode(44,"Iguc",50f,0.03f);
        theTree.addNode(55,"Tety",20f,1.5f);
        theTree.addNode(56,"og",80f,0.5f);
        theTree.addNode(61,"s",2.2f,0.25f);
        theTree.addNode(67,"s",0.75f,0.35f);

        theTree.postorderTraverseTree(theTree.root);
        System.out.println(theTree.findTotalPrice("3g3",30));

    }
    }

    class Node { 
        int key;
     String name;
     float price;
     float mass;
     Node leftChild;
     Node rightChild;
     Node(int key, String name,float price,float mass)
     {
         this.key = key;

         this.name = name;

         this.mass = mass;

         this.price = price;
     }
     public String toString() {
         return name + " has a key " + key;

     } 
}

此代码最初属于Derek Banas,当他给出该代码时,我对其进行了修改,但仍然无法管理字符串搜索。

0 个答案:

没有答案