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

DSA Nonlinear

Uploaded by

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

DSA Nonlinear

Uploaded by

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

Data Structures & Algorithms

Instructor: Gibson Mhlanga


Email: [email protected]
November 2024
Data Structures & Algorithms
Linear Data Structures
- Data elements arranged in a linear fashion e.g. arrays,
stacks, queues, linked lists
Non-linear data structures
- Data elements are not arranged in a linear fashion.
There is a hierarchical relationship between individual data
items, e.g. trees and graphs

Arrays
- A data structure that stores elements in adjacent locations
- Stores data of the same type
- Each data item is accessed using the array name and an
index
Data Structures & Algorithms
Limitations of arrays
- Arrays are of fixed size
- Elements are stored in contiguous memory locations
which may not be always available
- Insertion and deletion of elements may be problematic
because of shifting of elements form their positions
Data Structures & Algorithms
Linked List
- A DS in which each element contains a pointer/link to
the next element in the list
- Insertion and deletion is possible at any point of the list
- Each node contains data and a pointer to the next node
Disadvantage of List
- Slow search operation and requires more memory
space
Data Structures & Algorithms
Stack
 A linear data structure in which insertion and deletion
of elements are done at only one end, which is known
as the top of the stack.
 Stack is called a last-in, first-out (LIFO) structure
because the last element which is added to the stack is
the first element which is deleted from the stack
Operations
 PUSH
 POP
 PEEK
Data Structures & Algorithms
Applications of stack:
 Temporary storage structure for recursive operations
 Auxiliary storage structure for nested operations, function calls,
deferred/postponed functions
 Manage function calls
 Evaluation of arithmetic expressions in various programming
languages
 Conversion of infix expressions into postfix expressions
 Checking syntax of expressions in a programming environment
 Matching of parenthesis
 String reversal
 In all the problems solutions based on backtracking.
 Used in depth first search in graph and tree traversal.
 Operating System functions
 UNDO and REDO functions in an editor.
Data Structures & Algorithms
Queue
 first-in, first-out (FIFO) data structure in which the element that is inserted first is
the first one to be taken out.
 elements in a queue are added at one end called the rear and removed from the
other end called the front
Operations
 ENQUEUE, DEQUEUE, PEEK/FRONT,
REAR, isFULL, isEMPTY, SIZE

Applications:
 It is used in breadth search operation in graphs.
 Job scheduler operations of OS like a print buffer queue, keyboard buffer queue to
store the keys pressed by users
 Job scheduling, CPU scheduling, Disk Scheduling
 Priority queues are used in file downloading operations in a browser
 Data transfer between peripheral devices and CPU.
 Interrupts generated by the user applications for CPU
Data Structures & Algorithms
Trees
 An array is a good static data structure that can be accessed randomly
and is fairly easy to implement
 A linked list is dynamic and is ideal for applications that require frequent
operations such as add, delete, and update
 One drawback of linked list is that data access is sequential.
 One of the disadvantages of using an array or linked list to store data is
the time necessary to search for an item
 Both arrays and Linked Lists are linear structures therefore the time
required to search a “linear” list is proportional to the size of the data set
 A tree is a collection of nodes connected by directed (or undirected)
edges.
 A tree is a nonlinear data structure
 A tree can be empty with no nodes or a tree is a structure consisting of
one node called the root and zero or one or more subtrees.
Data Structures & Algorithms
Trees
 A tree has following general properties:
 One node is distinguished as a root;
 Every node (except the root) is connected by a directed edge from exactly one
other node; A direction is: parent -> children
 A is a parent of B, C, D,
B is called a child of A.
on the other hand, B is a parent of E, F, K
 Each node can have arbitrary number of children.
 Nodes with no children are called leaves, or external nodes.
 In the picture, C, E, F, L, G are leaves.
 Nodes, which are not leaves, are called internal nodes.
 Internal nodes have at least one child.
Data Structures & Algorithms
Trees
 Nodes with the same parent are called siblings.
 In the picture, B, C, D are called siblings.
 The depth of a node is the number of edges from the root to the node.
 The depth of K is 2.
 The height of a node is the number of edges from the node to the
deepest leaf. The height of B is 2.
 The height of a tree is a height of a root.

 A general tree is a tree where each node may have zero or more children
