Binary Search Tree (C++)
Loading...
Searching...
No Matches
Queue Class Reference

Represents an explicit queue used to support non-recursive tree operations. More...

#include <Queue.h>

Public Member Functions

 Queue ()
 Constructs an empty queue.
 ~Queue ()
 Destroys the Queue.
void enqueue (Node *n)
 Adds a node to the end of the queue.
Nodedequeue ()
 Removes and returns the node at the front of the queue.
bool isEmpty () const
 Checks whether the queue is empty.

Detailed Description

Represents an explicit queue used to support non-recursive tree operations.

The Queue class implements a simple queue structure composed of node pointers. It is used to support non-recursive, level-order traversal operations within a binary search tree.

The Queue does not assume ownership of the tree nodes it stores; it manages only the queue nodes required to support these operations.

Supported operations include:

  • enqueue(): Adds a node pointer to the end of the queue.
  • dequeue(): Removes and returns a node pointer from the front of the queue.
  • isEmpty(): Checks whether the queue is empty.

Definition at line 64 of file Queue.h.

Constructor & Destructor Documentation

◆ Queue()

Queue::Queue ( )
inline

Constructs an empty queue.

Note
Initializes the queue with its front and rear pointers set to nullptr.

Definition at line 70 of file Queue.h.

70: front(nullptr), rear(nullptr) {}

◆ ~Queue()

Queue::~Queue ( )

Destroys the Queue.

Destroys the queue by removing all remaining nodes.

Definition at line 21 of file Queue.cpp.

21 {
22 // Remove all nodes from front to rear.
23 while (!isEmpty()) {
24 dequeue();
25 }
26}
Node * dequeue()
Removes and returns the node at the front of the queue.
Definition Queue.cpp:45
bool isEmpty() const
Checks whether the queue is empty.
Definition Queue.cpp:58

Member Function Documentation

◆ dequeue()

Node * Queue::dequeue ( )

Removes and returns the node at the front of the queue.

Returns
Pointer to the node removed from the queue.

Dequeues the front element and returns the associated tree node pointer.

Definition at line 45 of file Queue.cpp.

45 {
46 if (!front) return nullptr;
47 QueueNode* temp = front;
48 Node* result = temp->data;
49 front = front->next;
50 if (!front) rear = nullptr;
51 delete temp;
52 return result;
53}

◆ enqueue()

void Queue::enqueue ( Node * n)

Adds a node to the end of the queue.

Parameters
nPointer to the Node added to the rear of the queue.

Appends a node to the queue.

Definition at line 31 of file Queue.cpp.

31 {
32 QueueNode* q = new QueueNode(n);
33 if (!rear) {
34 front = rear = q;
35 }
36 else {
37 rear->next = q;
38 rear = q;
39 }
40}

◆ isEmpty()

bool Queue::isEmpty ( ) const

Checks whether the queue is empty.

Returns
True if the queue contains no elements.

Determines whether the queue has any elements.

Definition at line 58 of file Queue.cpp.

58 {
59 return front == nullptr;
60}

The documentation for this class was generated from the following files: