简体   繁体   English

BST字符串遍历

[英]BST String Traversal

I'm trying to implement an inorder, preorder, and postorder traversal algorithm for my BST. 我正在尝试为我的BST实现有序,前序和后序遍历算法。 It seems that the inorder algorithm is working so far, but it's only returning the first character of the word. 到目前为止,顺序算法似乎可以正常工作,但是只返回单词的第一个字符。 I realize that I'm returning char c , but I'm confused on how I would make it return the entire word. 我意识到我要返回char c ,但是我对如何使其返回整个单词感到困惑。 Any help would be highly appreciated! 任何帮助将不胜感激!

package main;

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

// Node class
class Node
{
    char c;
    boolean end; 
    Node left, right;

    public Node(char c)
    {
        this.c = c;
        this.end = false;
        this.left = null;
        this.right = null;

    }          
}


class BinarySearchTree
{
    private Node root;

    public BinarySearchTree()
    {
        root = null;
    }

    public void addValue(String s)
    { 
            root = addValue(root, s, 0); 
    }

    private Node addValue(Node x, String s, int d)
    {
            char c = s.charAt(d);
            if (x == null) 
        x = new Node(c);
            if (c < x.c) 
        x.left = addValue(x.left, s, d);
            else if (c > x.c) 
        x.right = addValue(x.right, s, d);
            else x.end = true;
                return x;
    }

    public boolean search(String s)
    { 
            return search(root, s, 0); 
    }

    private boolean search(Node x, String s, int d)
    {
            if (x == null) 
        return false;

            char c = s.charAt(d);

            if (c < x.c) 
                return search(x.left, s, d);   
            else if (c > x.c) 
                return search(x.right, s, d);
            else 
                return x.end;

    }

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

        private void inorder(Node r){
            if (r != null){
                inorder(r.left);
                System.out.print(r.c);
                inorder(r.right);
            }
        }
} 

public class Project2
{
    public static void main(String[] args) throws Exception
    {
        BinarySearchTree tree  = new BinarySearchTree(); // Make new BST object

        // Variable for scanned word
        String token = "";

        // Use scanner for input file
        Scanner scan = new Scanner(new File("10words.txt")).useDelimiter("\\s+"); 

        //Check for next line in text file
        while (scan.hasNext()) 
        {
            token = scan.next();
            tree.addValue(token);
        }
        scan.close();

        tree.inorder();

        Scanner inputWord = new Scanner(System.in);
        System.out.print("\nEnter a word to search: ");
    String word = inputWord.next().toLowerCase();

    if(tree.search(word) == false){
            System.out.println("Word NOT Found!");
        } else{
            System.out.println("Word Found!");
        }
        inputWord.close();
    }
}

I did not look at your inorder code but you said it works so I'll believe you. 我没有查看您的订单代码,但您说它有效,所以我相信您。

However I did take a look at your addValue code and you only save the first character here. 但是,我确实看过了您的addValue代码,您只在这里保存了第一个字符。 No wonder you can't get the entire word back, you just don't save it :P 难怪你不能把整个单词都找回来,只是不保存:P

Instead, you should change your Node class to accept a String instead of a character. 相反,您应该将Node类更改为接受String而不是字符。 You won't need the d parameter in your addValue method then either. 您也不需要在addValue方法中使用d参数。

The Java String class provides the .compareTo () method in order to lexicographically compare Strings. Java String类提供.compareTo()方法,以按字典顺序比较String。 You can use it with string.compareTo(otherString) 您可以将其与string.compareTo(otherString)一起使用

This method will return a value < 0, equal to 0 or > 0. 此方法将返回值<0,等于0或> 0。

< 0 means string is lexicographically smaller than otherString (for example: "Apple" would be smaller than "Banana")

= 0 means it's the exact same word

> 0 means string is bigger than otherString (for example: "Banana" would be bigger than "Apple")

your addValue method looks like it is incorrect. 您的addValue方法看起来不正确。 it only modifies the root, then character will be equal, so it returns. 它仅修改根,然后字符将相等,因此返回。 In particular d is never modified. 特别是d永远不会被修改。

On a more fondamental level, a BST would be appropriate to look for a character in a tree, not for looking for a String. 在更基础的级别上,BST更适合在树中查找字符,而不是查找String。 If you want to look for a word, you can use a Trie instead (which is not a binary tree). 如果要查找单词,则可以改用Trie(不是二叉树)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM