Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Adeyinka David

18CG024711

Computer Science

CSC312

Queue

A queue is a linear collection of elements that is accessible from both ends. The first element
inserted will be the head of the list, and the last element inserted will be the tail of the list. It
supports the First-In, First-Out principle – the head of the list is removed first, and the tail is removed
last. It resembles a real-world queue such as a line for meal tickets in the cafeteria. It can be
implemented with an array or with pointers. Using an array will impose an upper bound, N, on its’
size therefore making it take linear (O(N)) space. The basic operations on a queue with upper size
bound N are:

i. enqueue(E): an element E is added to the front of the list and is made the head. It will
throw an error called “queue overflow” if the queue is full. This operation takes constant
(O(1)) time and space.
ii. dequeue(): removes and returns the tail of the list. It will throw an error called “queue
underflow” if the queue is empty. This operation takes O(1) time and space.
iii. peek(): returns, but doesn’t remove the element head of the list. It takes O(1) time and
space.
iv. isEmpty(): returns a Boolean value indicating if the queue is empty or not. It is used to
prevent queue underflow. It takes O(1) time and space.
v. isFull(): returns a Boolean value indicating if the queue is full or not. It is used to prevent
queue overflow. It takes O(1) time and space.

Stack

A stack is a linear data structure that supports the Last-In, First-Out (LIFO) principle – the most
recent element inserted is the at the front of the stack, and it is the first to be removed. The first
element inserted is at the back of the stack, and it is the last to be removed. It resembles a real-
world stack such as a stack of plates. A stack can be implemented with an array or with pointers. An
array implementation imposes an upper bound, N, on its’ size therefore making it take O(N) space.
The basic operations of a stack with upper size bound N are:

i. push(E): an element E is put at the front of the stack. This will throw an error known as
“stack overflow” if the stack is full. It takes constant time and is therefore O(1) time. It
uses a fixed number of variables throughout its’ execution and is therefore O(1) space.
ii. pop(): the element at the front of the stack is removed and returned. It will throw an
error known as “stack underflow” if the stack is empty. This operation also takes O(1)
time and space.
iii. peek(): the element at the front of the stack is returned, but not removed. This
operation takes O(1) time and space.
iv. isEmpty(): returns a Boolean value to determine if the stack is empty. It is used to
prevent stack underflow. It is a O(1) time and space operation.
v. isFull(): returns a Boolean value to determine if the stack is full. It is used to prevent
stack overflow. It is a O(1) time and space operation.

Array

An array is a fixed-size collection of elements of the same type stored at contiguous memory
locations. The address of the first element is known as the base address, and the other elements up
to the size N can be accessed by adding the index (or a multiple of the index depending on the
memory size of the elements) to the base address for an offset. It takes linear (O(N)) space. This
calculation can be done in constant time and so makes an array a time-efficient lookup structure.
Most implementations of arrays will index N elements from 0 (inclusive) to N-1(inclusive). This is
known as zero-based indexing. It supports only one operation:

i. get(position): Most programming languages represent this operation as:


<array-name>[position]
It retrieves the element indexed at this location. Most implementations will throw an
“out of bounds exception” if position is out of bounds of the indices. It takes constant, or
O(1) time and space complexity.

Linked List

A linked list is a data structure used to create a list of elements without an upper bound on the
number, N, of elements it contains. Each element has at least two attributes: the data it contains,
and a pointer to the next element in the list. The first element in the list is called the head, and the
last element is called the tail. A linked list may be singly linked, doubly linked, or circular. Elements
of a singly linked list only contain one pointer attribute that points to the next element. The tail
points to a special value like NULL to indicate it is the last element. Elements of a doubly linked list
contain a second pointer attribute that points to the previous element in the list. The tail of a circular
linked list will point to the head of the list. A linked list can be implemented using an array or
pointers. The basic operations are:

i. insert(E): inserts the element E at the beginning of the new list, making it the new head.
This procedure can be done mainly by changing a fixed number of pointer values and so
takes constant (O(1)) time. It also takes O(1) space.
ii. search(E): looks for the element E within the list. It returns a special value such as NULL
if the element doesn’t exist. It can search the list from head to tail and therefore takes
time proportional to the size, N, of the list. It is a linear (O(N)) operation. It also takes
O(1) space.
iii. delete(E): assuming the element E exists in the list, this procedure removes it from the
list. It only needs to reassign the pointers of the zero, one, or two neighbouring
elements and doesn’t depend on the list size. It therefore takes O(1) time. It also takes
O(1) space.
iv. get(position): returns the element at the specified position index within the list. It must
start from the head and iterate through all the elements till it reaches the index. It
depends on the size of the list and therefore takes O(N) time. It takes O(1) space.

Tree

A tree is a nonlinear and hierarchical data structure that is represented by a set of nodes
connected by edges. Each node can point to other nodes but can only be pointed at by exactly
one node that must not be the node itself. The pointing node is known as the parent and the
node being pointed at is known as the child. The root is a special node that does not have a
parent. The most common type of tree in computer science is a binary tree. It has the property
that each node must have at most 2 children. The two nodes are known as the left and right
children. A binary search tree (BST) has the following further restrictions:

Node.Left.Value <= Node.Value

Node.Right.Value >= Node.Value

Assuming the tree is balanced – the left and right subtrees of the root are of equal or nearly
equal length – then the height, H, of a BST with N nodes is bounded from above by:

H=O (log 2 N )
The operations on a BST are:

i. insert(E): insert E at the appropriate position within the tree. It takes time proportional
to the height of the tree since the element must be inserted at the bottom and is
therefore a O(log2N) time operation. It takes O(1) space.
ii. search(E): search for E within the tree and return E if it is found. Return a special value
such as NULL otherwise. The nature of the BST causes the search to go down one level
for each iteration till it reaches the bottom, thus causing it to take O(log 2N) time. It takes
O(1) space.
iii. traversal(): this could be an inorder, preorder, or postorder traversal. The difference in
these is the order in which the elements are traversed: inorder visits the left child, then
the node, then the right child; preorder visits the node then left then right; and
postorder visits the left then right then node. Traversal takes O(N) time because it visits
each node exactly once. It takes O(log2N) space for a balanced tree.
DIFFERENCE BETWEEN A FULL AND COMPLETE BINARY TREE

Full Binary Tree

A binary tree is full if every node has exactly 0 or exactly 2 children. Since a node without children
can be called a leaf, a full tree can also be called a tree where each node has only 2 children.

Complete Binary Tree

A binary tree is complete if every level of the tree is filled with nodes except the bottom, and the
nodes at the bottom are at the left of the tree.

You might also like