Data Structure 2
Data Structure 2
Linear Data Structure: Data is organized sequentially (e.g., Arrays, Stacks, Queues).
Non-Linear Data Structure: Data is organized hierarchically or non-linearly (e.g., Trees, Graphs).
Algorithm:
1. If TOP == MAX - 1, then Overflow.
2. Else, TOP = TOP + 1; STACK[TOP] = Element.
Algorithm:
1. If TOP == -1, then Underflow.
2. Else, Element = STACK[TOP]; TOP = TOP - 1.
Overflow: Occurs when adding an element exceeds the structure's capacity (e.g., pushing into a full stack).
Underflow: Occurs when trying to remove an element from an empty structure (e.g., popping an empty stack).
Stack: Follows LIFO (Last In, First Out). Operations are restricted to one end (top).
Array: Elements are indexed; supports random access. No restrictions on operations.
Circular Queue: End of the queue connects to the front, making it circular.
Advantages: Utilizes space efficiently, avoids "queue full" issues in a partially empty array.
Priority Queue: Elements are dequeued based on priority rather than order of arrival.
Dequeue (Double-Ended Queue): Elements can be added/removed from both ends.
7. Postfix Evaluation
Postfix notation eliminates the need for parentheses. Use a stack to evaluate.
Example: 3 4 + evaluates as (3 + 4).
Singly Linked List: Nodes contain data and a pointer to the next node.
Advantages: Dynamic size, efficient insertion/deletion.
A puzzle involving moving disks from one rod to another, following these rules:
1. Only one disk moved at a time.
2. A larger disk cannot be placed on a smaller disk.
Beginning:
Middle:
Last:
A tree where each node has at most two children: left and right.
A
/ \
B C
/ \ \
D E F
An ADT is a data structure defined by its behavior from the user's perspective.
Includes operations but not implementation details.
Example: Stack ADT with operations like Push(), Pop(), IsEmpty().
A B-Tree is a self-balancing search tree where nodes can have multiple children.
Ensures balanced height.
Used in databases and file systems.
Properties:
Each node contains t-1 to 2t-1 keys, where t is the order.
Leaves are at the same level.
Steps to construct:
1. Insert nodes while maintaining a balanced height difference of ≤ 1 between subtrees.
2. Perform rotations (LL, RR, LR, RL) as needed to balance.
Example: Insert 10, 20, 30.
Insert 10.
Insert 20. No imbalance.
Insert 30. Imbalance at root (LL rotation).
Resulting tree:
20
/ \
10 30
Given:
In-order: D, B, E, A, C, F
Pre-order: A, B, D, E, C, F
Steps:
Constructed tree:
A
/ \
B C
/ \ \
D E F
2
Example: 3𝑥 + 4𝑥 + 5
Operations: