0% found this document useful (0 votes)
15 views15 pages

ADS Notes

The document explains Polish Notation (Prefix) and Reverse Polish Notation (Postfix), highlighting their advantages and applications in expression evaluation. It also covers different types of queues, their operations, and implementations, alongside a discussion on data structure representations, binary search trees, threaded trees, AVL trees, and the differences between binary trees and general trees. Each section provides examples and key characteristics to illustrate the concepts.

Uploaded by

Pallavi. V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

ADS Notes

The document explains Polish Notation (Prefix) and Reverse Polish Notation (Postfix), highlighting their advantages and applications in expression evaluation. It also covers different types of queues, their operations, and implementations, alongside a discussion on data structure representations, binary search trees, threaded trees, AVL trees, and the differences between binary trees and general trees. Each section provides examples and key characteristics to illustrate the concepts.

Uploaded by

Pallavi. V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ADS

1. Explain the polish Notation and reverse polish Notation


1. Polish Notation (Prefix Notation)
Polish Notation, also known as Prefix Notation, was introduced by the
Polish mathematician Jan Łukasiewicz. In this notation, the operator
precedes its operands.
Example:
Infix Expression (Standard Notation):
A+BA + BA+B
Polish Notation (Prefix Form):
+AB+ A B+AB
Advantages of Polish Notation:
• Eliminates the need for parentheses, as the order of operations is
inherently clear.
• Reduces ambiguity in complex expressions.
Example with Multiple Operators:
Infix:
(A+B) ×C (A + B) \times C(A+B) ×C
Polish Notation:
×+ABC\times + A B C×+ABC

2. Reverse Polish Notation (Postfix Notation)


Reverse Polish Notation (RPN), also known as Postfix Notation, is the
reverse of Polish Notation, where the operator follows its operands.
Example:
Infix Expression:
A+BA + BA+B
Reverse Polish Notation (Postfix Form):
AB+A B +AB+
Example with Multiple Operators:
Infix:
(A+B)×C(A + B) \times C(A+B)×C
Reverse Polish Notation:
AB+C×A B + C \timesAB+C×
Advantages of Reverse Polish Notation:
• No need for parentheses.
• Efficient for stack-based computation (used in some calculators and
compilers).
• Easier for computers to evaluate.
Evaluation Using Stack (For Postfix)
Example:
Evaluate "3 4 + 5 ×" in Reverse Polish Notation.
1. Read 3 → Push to stack.
2. Read 4 → Push to stack.
3. Read + → Pop 4 and 3, compute 3+4=73+4 = 73+4=7, push 7.
4. Read 5 → Push to stack.
5. Read × → Pop 5 and 7, compute 7×5=357 \times 5 = 357×5=35, push 35.
6. Final result = 35.
Applications of Polish & Reverse Polish Notation
• Used in stack-based evaluation.
• Implemented in PostScript (used in printers).
• Found in HP calculators for fast calculations.
• Used in compilers for expression parsing.

2. Explain the types of queues


A queue is a linear data structure that follows the FIFO (First In, First
Out) principle, meaning the element that is inserted first is removed first. It
is widely used in scheduling, buffering, and resource management.
Types of Queues:
1. Simple Queue (Linear Queue)
• The most basic type of queue.
• Follows FIFO (First In, First Out).
• Insertions happen at the rear, and deletions occur at the front.
• Once elements are dequeued, the space is not reused, leading to wastage.
Example:
Enqueue (Insert): 10 → 20 → 30 → 40
Dequeue (Remove): 10 (First Element Removed)
Queue after operation: 20 → 30 → 40

2. Circular Queue
• A variation of the simple queue where the last position is connected back
to the first position (circular).
• Efficient use of memory as spaces are reused.
• Prevents the "Queue Overflow" problem seen in simple queues.
Example:
Enqueue: 10 → 20 → 30 → 40
Dequeue: 10 is removed
Now, the space of 10 can be reused.
• Instead of shifting elements, the rear wraps around when it reaches the
end.

