繁体   English   中英

BST字符串遍历

[英]BST String Traversal

我正在尝试为我的BST实现有序,前序和后序遍历算法。 到目前为止,顺序算法似乎可以正常工作,但是只返回单词的第一个字符。 我意识到我要返回char c ,但是我对如何使其返回整个单词感到困惑。 任何帮助将不胜感激!

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();
    }
}

我没有查看您的订单代码,但您说它有效,所以我相信您。

但是,我确实看过了您的addValue代码,您只在这里保存了第一个字符。 难怪你不能把整个单词都找回来,只是不保存:P

相反,您应该将Node类更改为接受String而不是字符。 您也不需要在addValue方法中使用d参数。

Java String类提供.compareTo()方法,以按字典顺序比较String。 您可以将其与string.compareTo(otherString)一起使用

此方法将返回值<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")

您的addValue方法看起来不正确。 它仅修改根,然后字符将相等,因此返回。 特别是d永远不会被修改。

在更基础的级别上,BST更适合在树中查找字符,而不是查找String。 如果要查找单词,则可以改用Trie(不是二叉树)。

暂无
暂无

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

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