Unit 4: Computational Structures — Short Questions
11th Class Computer Science · Unit 4: Unit 4: Computational Structures
Primitive Computational Structures
Primitive computational structures are the basic building blocks of computer programs. They include data types, operations, and control structures like loops and conditionals. These elements help build complex systems by combining simple parts.
They form the foundation for all software and algorithms. Primitives allow abstraction and help developers design efficient systems. Understanding them makes learning programming and problem solving easier.
A list stores multiple items in order, where each item has an index. It allows easy access, insertion, and deletion of data. Lists are used to manage collections of related items.
In Python, lists are created using square brackets '[]'. Items are separated by commas inside the brackets.
For example: fruits = ["apple", "banana", "cherry"].
Dynamic size means a list can grow or shrink as needed. You can add or remove items without setting a fixed size first. This makes lists more flexible than arrays in other languages.
Index-based access lets you get or change an item using its position. The first item is at index 0, the second at index 1, and so on. This helps in quickly accessing specific data.
Ordered means the sequence of items in a list is preserved. If you add "A" before "B", they will stay in that order unless changed. This helps maintain data relationships.
Use the append() method to add an item to the end.
For example: mylist.append("new item"). This changes the list permanently.
Use the insert() function with the .index and value.
Example: mylist.insert(1, "new item") inserts at position 1. Other items shift to make space.
Use the remove() method.
Example: mylist.remove("banana") removes the first occurrence of "banana". It gives an error; if the item is not found.
Use the pop() method.
Example: mylist.pop(0) removes the item at index 0. If no index is given, it removes the last item.
Use the in keyword.
Example: if "apple" in mylist: checks if "apple" is present. It returns True or False.
Lists store and manage data like names, numbers, or tasks. They also help implement stacks and queues, which are used in many computing tasks.
Stack
A stack is a data structure where items are added and removed from the top only. It follows the LIFO (Last-In, First-Out) principle. Like stacking plates, the last one added is the first one taken off.
LIFO stands for Last-In, First-Out. In a stack, the last item added is the first one removed. This rule ensures a specific order of processing.
The main operations are
- Push: Adds an item to the top.
- Pop: Removes an item from the top. These follow the LIFO rule.
A browser's back button uses a stack to keep track of visited pages. When you click back, the most recent page is removed from the stack. This helps navigate in reverse order.
Queue
A queue is a data structure where items are added at the back and removed from the front. It follows the FIFO (First-In, First-Out) principle. Like a line at a ticket counter.
FIFO stands for First-In, First-Out. The first item added is the first one removed. It ensures fair processing order in queues.
The main operations are
- Enqueue: Add an item to the back.
- Dequeue: Remove an item from the front These follow the FIFO rule.
Print jobs in a printer use a queue. The first document sent is printed first, and others wait in order. This prevents confusion and keeps processing fair.
Trees
A tree organizes data hierarchically starting from a root node. Each node can have child nodes, forming a branching structure. It shows parent-child relationships clearly.
The root node is the topmost node in a tree. All other nodes branch out from it. It is like the CEO in an organizational chart.
A leaf node is a node with no children. It appears at the ends of branches. Like a file in a folder, that does not contain any subfolders.
Height is the number of levels in a tree. It measures how deep the tree goes from the root to the farthest leaf. A taller tree takes longer to search.
A balanced tree has nearly equal heights on both sides. It helps keep searching and updating fast. An unbalanced tree may slow down operations.
Trees are used to represent file systems and family trees. They also help in decision-making processes like decision trees in AI.
Introduction to Graphs
A graph consists of vertices (nodes) connected by edges. It represents relationships between objects. Graphs can model networks like roads, social connections, or web links.
A tree is hierarchical with a root and no cycles. A graph has no root and can have cycles. Trees are a special type of graph.
In a directed graph, edges have direction (like one-way streets). In an undirected graph, edges go both ways. Directed graphs model Twitter follows; undirected graphs model Facebook friendships.