Define Linear Data Structure and Non-linear Data Structure (1)
Define Linear Data Structure and Non-linear Data Structure (1)
pe
6) State any two differences between linear search and binary search.
Time The time complexity is O(n), where n is the The time complexity is O(log n), making it faster for
Complexity number of elements in the list. larger datasets.
Efficiency Less efficient, especially for large datasets. More efficient, especially for large datasets.
Data
Does not require the list to be sorted. Requires the list to be sorted.
Requirement
Directed Graph:
• Edges have direction: An edge from node A to node B is different from an edge
from node B to A.
• Representation: Often visualized with arrows on the edges.
• Examples: Social networks where one user follows another, or a workflow
diagram showing the order of tasks.
• Directed graph edge (vi, vj) indicates a path from vertex vi to vertex vj .
• For a graph that is weakly connected, the edges can be represented as a
directed graph by removing all edges and replacing them with a directed edge
from the other node to the original node .
Undirected Graph:
• Edges have no direction: An edge between node A and node B is the same as
an edge between node B and node A.
• Representation: Edges are usually drawn as lines without arrows.
• Examples: A map showing roads where you can travel in either direction, or a
network of friends where everyone can call each other.
• Undirected graph edge {vi, vj} represents an edge between vertices vi and vj .
• For a graph that is strongly connected, the edges can be represented as an
undirected graph by representing them as a two-way edg
8) Define : (i) Binary tree (ii) Binary search tree
Binary Tree:
• A binary tree is a data structure where each node can have at most two children,
a left child and a right child.
• It's a hierarchical structure, meaning nodes are organized in a parent-child
relationship.
• This structure allows for efficient traversal and manipulation of data, making it
useful in various applications.
• A BST is a specialized binary tree where the values of nodes are arranged in a
specific order.
• The left subtree of a node contains only values less than the node's value, and
the right subtree contains only values greater than the node's value.
• This ordering makes it efficient for searching, insertion, and deletion
1) Write an algorithm to count number of nodes in singly linked list.
1. Initialize a variable count to 0.
2. Start at the head of the linked list.
3. Traverse the linked list using a loop.
4. For each node visited, increment the count by 1.
5. Continue this process until reaching the end of the list (i.e., when the next
pointer of the current node is NULL).
6. Return the value of count as the total number of nodes in the linked list.;
• If head is NULL:
→ List is empty; return NULL.
• If head->next == head:
→ Only one node in the list.
→ Free/delete the head and return NULL.
• Otherwise:
a. Find the last node (node whose next points to head).
b. Point last node’s next to head->next.
c. Store head in a temporary pointer.
d. Move head to head->next.
e. Free/delete the old head.
f. Return the new head.
Code:
function deleteFromBeginning(head):
if head == NULL:
return NULL
if head.next == head:
free(head)
return NULL
temp ← head
last ← head
last.next ← head.next
head ← head.next
free(temp)
return head
Pseudocode:
Algorithm:
f root is NULL, return.
Visit (process/print) root->data.
Call preorder(root->left).
Call preorder(root->right).
🧾 Pseudocode:
function preorder(root):
if root is NULL:
return
visit(root)
preorder(root.left)
preorder(root.right)
🧾 Pseudocode:
function deleteFromBeginning(head):
if head == NULL:
return NULL
temp ← head
head ← head.next
free(temp)
return head
10) Write an algorithm to search a particular node in the given linked list.