(a binary tree is a specialized case of a general tree).
 General trees are used to model applications such as file systems.
Data Structures & Algorithms
Trees
 Nodes with the same parent are called siblings.
 In the picture, B, C, D are called siblings.
 The depth of a node is the number of edges from the root to the node.
 The depth of K is 2.
 The height of a node is the number of edges from the node to the
deepest leaf. The height of B is 2.
 The height of a tree is a height of a root.

 A general tree is a tree where each node may have zero or more children
(a binary tree is a specialized case of a general tree).
 General trees are used to model applications such as file systems.
Data Structures & Algorithms
Binary Trees
 A binary tree is a type of tree data structure where each node can have a
maximum of two child nodes, a left node and a right node
 A parent node, or internal node, in a Binary Tree is a node with one or
two child nodes.
 The left child node is the child node to the left.
 The right child node is the child node to the right.
 The tree height is the maximum number of edges from the root node to
a leaf node.
Data Structures & Algorithms
Types of Binary Trees
Balanced Binary Tree - has at most 1 in difference between its left and right
subtree heights, for each node in the tree.

Complete Binary Tree - has all levels full of nodes, except the last level,
which can also be full, or filled from left to right. The properties of a
complete Binary Tree means it is also balanced.
Data Structures & Algorithms
Types of Binary Trees
Full Binary Tree is a kind of tree where each node has either 0 or 2 child
nodes.

Perfect Binary Tree has all leaf nodes on the same level, which means that
all levels are full of nodes, and all internal nodes have two child nodes. The
properties of a perfect Binary Tree means it is also full, balanced, and
complete.
Data Structures & Algorithms
Binary Tree Traversal
 Going through a Tree by visiting every node, one node at a time
 Start at the first element, or node, and continue to visit the next until you
have visited them all.
 Since a Tree can branch out in different directions (non-linear), there are
different ways of traversing Trees
Breadth First Search (BFS) - the nodes on the same level are visited
before going to the next level in the tree. This means that the tree is
explored in a more sideways direction.
Depth First Search (DFS) - the traversal moves down the tree all the way
to the leaf nodes, exploring the tree branch by branch in a downwards
direction.
Data Structures & Algorithms
Binary Tree Traversal
 There are three different types of DFS traversals:
Pre-order Traversal - done by visiting the root node first, then recursively
do a pre-order traversal of the left subtree, followed by a recursive pre-order
traversal of the right subtree
This traversal is "pre" order because the node is visited "before" the
recursive pre-order traversal of the left and right subtrees

Result: R,A,C,D,B,E,F,G
Data Structures & Algorithms
Binary Tree Traversal
 In-order Traversal - does a recursive In-order Traversal of the left
subtree, visits the root node, and finally, does a recursive In-order
Traversal of the right subtree. This traversal is mainly used for Binary
Search Trees where it returns values in ascending order.
 the node is visited in between the recursive function calls. The node is
visited after the In-order Traversal of the left subtree, and before the In-
order Traversal of the right subtree.

Result: C,A,D,R,E,B,G,F
Data Structures & Algorithms
Binary Tree Traversal
 Post-order Traversal - works by recursively doing a Post-order Traversal
of the left subtree and the right subtree, followed by a visit to the root
node
 What makes this traversal "post" is that visiting a node is done "after" the
left and right child nodes are called recursively.

Result: C,D,A,E,G,F,B,R
Data Structures & Algorithms
Binary Search Tree
 A binary Tree where every node's left child has a lower value, and every
node's right child has a higher value.
 The advantage with Binary Search Trees is that operations like search,
delete, and insert are fast and done without having to shift values in
memory
 The X node's left child and all of its descendants (children, children's
children, and so on) have lower values than X's value
 The right child, and all its descendants have higher values than X's value.
 Left and right subtrees must also be Binary Search Trees
Data Structures & Algorithms
AVL Tree
 A self-balancing binary search tree (BST) where the difference between
the heights of left and right subtrees for any node cannot be more than
one
 The difference between the heights of the left subtree and the right
subtree for any node is known as the balance factor of the node.
 AVL tree is named after its inventors, Georgy Adelson-Velsky and Evgenii
