英文:
How can I add a tuple (key, value) from AVL Tree to a priority queue in java?
问题
I want to build a priority queue, where the elements are the nodes of my AVL tree. The priority queue should order the nodes with the one with the highest value first, and the one with the lowest value last. I tried to create a new class to implement it, but I'm not sure how to create it in a way that indicates that each element in the priority queue is a node in the AVL tree.
This is my code so far:
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
public class PriorityQueue_AVL {
static class Tuple implements Comparable<Tuple> {
String key;
int value;
public Tuple(String key, int value) {
this.key = key;
this.value = value;
}
//@Override
public int compareTo(Tuple node) {
return this.value - node.value; // if positive, then this.value is larger so it goes first, and vice versa
}
Right below the compareTo()
method in the above class, I also tried to create a method that iterates through the AVL tree to add each element to the priority queue, but I'm not sure if that's how it should be done. node
, key
, and value
are all underlined in red in the method:
static void AVLtoPQ(){
System.out.println();
// push elements into priority queue PQ
PriorityQueue<Tuple> PQ = new PriorityQueue<Tuple>();
if (AVLTree.node != null) {
inOrderTraversal(node.left);
PQ.add(new Tuple(key, value));
inOrderTraversal(node.right);
}
}
This is how I'm defining the AVL tree nodes in the AVLTree.java
class:
public class AVLTree<Key extends Comparable<Key>, Value> {
private Node root; // root of the AVL tree
// A node of the AVL tree
public class Node {
private Key key; // key of the node
private Value value; // value of the node
private Node left, right; // left and right subtrees of the node
private int height; // height of the node
public Node(Key key, Value value) {
this.key = key;
this.value = value;
this.height = 1;
}
}
What's wrong with the code, and how can I fix it? I know I need to "connect" the AVLTree.java
class to the PriorityQueue_AVL.java
class to be able to use the object Node there, but I'm not sure how. Also, the syntax for the priority queue is off, and I can't pinpoint why.
【Note】: This response contains the translated code and an explanation of the issues you're facing. If you need further assistance, feel free to ask.
英文:
I want to build a priority queue, where the elements are the nodes of my AVL tree. The priority queue should order the nodes with the one with the highest value first, and the one with the lowest value last. I tried to create a new class to implement it, but I'm not sure how to create it in a way that indicates that each element in the priority queue is a node in the AVL tree.
This is my code so far:
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
public class PriorityQueue_AVL {
static class Tuple implements Comparable<Tuple>{
String key;
int value;
public Tuple(String key, int value){
this.key = key;
this.value = value;
}
//@Override
public int compareTo(Tuple node) {
return this.value - node.value; //if positive, then this.value is larger so it goes first, and vice versa
}
Right below the compareTo()
method in the above class, I also tried to create a method that iterates through the AVL tree to add each element to the priority queue, but I’m not sure if that's how it should be done. node
, key
, and value
are all underlined in red in the method:
static void AVLtoPQ(){
System.out.println();
// push elements into priority queue PQ
PriorityQueue<Tuple> PQ = new PriorityQueue<Tuple>();
if (AVLTree.node != null) {
inOrderTraversal(node.left);
PQ.add(new Tuple(key, value));
inOrderTraversal(node.right);
}
}
This is how I'm defining the AVL tree nodes in the AVLTree.java
class:
public class AVLTree<Key extends Comparable<Key>, Value> {
private Node root; // root of the AVL tree
// A node of the AVL tree
public class Node {
private Key key; // key of the node
private Value value; // value of the node
private Node left, right; // left and right subtrees of the node
private int height; // height of the node
public Node(Key key, Value value) {
this.key = key;
this.value = value;
this.height = 1;
}
}
What's wrong with the code, and how can I fix it? I know I need to "connect" the AVLTree.java
class to the PriorityQueue_AVL.java
class to be able to use the object Node there, but I'm not sure how. Also, the syntax for the priority queue is off, and I can't pinpoint why.
答案1
得分: 1
以下是翻译好的部分:
-
首先,我会尽量不过分强调AVL树和优先队列之间的集成。这作为最终目标是可以的,但大多数数据结构的良好实现应该足够通用,可以轻松修改以接受稍微不同的输入数据类型。你通常可以使用泛型(似乎你已经熟悉了),但良好的、灵活的类设计也可以走很长一段路。所以,起初,我会专注于创建一个功能性的AVL树,只存储例如整数,然后创建一个功能性的优先队列,也只存储整数,然后你可以担心额外类型的复杂性和它们之间的接口,一旦基本功能可用。这是我的个人理念,但这里也有很多其他可行的设计策略。
-
其次,你说:“我正在尝试创建一个以*节点(元组)*作为输入的优先队列类”。我想指出,现在,Node不是Tuple,它们也没有任何关联。在你的优先队列上定义接受AVLTree::Node实例的方法会非常奇怪,这将紧密地将两个本质上不相关的数据结构耦合在一起。如果你想将Node提取到自己的公共类中,并使其扩展为Tuple,或者采用类似的方式,那是一种选择。但你在现实世界中看到的大多数数据结构实现都是独立的,将使用原始类型或泛型类型来实现其公共方法。
-
最后,我想指出,AVL树(以及一定程度上优先队列)是复杂的数据结构。如果你对Java还不熟悉(尤其是如果你对编程一无所知),并且你只是出于学术目的在做这个练习,我建议从实现一些更简单的东西开始(比如ArrayList和LinkedList)。但如果不是这种情况,请忽略我的建议。
如果你有更多具体的问题,我(或其他人)很乐意帮助,但如果没有更多的代码或细节添加到你的原始问题中,我认为上面的内容基本上是我可以提供的一切。
英文:
I have a few comments to offer, based on the OP's original question and most recent reply. Since the OP doesn't seem to have a specific technical question, I'm just going to provide some commentary, in hopes of moving them forwards.
First, I would try not to get too hung up on the integration between the AVL tree and PriorityQueue. It's fine to have this as an end goal, but good implementations of most data structures will be generic enough that they can be easily modified to accept slightly different incoming data types. You can often accomplish this with generics (which it seems you are already familiar with), but good, flexible class design can also go a long way. So to start with, I would focus on creating a functional AVL tree that just stores, for example, integers, and then a functional Priority Queue that also just stores integers, and then you can worry about the extra complexity of additional types and interfacing between the two of them once the base functionality works. That is my personal philosophy though, and there are lots of other viable design strategies here as well.
Second, you said "I'm trying to create a priorityqueue class that takes a the node (a tuple) as input". I want to point out that right now, a Node is NOT a Tuple, nor are they related to each other in any way. It would also be very weird to define a method on your priority queue that accepts an instance of AVLTree::Node; this would be tightly coupling two inherently unrelated data structures together. If you wanted to extract Node to its own public class, and have it extend Tuple, or something of that nature, that is one option. But most data structure implementations you see in the real world will be standalone and will utilize either primitive or generic types for their public methods.
So to give an example, using some very rough code that will not compile, I would consider this to be a bad implementation:
public class PriorityQueue{
private LinkedList<Integer> queue;
// tied too tightly to a different data structure that should be unrelated
public void addNodeToPQ(Node node){
this.queue.addLast(node.value); // this isn't really how you would add a value to a PriorityQueue, it's just for example
}
}
PriorityQueue pq = new PriorityQueue();
AVLTree avlTree = new AVLTree();
pq.addNodeToPQ(avlTree.get(1));
While this implementation is cleaner and more extendable
public class PriorityQueue{
private LinkedList<Integer> queue;
// Uses a very basic type (Integer), rather than relying on another specialized data structure. This allows multiple consumers to use your PriorityQueue, not just your AVLTree
public void addValueToPQ(Integer value){
this.queue.addLast(value); // this isn't really how you would add a value to a PriorityQueue, it's just for example
}
}
PriorityQueue pq = new PriorityQueue();
AVLTree avlTree = new AVLTree();
pq.addValueToPQ(avlTree.get(1).value);
Regarding errors in your PriorityQueue_AVL
code, I can think of 4 things I would change off the top of my head
- No obvious reason to import HashMap, Map, or PriorityQueue in your class
- Presumably you are going to be instantiating individual Tuple objects, so using
static
in the class definition is not appropriate and will not work @Override
should not be commented- You were missing a closing
}
for your class.
Outside of these, the code compiles just fine for me, so if there are additional syntax errors that you would like help with, some more details will be necessary.
Lastly, I do want to point out that an AVLTree (and also a PriorityQueue to some extent) is a complex data structure. If you're new to Java (and ESPECIALLY if you are new to programming in general) and you're doing this exercise purely for academic purposes, I suggest starting off by implementing something a little easier (ArrayList and LinkedList come to mind). But if that isn't the case, disregard.
If you have additional, specific questions, I (or others) are likely happy to help, but without more code or details added to your original question, I think the above is pretty much everything I have to offer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论