3. Double-Ended Queue (Deque)


• Also known as Deque (Double-Ended Queue).
• Insertions and deletions can occur at both ends (front and rear).
• Two types:
1. Input Restricted Deque – Insertion allowed at one end, deletion
allowed at both ends.
2. Output Restricted Deque – Deletion allowed at one end, insertion
allowed at both ends.
Example:
Enqueue Front: 10, Enqueue Rear: 20 → 30
Deque Front: 10 removed, Deque Rear: 30 removed

4. Priority Queue
• Each element is assigned a priority.
• Elements are dequeued based on priority, not just FIFO.
• Two types:
1. Ascending Priority Queue – Elements with lower priority values
are dequeued first.
2. Descending Priority Queue – Elements with higher priority
values are dequeued first.

Example:

Element Priority

Task A 3

Task B 1

Task C 2

• Task B (Priority 1) is dequeued first, followed by Task C (Priority 2), then


Task A (Priority 3).

Applications of Queue
• CPU Scheduling (Round Robin uses Circular Queue)
• Printers & Task Scheduling (FIFO)
• Call Centre Systems (Priority Queue)
• Deque in Palindrome Checking

3. Write a program to stack implementation using an array


#include <stdio.h>

#define MAX 5
int stack[MAX], top = -1;

// Push operation
void push(int value) {
if (top == MAX - 1)
printf("Stack Overflow! \n");
else
stack[++top] = value;
}

// Pop operation
int pop() {
if (top == -1)
printf ("Stack Underflow! \n");
else
return stack[top--];
return -1;
}

// Display stack elements


void display () {
if (top == -1)
printf ("Stack is empty!\n");
else {
printf ("Stack: ");
for (int i = top; i >= 0; i--)
printf ("%d ", stack[i]);
printf("\n");
}
}
// Main function
int main () {
push (10);
push (20);
push (30);
display ();

printf ("Popped: %d\n", pop ());


display ();

return 0;
}
Output:
Stack: 30 20 10
Popped: 30
Stack: 20 10

4. Explain the sequential representation and linked list representation

Data structures can be stored in two primary ways: Sequential


Representation (using arrays) and Linked List Representation (using
pointers and nodes). Below is a detailed comparison:

1. Sequential Representation (Using Arrays)


• Data elements are stored contiguously in memory.
• Each element is accessed using an index.
• Uses fixed-size memory allocation, meaning size must be defined in
advance.
Example (Array Representation of a List)
int arr[5] = {10, 20, 30, 40, 50};
Characteristics:
✔ Fast Access: Elements can be accessed directly using an index.
✔ Efficient Memory Usage (for small datasets)
Fixed Size: Hard to increase or decrease dynamically.
Insertion & Deletion is Costly: Requires shifting elements.
Example: Inserting 25 in an array at position 2
Before:
[10, 20, 30, 40, 50]
After shifting:
[10, 20, __, 30, 40, 50]
After inserting 25:
[10, 20, 25, 30, 40, 50]
Shifting elements makes insertions/deletions inefficient.\

2. Linked List Representation


• Data elements (nodes) are stored non-contiguously.
• Each node has:
o Data (Value)
o Pointer (Address of the next node)
Example (Linked List Representation)
struct Node {
int data;
struct Node* next;
};
Characteristics:
✔ Dynamic Memory Allocation: Can grow or shrink as needed.
✔ Efficient Insertions & Deletions: No shifting required.
Extra Memory Overhead: Each node stores an extra pointer.
Slower Access: Requires traversal to find an element.
Example: Inserting 25 between 20 and 30
Before:
10 → 20 → 30 → 40 → 50
After inserting 25:
10 → 20 → 25 → 30 → 40 → 50
No need for shifting—just update pointers.

5. Explain the binary search tree insert and delete operations

A Binary Search Tree (BST) is a binary tree where:


• The left subtree contains values less than the root.
• The right subtree contains values greater than the root.
• The left and right subtrees must also be BSTs.

1. Insertion in a Binary Search Tree


Steps for Inserting a Node
1. Start from the root.
2. Compare the new value with the current node.
o If it is smaller, go left.
o If it is larger, go right.
3. Repeat until you find an empty spot, then insert the node.
Example
Insert 40, 20, 60, 10, 30, 50, 70 into an empty BST.
Step-by-step insertion
1. Insert 40 → Root: 40
2. Insert 20 → Left of 40
3. Insert 60 → Right of 40
4. Insert 10 → Left of 20
5. Insert 30 → Right of 20
6. Insert 50 → Left of 60
7. Insert 70 → Right of 60
Final BST Structure
40
/ \
20 60
/ \ / \
10 30 50 70

2. Deletion in a Binary Search Tree


Steps for Deleting a Node
1. Find the node to be deleted.
2. Three cases:
o Case 1: Leaf Node (No Children) → Simply remove it.
o Case 2: One Child → Replace the node with its child.
o Case 3: Two Children → Find the in order successor (smallest
node in right subtree) and replace the node with it.
Example
Delete 40 from the BST.
Original BST
40
/ \
20 60
/ \ / \
10 30 50 70
• 40 has two children → Find the in-order successor (smallest in right
subtree = 50).
• Replace 40 with 50 and delete 50 from the right subtree.
Updated BST
50
/ \
20 60
/ \ \
10 30 70
Time Complexity
Operation Best Case Worst Case (Skewed Tree)

Insert O(log n) O(n)

Delete O(log n) O(n)

Search O(log n) O(n)

6. Describe the right threaded tree and left threaded tree

Introduction to Threaded Binary Trees


In a normal binary tree, many NULL pointers exist in the leaf nodes.
Threaded binary trees help utilize these NULL pointers to improve tree
traversal efficiency (especially in in-order traversal).
Instead of having NULL pointers, these trees use "threads" to point to
the in-order predecessor or successor.

1. Right Threaded Tree


• In a right-threaded binary tree, right NULL pointers in the tree are
replaced with a thread pointing to the in-order successor.
• This makes in-order traversal faster without recursion or a stack.
Example of Right Threaded Tree
10
/ \
5 20
/ \
15 30
➡ If 20 has a NULL right child, it will be threaded to 30 (its in-order
successor).
➡ If 15 has a NULL right child, it will be threaded to 20 (its in-order
successor).

2. Left Threaded Tree


• In a left-threaded binary tree, left NULL pointers are replaced with a
thread pointing to the in-order predecessor.
• It helps in reverse in-order traversal (Right → Root → Left).
Example of Left Threaded Tree
20
/ \
10 30
/
25
➡ If 25 has a NULL left child, it will be threaded to 20 (its in-order
predecessor).
➡ If 30 has a NULL left child, it will be threaded to 25 (its in-order
predecessor).

3. Fully Threaded Binary Tree


• A fully threaded tree has both left and right threading (threads replace
NULL pointers in both directions).
• This allows efficient in-order and reverse in-order traversal without
recursion.

Advantages of Threaded Trees


✔ Faster In-Order Traversal: No need for recursion or stack.
✔ Memory Efficient: Reduces NULL pointers by using threads.
✔ Efficient Reverse Traversal: In left-threaded trees, reverse in-order
traversal is easy.

Comparison Table
Right Threaded Left Threaded Tree
Feature
Tree
Pointer Used Right NULL pointers Left NULL pointers

Thread Points In-order predecessor


In-order successor
To
Faster in-order Faster reverse in-
Used For order traversal
traversal

Memory Saves right-child Saves left-child


Efficiency NULL pointers NULL pointers
Conclusion
Threaded binary trees optimize tree traversal by replacing NULL
pointers with threads to predecessors or successors. These are useful
for efficient traversal in applications like expression trees and
databases.

7. Discuss the avial tree and avial balance factor


