嗨,我对编程很陌生,并且有一个项目希望能得到一些帮助。我应该从文本文件中读取一些姓名和电话号码,然后使用它们来填充二叉搜索树。文本文件中的每一行(总共 5 行)都有一个人的姓名(名字和姓氏)以及他们的电话号码。然后我应该使用搜索键来查找每个人的姓名。以下是说明:
编写一个程序,为您提供一种存储和检索电话号码的方法。设计一个提供以下操作的控制台程序:
添加:将一个人的姓名和电话号码添加到电话簿。
删除:从电话簿中删除给定人员的姓名和电话号码,仅给出姓名。
查找:查找某人的电话号码,仅给出该人的姓名。
更改:根据人名和新电话号码更改一个人的电话号码。
退出:首先将电话簿保存在文本文件中后退出应用程序。
您可以进行如下操作:
设计并实现 Person 类,它代表一个人的姓名和电话号码。您将在电话簿中存储此类的实例。设计并实现代表电话簿的类PhoneBook。该类应包含二叉搜索树作为数据字段。这棵树包含书中的人物。添加使用文本文件保存和恢复树的方法。设计和实现类Menu,它提供程序的用户界面。
程序应在开始时从文本文件中读取数据,并在用户退出程序时将数据保存到文本文件中。
我遇到麻烦的地方是填充 BST。当我尝试填充树时,我收到此错误消息“不兼容的类型:字符串无法转换为 KeyedItem” 如何更改 KeyedItem 类以使其正常工作?另外,如果我可以克服 KeyedItem 问题,我在 Menu 类中编写的代码是否足以填充 BST?我知道这里有很多信息;我提前感谢你能给我的任何帮助。
这是我的课程:
菜单类:
public static void main(String[]args) throws IOException{
String read = "";
PhoneBook btree = new PhoneBook(); //creates bst object from phonebook class
BufferedReader input = new BufferedReader(new FileReader("names.txt"));
for (int i=0; i<5; i++){
read = input.readLine(); //reads each lin of text file and stores it in var read
}
btree.insert(read); //this is where I get the ERROR message
}
}
KeyedItem 搜索类:
public abstract class KeyedItem<KT extends Comparable<? super KT>> {
private KT searchKey;
public KeyedItem(KT key){ //constructor
searchKey = key;
}
电话簿类:
public class PhoneBook<T extends KeyedItem<KT>, KT extends Comparable<? super KT>> extends BinaryTreeBasis<T> {
public PhoneBook(){
}
public PhoneBook(T rootItem){
super(rootItem);
}
public void setRootItem(T newItem) //I believe this class is not always used
throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public void insert(T newItem){
root = insertItem(root, newItem);
}
public T retrieve(KT searchKey){
return retrieveItem(root, searchKey);
}
public void delete(KT searchKey) throws TreeException{
root = deleteItem(root, searchKey);
}
public void delete(T item) throws TreeException{
root = deleteItem(root, item.getKey());
}
protected TreeNode<T> insertItem(TreeNode<T> tNode, T newItem){
TreeNode<T> newSubtree;
if(tNode == null){
tNode = new TreeNode<T>(newItem, null, null);
return tNode;
}
T nodeItem = tNode.item;
if(newItem.getKey().compareTo(nodeItem.getKey()) < 0) {
newSubtree = insertItem(tNode.leftChild, newItem);
tNode.leftChild = newSubtree;
return tNode;
}
else {
newSubtree = insertItem(tNode.rightChild, newItem);
tNode.rightChild = newSubtree;
return tNode;
}
}
protected T retrieveItem (TreeNode<T> tNode, KT searchKey) {
T treeItem;
if(tNode == null){
treeItem = null;
}
else {
T nodeItem = tNode.item;
if (searchKey.compareTo(nodeItem.getKey()) == 0) {
treeItem = tNode.item;
}
else if (searchKey.compareTo(nodeItem.getKey()) < 0){
treeItem = retrieveItem(tNode.leftChild, searchKey);
}
else {
treeItem = retrieveItem(tNode.rightChild, searchKey);
}
}
return treeItem;
}
protected TreeNode<T> deleteItem(TreeNode<T> tNode, KT searchKey) {
TreeNode<T> newSubtree;
if (tNode == null){
throw new TreeException("TreeException: Item not found");
}
else{
T nodeItem = tNode.item;
if (searchKey.compareTo(nodeItem.getKey()) == 0){
tNode = deleteNode(tNode);
}
else if (searchKey.compareTo(nodeItem.getKey()) < 0){
newSubtree = deleteItem(tNode.leftChild, searchKey);
tNode.leftChild = newSubtree;
}
else {
newSubtree = deleteItem(tNode.rightChild, searchKey);
tNode.rightChild = newSubtree;
}
}
return tNode;
}
protected TreeNode<T> deleteNode(TreeNode<T> tNode){
T replacementItem;
if((tNode.leftChild == null) &&
(tNode.rightChild == null)){
return null;
}
else if (tNode.leftChild == null){
return tNode.rightChild;
}
else if (tNode.rightChild == null){
return tNode.leftChild;
}
else {
replacementItem = findLeftmost(tNode.rightChild);
tNode.item = replacementItem;
tNode.rightChild = deleteLeftmost(tNode.rightChild);
return tNode;
}
}
protected T findLeftmost(TreeNode<T> tNode){
if (tNode.leftChild == null) {
return tNode.item;
}
else {
return findLeftmost(tNode.leftChild);
}
}
protected TreeNode<T> deleteLeftmost(TreeNode<T> tNode){
if (tNode.leftChild == null){
return tNode.rightChild;
}
else{
tNode.leftChild = deleteLeftmost(tNode.leftChild);
return tNode;
}
}
}
public KT getKey(){
return searchKey;
}
}
人物类:
public class Person extends KeyedItem<String>{
private FullName name;
private String phoneNumber;
public Person(String id, FullName name, String phone){ //constructor
super(id);
this.name = name;
phoneNumber = phone;
}
public String toString(){
return getKey() + " # " + name;
}
}
树节点类:
public class TreeNode<T> {
T item;
TreeNode<T> leftChild;
TreeNode<T> rightChild;
public TreeNode(T newItem){ //constructor
item = newItem;
leftChild = null;
rightChild = null;
}
public TreeNode(T newItem, TreeNode<T> left, TreeNode<T> right){
item = newItem;
leftChild = left;
rightChild = right;
}
}
BinaryTreeBasis 类:
public abstract class BinaryTreeBasis<T> {
protected TreeNode<T> root;
public BinaryTreeBasis(){ //default constructor
root = null;
}
public BinaryTreeBasis(T rootItem){
root = new TreeNode<T>(rootItem, null, null);
}
public boolean isEmpty(){
return root == null;
}
public void makeEmpty(){
root = null;
}
public T getRootItem() throws TreeException {
if (root == null){
throw new TreeException("Tree Exception: Empty Tree");
}
else{
return root.item;
}
}
public abstract void setRootItem(T newItem);
}
全名类:
public class FullName implements java.lang.Comparable<Object>{ //change from object?
private String firstName;
private String lastName;
public FullName(String first, String last){
firstName = first;
lastName = last;
}
public int compareTo(Object rhs){
FullName other = (FullName)rhs;
if(lastName.compareTo(((FullName)other).lastName)==0){
return firstName.compareTo(((FullName)other).firstName);
}
else {
return lastName.compareTo(((FullName)other).lastName);
}
}
}
树迭代器类:
public class TreeIterator<T> implements java.util.Iterator<T> {
private BinaryTreeBasis<T> binTree;
private TreeNode<T> currentNode;
private LinkedList <TreeNode<T>> queue;
public TreeIterator(BinaryTreeBasis<T> bTree){
binTree = bTree;
currentNode = null;
queue = new LinkedList <TreeNode<T>>();
}
public boolean hasNext(){
return !queue.isEmpty();
}
public T next()
throws java.util.NoSuchElementException{
currentNode = queue.remove();
return currentNode.item;
}
public void remove()
throws UnsupportedOperationException{
throw new UnsupportedOperationException();
}
public void setPreorder(){
queue.clear();
preorder(binTree.root);
}
public void setInorder(){
queue.clear();
inorder(binTree.root);
}
public void setPostorder(){
queue.clear();
postorder(binTree.root);
}
private void preorder(TreeNode<T> treeNode){
if(treeNode != null){
queue.add(treeNode);
preorder(treeNode.leftChild);
preorder(treeNode.rightChild);
}
}
private void inorder(TreeNode<T> treeNode){
if(treeNode != null){
inorder(treeNode.leftChild);
queue.add(treeNode);
inorder(treeNode.rightChild);
}
}
private void postorder(TreeNode<T> treeNode){
if(treeNode != null){
postorder(treeNode.leftChild);
postorder(treeNode.rightChild);
queue.add(treeNode);
}
}
}