Landis, who published it in their 1962 paper “An algorithm for the
organization of information”
Data Structures & Algorithms
Graphs
 A Graph in data structure is a type of non-linear data structure
 A map is a well-established example of a graph. In a map, various cities
are connected using links. These links can be considered as roads, railway
lines or aerial network
 Example applications of Graphs in real life:
1. Solving Electricity Distribution problem
2. Maps like Cities, Rivers, Countries and so on
3. Water distribution in various areas
4. CAD/CAM applications
5. Finding Disaster Relief Solutions
Data Structures & Algorithms
Basic Concepts of Graphs
 Nodes / Vertices: A graph contains a set of points known as nodes or
vertices
 Edge / Link / Arc: A link joining any two-vertex is known as an edge or
Arc.
 Graph: A graph is a collection of vertices and arcs which connects
vertices in the graph.
D
 A graph G is represented as G = (V, E), where V is Bset of vertices and E is
set of edges.

 Example: Graph G can be defined as G = (V, E) where, E

V = {A,B,C,D,E} and C
A
E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
Data Structures & Algorithms
Graph Terminology
 Vertex: An individual data element of a graph is called as Vertex. Vertex is also
known as node.
In above example graph, A, B, C, D & E are known as vertices.
 Edge: An edge is a connecting link between two vertices. Edge is also known as Arc.
An edge is represented as (starting Vertex, ending Vertex).
Example: In above graph, the link between vertices A and B is represented as
(A,B)
Edges are of three types:
a. Undirected Edge- An undirected edge is a bidirectional edge. If there is an
undirected edge between vertices A and B then edge (A, B) is equal to edge (B,
A).
b. Directed Edge - A directed edge is a unidirectional edge. If there is a
directed
edge between vertices A and B then edge (A, B) is not equal to edge (B,
A).
c. Weighted Edge - A weighted edge is an edge with cost as weight on it.
Data Structures & Algorithms
Graph Terminology
 Degree of a Vertex: The degree of a vertex is said to the number of edges
incident on it, i.e. coming into the vertex
 Outgoing Edge: A directed edge is said to be an outgoing edge on its origin vertex.
 Incoming Edge: A directed edge is said to be an incoming edge on its destination
vertex.
 Degree: Total number of edges connected to a vertex is said to be degree of that
 vertex.
 Indegree: Total number of incoming edges connected to a vertex is said to be
indegree of that vertex.
 Outdegree: Total number of outgoing edges connected to a vertex is said to be
outdegree of that vertex.
 Parallel edges or Multiple edges: If there are two undirected edges to have the
same end vertices, and for two directed edges to have the same origin and the
same destination. Such edges are called parallel edges or multiple edges.
 Self-loop: An edge (undirected or directed) is a self-loop if its two endpoints
coincide.
 Simple Graph: A graph is said to be simple if there are no parallel and self-loop
edges.
Data Structures & Algorithms
Types of Graphs
 Undirected Graph: A graph with only undirected edges is said to be
undirected graph.

 Directed Graph:A graph with only directed edges is said to be directed


graph.

 Complete Graph: A graph in which any V node is adjacent to all other


nodes present in the graph is known as a complete graph. An undirected
graph contains the edges that are equal to edges = n(n-1)/2 where n is
the number of vertices present in the graph. The following figure shows a
complete graph
Data Structures & Algorithms
Types of Graphs
 Regular Graph: Regular graph is the graph in which nodes are adjacent
to each other, i.e., each node is accessible from any other node.

 Weighted Graph: A graph is said to be weighted if there are some non-


negative value assigned to each edges of the graph. The value is equal to
the length between two vertices. Weighted graph is also called a network.
Data Structures & Algorithms
Representing Graphs
 Adjacency Matrix: a representation of a graph using a matrix of size total
number of vertices by total number of vertices
 Rows and columns both represent vertices
 The matrix is filled with either 1 or 0
 1 represents there is an edge from row vertex to column vertex and 0
represents there is not edge from row vertex to column vertex
e.g. for an undirected graph:
Data Structures & Algorithms
Representing Graphs
e.g. for a directed graph:

 The adjacency matrix for an undirected graph is symmetric but the


adjacency matrix for a digraph need not be symmetric.

You might also like