AVL Tree and Balance Factor
What is an AVL Tree?
An AVL Tree (named after Adelson-Velsky and Landis) is a self-
balancing Binary Search Tree (BST) where:
1. The height difference (balance factor) between the left and right subtrees
of any node is at most 1.
2. If the tree becomes unbalanced after an insertion or deletion, it is
rotated to restore balance.

1. AVL Balance Factor


The Balance Factor (BF) of a node is calculated as:
Balance Factor=Height of Left Subtree−Height of Right Subtree\text{Bal
ance Factor} = \text{Height of Left Subtree} - \text{Height of Right
Subtree}Balance Factor=Height of Left Subtree−Height of Right Subtree
• BF = -1, 0, or 1 → Tree is balanced
• BF < -1 or BF > 1 → Tree is unbalanced, requires rotation
Example of Balance Factor Calculation
30
/ \
20 40
/
10
• BF (30) = Height (Left) - Height(Right) = (2 - 1) = 1
• BF (20) = (1 - 0) = 1
• BF (10) = (0 - 0) = 0
Since all nodes have BF ≤ 1, the tree is balanced.

2. Rotations in AVL Tree


If insertion or deletion makes the tree unbalanced (BF > 1 or BF < -1),
rotations are performed:
Types of Rotations
1. Right Rotation (Single Rotation - LL Case)
o Used when nodes are inserted in the left subtree of the left child.
o Example: Inserting 5 in this tree causes LL imbalance.
30
/
20
/
10

2. Left Rotation (Single Rotation - RR Case)


o Used when nodes are inserted in the right subtree of the right
child.
o Example: Inserting 50 in this tree causes RR imbalance.
10
\
20
\
30
\
50
o Left Rotate(10) → New root = 20
20
/ \
10 30
it
3. Left-Right Rotation (Double Rotation - LR Case)
o Used when nodes are inserted in the right subtree of the left
child.
4. Right-Left Rotation (Double Rotation - RL Case)
o Used when nodes are inserted in the left subtree of the right
child.
o Example: Inserting 35 in this tree.
20
\
40
/
35
o Step 1: Right Rotate(40) →
20
\
35
\
40
o Step 2: Left Rotate(20) →
35
/ \
20 40

8. Diff between binary tree and general tree


Difference Between Binary Tree and General Tree
Feature Binary Tree General Tree

A tree where each A tree where each


node has at most two node can have any
Definition
children (left and number of children.
right).
Strictly follows left No fixed number of
Structure and right child child nodes.
structure.
Maximum 2 children Can have more than
Child Nodes 2 children per node.
per node.
More complex since
Traversal Easier due to a fixed nodes can have
Complexity left/right structure. multiple children.

Includes BST, AVL, Includes N-ary


Types Trees, Trie, etc.
Heap, etc.

Used in searching, Used in file systems,


Usage sorting (BST, AVL), hierarchical data,
and heap operations. and game trees.

Example of a Binary Tree


A
/\
B C
/\ \
D E F
Example of a General Tree
A
/|\
B C D
/| ||\
E F GH I
Binary Tree is a special case of a General Tree with a restriction
of at most two children per node.

9. Write a program to implement stack implementation


using Linked list
#include <stdio.h>
#include <stdlib.h>

// Define Node structure


struct Node {
int data;
struct Node* next;
};

// Initialize top as NULL


struct Node* top = NULL;

// Push function
void push(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = top;
top = newNode;
printf("%d pushed\n", value);
}

// Pop function
void pop() {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
struct Node* temp = top;
printf("%d popped\n", top->data);
top = top->next;
free(temp);
}

// Display function
void display() {
struct Node* temp = top;
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function
int main() {
push(10);
push(20);
push(30);
display();
pop();
display();
return 0;
}
Output:
10 pushed
20 pushed
30 pushed
30 -> 20 -> 10 -> NULL
30 popped
20 -> 10 -> NULL

You might also like