BST字符串遍历

时间:2016-03-04 22:06:10

标签: java algorithm binary-tree tree-traversal

我正在尝试为我的BST实现一个inorder,preorder和postorder遍历算法。似乎inorder算法到目前为止工作,但它只返回单词的第一个字符。我意识到我正在回归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();
    }
}

2 个答案:

答案 0 :(得分:1)

我没有查看你的缺陷代码,但你说它有效,所以我会相信你。

但是我确实看了你的addValue代码而你只保存了第一个字符。难怪你不能把整个单词都拿回来,你只是不保存它:P

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

Java String类提供.compareTo()方法,以便按字典顺序比较字符串。您可以将它与string.compareTo(otherString)

一起使用

此方法将返回一个值&lt; 0,等于0或> 0.

extern "C" { ... }

答案 1 :(得分:0)

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

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