0% found this document useful (0 votes)
17 views

Data Structures

dsa

Uploaded by

arizsheikh17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Data Structures

dsa

Uploaded by

arizsheikh17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

---

Data Structures

1. What are Data Structures?

Data structures are ways to organize and store data in a computer so that it can be
accessed and modified efficiently. They are essential for managing large amounts of
data and optimizing performance in algorithms.

2. Linear vs Non-linear Data Structures

- Linear Data Structures: Elements are arranged in a sequential manner. Each element
is connected to its previous and next element. Examples include:

- Arrays: A collection of elements identified by index.

- Linked Lists: A sequence of nodes where each node points to the next.

- Stacks: Follow Last In First Out (LIFO) principle.

- Queues: Follow First In First Out (FIFO) principle.

- Non-linear Data Structures: Elements are not arranged in a sequential manner.


Examples include:

- Trees: Hierarchical structure with nodes connected in a parent-child relationship.

- Graphs: Nodes (vertices) connected by edges.

3. Stack, Queue, and Linked List

- Stack: A stack is a linear data structure that follows the LIFO principle. You can only
add or remove elements from one end (the top of the stack).

- Array Implementation: Use an array with a pointer to the top of the stack.

- Operations: Push (add), Pop (remove), Peek (view top element).


- Queue: A queue is a linear data structure that follows the FIFO principle. Elements are
added at the rear and removed from the front.

- Array Implementation: Use an array with two pointers (front and rear).

- Operations: Enqueue (add), Dequeue (remove), Peek (view front element).

- Linked List: A linked list consists of nodes where each node points to the next node. It
allows dynamic memory allocation.

- Types:

- Singly Linked List: Each node points to the next node.

- Doubly Linked List: Each node points to both the next and previous nodes.

- Circular Linked List: The last node points back to the first node.

4. Circular Queue

A circular queue is a type of queue where the last position is connected back to the first
position to form a circle. It effectively utilizes the storage by reusing the empty spaces
created when elements are removed.

5. Double-ended Queue (Deque)

A deque (pronounced "deck") is a linear data structure that allows insertion and
deletion of elements from both ends (front and rear). It can be implemented using
arrays or linked lists.

6. Types of Linked Lists

- Singly Linked List: Each node points to the next node only.

- Doubly Linked List: Each node points to both the next and previous nodes.

- Circular Linked List: The last node points back to the first node.

7. Insertion, Deletion, and Update in Linked Lists

- Insertion: Add a new node to a specified position.

- At the beginning: Adjust the head pointer.


- At the end: Traverse to the end and insert.

- At a specific position: Traverse to the position and adjust pointers.

- Deletion: Remove a node from a specified position.

- At the beginning: Adjust the head pointer.

- At the end: Traverse to the end and remove.

- At a specific position: Traverse to the position and adjust pointers.

- Update: Change the data in a specified node without changing its position.

8. Tree and Terminology

- Tree: A hierarchical data structure with a root node and child nodes. Each node has
zero or more child nodes and at most one parent node.

- Root: The top node of the tree.

- Node: An element of the tree.

- Edge: Connection between two nodes.

- Leaf: A node with no children.

- Subtree: A tree formed by a node and its descendants.

- Height: Length of the longest path from the root to a leaf.

9. Types of Binary Trees

- Binary Tree: Each node has at most two children (left and right).

- Binary Search Tree (BST): A binary tree where the left child is less than the parent node,
and the right child is greater.

- Balanced Binary Tree: A binary tree where the height difference between left and right
subtrees is minimal.

10. Tree Traversal Methods

- Inorder Traversal: Visit left subtree, root, right subtree.


- Preorder Traversal: Visit root, left subtree, right subtree.

- Postorder Traversal: Visit left subtree, right subtree, root.

11. Numerical Examples of Tree Traversal

Given a binary tree:

```

/\

B C

/\

D E

```

- Preorder: A, B, D, E, C

- Inorder: D, B, E, A, C

- Postorder: D, E, B, C, A

---

Q9) elaborate version:

Certainly! Let's dive deeper into the types of binary trees and the traversal methods
(Pre-order, In-order, and Post-order):

### Types of Binary Trees

1. Full Binary Tree:


- A Full Binary Tree is a tree in which every node other than the leaves has two children.
This means that every parent node has exactly two child nodes, and no node has only
one child.

- Example:

```

/\

2 3

/\/\

4 56 7

```

- In the above example, each internal node (nodes 1, 2, 3) has exactly two children.

2. Complete Binary Tree:

- A Complete Binary Tree is a binary tree in which all levels are completely filled except
possibly the last level, and the last level has all keys as left as possible.

- Example:

```

/\

2 3

/\/

4 56

```

- Here, all levels are fully filled except the last, which is filled from left to right.

3. Perfect Binary Tree:

- A Perfect Binary Tree is a type of binary tree in which all internal nodes have exactly
two children and all leaf nodes are at the same level.
- Example:

```

/\

2 3

/\/\

4 56 7

```

- In this example, all leaves (4, 5, 6, 7) are at the same level, and every internal node
has two children.

4. Balanced Binary Tree:

- A Balanced Binary Tree is a binary tree in which the height of the left and right
subtrees of any node differ by at most one. This ensures that the tree remains balanced
and operations like insertion, deletion, and search can be performed efficiently.

- Example:

```

/\

2 3

```

- This tree is balanced because the difference in height between the left and right
subtrees of every node is at most one.

5. Degenerate (or Pathological) Tree:

- A Degenerate Tree is a tree where each parent node has only one child. This
essentially makes the tree behave like a linked list.
- Example:

```

```

- Here, each node has only one child, making the structure resemble a linear linked
list.

### Traversal Methods

Traversal refers to the process of visiting each node in a tree data structure, exactly
once, in a specific order. Here’s a detailed explanation of the three common traversal
methods:

1. Pre-order Traversal (Root, Left, Right):

- Steps:

1. Visit the root node.

2. Traverse the left subtree in pre-order.

3. Traverse the right subtree in pre-order.

- Example:

- Given the tree:


```

/\

B C

/\ \

D E F

```

- Pre-order Traversal: Start at `A`, move to `B`, visit `D` and `E`, then move to `C`
and finally visit `F`.

- Result: `A, B, D, E, C, F`

2. In-order Traversal (Left, Root, Right):

- Steps:

1. Traverse the left subtree in in-order.

2. Visit the root node.

3. Traverse the right subtree in in-order.

- Example:

- Using the same tree:

```

/\

B C

/\ \

D E F

```

- In-order Traversal: Start at `D`, move up to `B`, visit `E`, then `A`, move to `C`,
and finally visit `F`.

- Result: `D, B, E, A, C, F`
3. Post-order Traversal (Left, Right, Root):

- Steps:

1. Traverse the left subtree in post-order.

2. Traverse the right subtree in post-order.

3. Visit the root node.

- Example:

- Using the same tree:

```

/\

B C

/\ \

D E F

```

- Post-order Traversal: Start at `D`, move to `E`, then `B`, move to `F`, then `C`,
and finally visit `A`.

- Result: `D, E, B, F, C, A`

### Additional Notes:

- Pre-order Traversal is useful for creating a copy of the tree or for prefix expression
notation (Polish notation).

- In-order Traversal is widely used to obtain a sorted list of elements from a binary
search tree (BST).

- Post-order Traversal is useful for deleting the tree or evaluating postfix expressions.

Understanding these traversal methods and types of binary trees is essential for solving
a variety of problems in data structures, especially those involving tree manipulations
and searches.

You might also like