[英]In-order traversal of BST not working correctly?
因此,我正在編寫一個程序來記錄二進制樹數組中文件中的單詞。 該數組的大小為26,每個索引代表一個字母。 每個元素都是一個BST,其Nodes由一個單詞和行號隊列組成(仍在起作用,目前,它只是創建一個新節點,而不是向隊列中添加新的行號)
我遇到的問題是遍歷樹。 root.toString()永遠不會在控制台上打印任何內容,但始終會打印“ Empty”。 不太清楚為什么會這樣,所以任何見解都會有很大幫助!
(請記住,這里的一些代碼僅用於我的測試目的,而一些次要的事情尚未完全實現。)
BinarySearchTree.java
import java.util.LinkedList;
import java.util.Queue;
//Binary Search Tree (BST)
public class BinarySearchTree{
//Pointer to root Node
private Node root = null;
//Nested Node class
static class Node{
//Node contents
private String word;
private Node leftChild;
private Node rightChild;
private Queue<String> queue = new LinkedList<String>();
public Node(String word, String line){
this.word = word;
this.queue.add(line);
this.leftChild = null;
this.rightChild = null;
}//End constructor
public void setLeftChild(Node left){
this.leftChild = left;
}//End setLeftChild
public void setRightChild(Node right){
this.rightChild = right;
}//End setRightChild
public Node getLeftChild(){
return this.leftChild;
}//End getLEftChild
public Node getRightChild(){
return this.rightChild;
}//End getRightChild
public String getWord(){
return this.word;
}//End getWord
@Override
public String toString() {
//return this.getWord() + this.view(); //TODO fix later
return "This is the toString for a Node";
}//End toString
public String dequeue() {
return this.queue.remove();
}//End dequeue
public String view() {
return this.queue.peek();
}//End view
}//End Node class
public Node getRoot() {
return this.root;
}//End getRoot
public void setRoot(Node n) {
this.root = n;
}//End setRoot
// insert a node to the binary search tree
public void insert(Node root, Node newNode){
//If no root (empty tree), newNode is root
if(root == null) {
setRoot(newNode);
}
//If root word > newNode word, move left
else if(root.getWord().compareToIgnoreCase(newNode.getWord()) > 0){
//No left child, insert here
if(root.getLeftChild() == null){
root.setLeftChild(newNode);
}
//Left child exists, recurse
else{
insert(root.getLeftChild(),newNode);
}
}
//If root word < newNode word, move right
else if(root.getWord().compareToIgnoreCase(newNode.getWord()) < 0){
//No right child, insert here
if(root.getRightChild() == null){
root.setRightChild(newNode);
}
//Right child exists, recurse
else{
insert(root.getRightChild(),newNode);
}
}
}//End insert
//in-order Traversal of Node
public void inOrderTraversal(Node root){
if(root != null){
inOrderTraversal(root.getLeftChild());
root.toString();
inOrderTraversal(root.getRightChild());
}
else
System.out.println("Empty");
}//End inOrderTraversal
}
FileSorter.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileSorter {
//Create BST Array
private static BinarySearchTree[] bstArray = new BinarySearchTree[26];
public static void main(String[] args) throws FileNotFoundException {
//Initialize the 26 BSTs
for(int i = 0; i < 26; i++) {
bstArray[i] = new BinarySearchTree();
}
//File to work with
File file = new File("Gettysburg.txt");
//Sort through file
scanByLine(file);
bstArray[1].inOrderTraversal(bstArray[1].getRoot());
System.out.println("Complete!");
}//End main
/*
* Below are methods for scanning a file, and also cleaning/stripping it
* of all non alphabetic characters so they will more cleanly fit in a Node
*/
public static void scanByLine(File f) throws FileNotFoundException {
//Keep track of line number
int lineNum = 1;
Scanner line = new Scanner(f);
//While there is another line, scan it, pass to scanWords, increment lineNum
while(line.hasNextLine()) {
String alphaString = removeNonAlpha(line.nextLine());
scanWords(alphaString, lineNum);
if(!alphaString.isEmpty()) {
lineNum++;
}
}
//Close line scanner when finished
line.close();
}//End scanByLine
public static void scanWords(String s, int line) {
Scanner word = new Scanner(s);
//While another word exists, scan it, place new node into array
while(word.hasNext()) {
String nodeWord = word.next().toLowerCase();
String nodeLine = Integer.toString(line);
//Add newWord to appropriate index of bstArray
int index = (int)nodeWord.charAt(0) - 97;
System.out.println("Creating Node.");
//Create new Node
BinarySearchTree.Node newNode = new BinarySearchTree.Node(nodeWord, nodeLine);
System.out.println("Created. Adding Node with " + newNode.getWord() + " at line: " + newNode.view());
bstArray[index].insert(bstArray[index].getRoot(), newNode);
}
//Close word scanner when finished
word.close();
}//End scanWords
//Remove anything non a-z and A-Z
public static String removeNonAlpha(String s) {
s = s.replaceAll("[^A-Za-z]", " ");
return s;
}
}
問題是您調用root.toString()
但對返回值不做任何事情。 最后,當沒有更多的葉子時,您將只打印Empty 。
你應該改變
root.toString();
至
System.out.println(root.toString()):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.