java - JAVA PROGRAM:AVL 树字典输出问题
问题描述
我正在研究一个既存储单词又存储定义的 AVL 树。我在使用命令行参数提示符读取我的文件时遇到问题,它似乎拒绝读取它,除非我将它作为参数运行?(在 JGrasp 中构建运行参数)这是我必须在 AVL 树中将其作为命令行参数运行的代码:
Scanner sc = new Scanner(new File(args[0]));
Scanner scanner = new Scanner(System.in);
我也很难找到我的树的高度?当我放入的文件的高度不为 0 时,它一直给我 0
System.out.println(tree.height(tree.root));
这是我的整个代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;
import java.io.IOException;
public class Dictionary{
String word;
int height;
Dictionary left;
Dictionary right;
Vector v;
Dictionary(String data,String Defn)
{
word = data;
v = new Vector();
v.add(Defn);
height = 1;
}
}
class AVLTree {
Dictionary root;
static int flag = 0;
int height(Dictionary N) {
if (N == null)
return 0;
return N.height;
}
int max(int a, int b) {
return (a > b) ? a:b;
}
Dictionary rightRotate(Dictionary input) {
Dictionary x = input.left;
Dictionary y = x.right;
x.right = input;
input.left = y;
input.height = max(height(input.left), height(input.right)) + 1;
x.height = max(height(x.left), height(x.right)) + 1;
return x;
}
Dictionary leftRotate(Dictionary x) {
Dictionary input = x.right;
Dictionary y = input.left;
input.left = x;
x.right = y;
x.height = max(height(x.left), height(x.right)) + 1;
input.height = max(height(input.left), height(input.right)) + 1;
return input;
}
int getBalance(Dictionary N) {
if (N == null)
return 0;
return height(N.left) - height(N.right);
}
//insert
Dictionary insert(Dictionary root, String word, String Defn) {
if (root == null)
return (new Dictionary(word,Defn));
if ((word.compareTo(root.word)) < 0)
root.left = insert(root.left, word,Defn);
else if ((word.compareTo(root.word)) > 0)
root.right = insert(root.right, word,Defn);
else
return root;
root.height = 1 + max(height(root.left),
height(root.right));
int balance = getBalance(root);
if (balance > 1 && (word.compareTo(root.left.word)) < 0)
return rightRotate(root);
if (balance < -1 && (word.compareTo(root.left.word)) < 0)
return leftRotate(root);
if (balance > 1 && (word.compareTo(root.left.word)) < 0) {
root.left = leftRotate(root.left);
return rightRotate(root);
}
if (balance < -1 && (word.compareTo(root.left.word)) < 0) {
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
void search(Dictionary root,String word) {
if (root == null)
{
if(word.equals(root.word)){
flag = 1;
for(int i=0;i<root.v.size();i++)
System.out.println(root.v.get(i));
}
this.search(root.left,word);
this.search(root.right,word);
}
}
public static void main(String[] args) throws Exception {
AVLTree tree = new AVLTree();
while(sc.hasNext()) {
String str = sc.nextLine();
String arr[] = str.split(" ");
String word = arr[0];
String Defn = str.substring(arr[0].length()+1);
tree.root = tree.insert(tree.root,word,Defn);
}
System.out.println(tree.height(tree.root));
while(true){
System.out.print("$ ");
String command = scanner.next();
if(command.equals("SEARCH")){
String word = scanner.next();
flag = 0;
tree.search(tree.root,word);
if(flag == 0)
System.out.println("Word does not exist");
}
else if(command.equals("EXIT"))
break;
else
System.out.println("Invalid Command");
}
}
}
解决方案
推荐阅读
- azure - ARM 模板是否提供了一种在创建资源期间捕获日志的方法
- php - 通过表单提交阿拉伯语内容导致禁止页面
- google-chrome - Google Chrome Lighthouse/Audit 减少 JavaScript 执行时间 - 什么是“其他”
- sql - 在没有交付行的情况下填充 0 值
- json - 如何在 .net core 3.0 中反序列化 json
- javascript - 在数据中创建假 $route
- linux - INIT_DEV_PARAMS 失败重启设备
- python - 函数结果与可变默认参数的连接
- android - 如果子元素以边距后退,则不会显示布局上的 Android 圆角
- sql - 根据同一列的不同行中存在的值查询特定数据的问题