
Concept explainers
Draw a UML class diagram for the following code:
class Node<E> {
E data;
Node<E> next;
public Node(E data) {
this.data = data;
this.next = null;
}
}
class Queue<E> {
Node<E> front;
Node<E> rear;
public Queue() {
front = null;
rear = null;
}
public boolean isEmpty() {
return front == null;
}
public void enqueue(E item) {
Node<E> newNode = new Node<E>(item);
if (isEmpty()) {
front = newNode;
rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
}
public E dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty.");
return null;
} else {
E item = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return item;
}
}
public void dequeueAll() {
front = null;
rear = null;
}
public E peek() {
if (isEmpty()) {
System.out.println("Queue is empty.");
return null;
} else {
return front.data;
}
}
}
public class Main {
public static void printQueue(Queue<Integer> queue) {
Node<Integer> currentNode = queue.front;
System.out.print("Queue: ");
while (currentNode != null) {
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
System.out.println();
}
public static void main(String[] args) {
Queue<Integer> queue = new Queue<Integer>();
System.out.println("Is the queue empty? " + queue.isEmpty());
printQueue(queue);
queue.enqueue(1);
System.out.println("Enqueued: 1");
printQueue(queue);
queue.enqueue(2);
System.out.println("Enqueued: 2");
printQueue(queue);
queue.enqueue(3);
System.out.println("Enqueued: 3");
printQueue(queue);
System.out.println("Is the queue empty? " + queue.isEmpty());
printQueue(queue);
System.out.println("Front of the queue: " + queue.peek());
printQueue(queue);
System.out.println("Dequeue: " + queue.dequeue());
printQueue(queue);
System.out.println("Front of the queue: " + queue.peek());
printQueue(queue);
queue.dequeueAll();
System.out.println("Dequeued all items");
printQueue(queue);
System.out.println("Is the queue empty? " + queue.isEmpty());
}
}

Step by stepSolved in 3 steps with 1 images

- class BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)…arrow_forwardclass BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)…arrow_forwardQuestion 13 gulab Consider the Binary Search Tree(BST) provided below: /* Class to represent Tree node */ class Node { int data; Node left, right; public Node(int item) { data = item; left = null; right = null; } } tree.root = new Node(4); tree.root.left = new Node(2); tree.root.right = new Node(6); tree.root.left.left = new Node(1); tree.root.left.right = new Node(3); tree.root.right.left = new Node(5); tree.root.right.right = new Node(7) Write a Java program that returns all the nodes in Level-Order Traversal that the output will be 4 2 6 3 5 7. The time complexity of this solution should be O(n) time and O(n) space. Include comments and description of time/space complexity please! Full explain this question and text typing work only We should answer our question within 2 hours takes more time then we will reduce Rating Dont ignore this linearrow_forward
- class BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)…arrow_forwardpublic class HeapPriQ<T> implements PriQueueInterface<T>{ protected ArrayList<T> elements; // priority queue elements protected int lastIndex; // index of last element in priority queue protected int maxIndex; // index of last position in ArrayList protected Comparator<T> comp; public HeapPriQ(int maxSize) // Precondition: T implements Comparable { elements = new ArrayList<T>(maxSize); lastIndex = -1; maxIndex = maxSize - 1; comp = new Comparator<T>() { public int compare(T element1, T element2) { return ((Comparable)element1).compareTo(element2); } }; } public HeapPriQ(int maxSize, Comparator<T> comp) // Precondition: T implements Comparable { elements = new ArrayList<T>(maxSize); lastIndex = -1; maxIndex = maxSize - 1; this.comp = comp; } public boolean isEmpty() // Returns true if this priority queue is empty; otherwise, returns false. {…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





