0% found this document useful (0 votes)
16 views4 pages

Dsa 03

The document provides an overview of non-linear data structures, including trees, graphs, heaps, and tries, emphasizing their hierarchical and networked arrangements. It details specific types of trees like binary trees and binary search trees, along with traversal methods, and introduces graphs with their types and traversal techniques. Additionally, it covers heaps for priority queue operations and tries for efficient string storage, concluding with the importance of non-linear structures in solving complex problems.

Uploaded by

22bph016
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)
16 views4 pages

Dsa 03

The document provides an overview of non-linear data structures, including trees, graphs, heaps, and tries, emphasizing their hierarchical and networked arrangements. It details specific types of trees like binary trees and binary search trees, along with traversal methods, and introduces graphs with their types and traversal techniques. Additionally, it covers heaps for priority queue operations and tries for efficient string storage, concluding with the importance of non-linear structures in solving complex problems.

Uploaded by

22bph016
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/ 4

Data Structures Part 2 - Non-Linear

Structures
Introduction to Non-Linear Structures
Non-linear data structures do not store data sequentially. Elements are arranged hierarchically
or in a networked way, allowing complex relationships.

Common Non-Linear Structures:

●​ Trees​

●​ Graphs​

●​ Heaps​

●​ Tries​

Trees
A tree is a hierarchical structure with a root element and children forming a parent-child
relationship.

Binary Trees

A binary tree has nodes with at most two children (left and right).

Binary Search Trees (BST)

●​ Left subtree has nodes with lesser values.​

●​ Right subtree has nodes with greater values.​

Tree Traversal Methods:

●​ In-order: Left, Root, Right​


●​ Pre-order: Root, Left, Right​

●​ Post-order: Left, Right, Root​

Example:

class Node:

def __init__(self, value):

self.value = value

self.left = None

self.right = None

root = Node(10)

root.left = Node(5)

root.right = Node(15)

Graphs
Graphs consist of vertices (nodes) and edges (connections).

Types of Graphs:

●​ Directed and Undirected​

●​ Weighted and Unweighted​

Traversal Methods:

●​ Breadth-First Search (BFS)​

●​ Depth-First Search (DFS)​


Implementation:

from collections import defaultdict

graph = defaultdict(list)

graph[0].append(1)

graph[0].append(2)

graph[1].append(3)

Heaps
A heap is a complete binary tree used for priority queue operations.

Types:

●​ Min-Heap: Root is the minimum element.​

●​ Max-Heap: Root is the maximum element.​

Operations:

●​ Insertion: O(log n)​

●​ Deletion (extract min/max): O(log n)​

Example:

import heapq

heap = []

heapq.heappush(heap, 3)

heapq.heappush(heap, 1)

print(heapq.heappop(heap)) # Output: 1
Tries
A trie is a special tree used for storing strings efficiently, especially for prefix-based operations.

Use Cases:

●​ Autocomplete​

●​ Spell checking​

●​ Prefix matching​

Implementation:

class TrieNode:

def __init__(self):

self.children = {}

self.is_end_of_word = False

Conclusion
Non-linear structures are crucial for solving complex problems that require hierarchical or
networked relationships. Next, we will discuss algorithms for sorting and searching.

You might also like