Как изменить поиск в целочисленном дереве для поиска на основе строк? - PullRequest
0 голосов
/ 07 мая 2019

Я хочу искать на основе ввода строки. Целочисленный ввод работает легко, однако я назначен для строковых входов, которыми я не могу управлять.

Я создал метод поиска, чтобы найти общую стоимость определенного количества определенного материала, но он не работает.

Я ожидал, что вывод 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;

     } 
}

Этот код изначально принадлежит Дереку Банасу, я изменил его для своего использования, когда он выдал его, но я все еще не могу управлять поиском строк.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...