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

Non Linear Data Structure 1

Non-linear data structures, such as trees and graphs, do not arrange data elements sequentially, allowing for complex relationships and hierarchical organization. Trees, including binary trees and B-trees, have specific properties and applications in file systems, data compression, and database indexing, while graphs represent relationships between entities and are useful in social network analysis and network monitoring. Each structure has its advantages and disadvantages, impacting their efficiency and suitability for various applications.

Uploaded by

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

Non Linear Data Structure 1

Non-linear data structures, such as trees and graphs, do not arrange data elements sequentially, allowing for complex relationships and hierarchical organization. Trees, including binary trees and B-trees, have specific properties and applications in file systems, data compression, and database indexing, while graphs represent relationships between entities and are useful in social network analysis and network monitoring. Each structure has its advantages and disadvantages, impacting their efficiency and suitability for various applications.

Uploaded by

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

Non-Linear Data

Structure
It is a form of data structure where the data elements
don’t stay arranged linearly or sequentially.

Since the data structure is non-linear, it does not


involve a single level. Therefore, a user can’t traverse
all of its elements in a single run.
Type of Non-Linear Data
Structure
 Trees and Graphs are the types of non-
linear data structure.
Trees

 TreeData Structure is a hierarchical data


structure in which a collection of elements known
as nodes are connected to each other via edges
such that there exists exactly one path between
any two nodes.
Properties of Trees

1.Root: A binary tree has a root node, which is the


topmost node in the tree. It is the starting point for
traversing the tree.

2. Nodes: Each node in a binary tree has at most two


children, referred to as the left child and the right child.

3. Number of edges: An edge can be defined as the


connection between two nodes. If a tree has N nodes
then it will have (N-1) edges.
• 4. Depth of a node: The depth of a node is
defined as the length of the path from the root to
that node. Each edge adds 1 unit of length to the
path. So, it can also be defined as the number of
edges in the path from the root of the tree to the
node.

• 5. Height of a node: The height of a node can be


defined as the length of the longest path from the
node to a leaf node of the tree.

• 6. Leaves: Nodes without any children are called


7. Subtrees: A binary tree is composed of subtrees. A subtree is
a tree formed by selecting a node and all its descendants,
including the node itself.

8. Degree: The degree of a node is the number of children it


has. In a binary tree, nodes have a degree of at most 2.

9. Balanced Binary Tree: A binary tree is considered balanced


if the height of the left

10. Full Binary Tree: A binary tree is full if each node has either
0 or 2 children. In other words, every node is either a leaf or
has two children. and right subtrees of any node differ by at
most one.
11. Complete Binary Tree: A binary tree is complete
if all levels, except possibly the last, are completely
filled, and all nodes are as left as possible.
 Theseproperties provide a foundation for
understanding and working with binary trees in
various algorithms and applications, such as
searching, sorting, and hierarchical data
representation.
Application of Tree Data Structure

 File
System: This allows for efficient navigation and
organization of files.

 Data Compression: Huffman coding is a popular


technique for data compression that involves
constructing a binary tree where the leaves represent
characters and their frequency of occurrence. The
resulting tree is used to encode the data in a way that
minimizes the amount of storage required.
 Compiler Design: In compiler design, a syntax
tree is used to represent the structure of a
program.

 Database Indexing: B-trees and other tree


structures are used in database indexing to
efficiently search for and retrieve data.
Advantages of Tree

 Treeoffer Efficient Searching Depending on the


type of tree, with average search times of O(log
n) for balanced trees like AVL.

 Treesprovide a hierarchical representation of


data, making it easy to organize and navigate
large amounts of information.

 The recursive nature of trees makes them easy


to traverse and manipulate using recursive
algorithms.
Disadvantages of Tree

• Unbalanced Trees, meaning that the height of the


tree is skewed towards one side, which can lead
to inefficient search times.
• Trees demand more memory space
requirements than some other data structures like
arrays and linked lists, especially if the tree is very
large.
• The implementation and manipulation of trees
can be complex and require a good understanding
of the algorithms.
Types of Tree

1.Binary Tree
2.Binary Search Tree
3.AVL Tree
4.B-Tree
Binary Tree

A binary tree is a tree data structure in which


each parent node can have at most two
children. Each node of a binary tree consists of
three items:

 data item

 address of left child


Application of Binary Trees
 Game AI: Binary trees can be used to implement game
AI, where each node represents a possible move in the
game. The AI algorithm can search the tree to find the
best possible move.
 Database systems: Binary trees can be used to store data
in a database system, with each node representing a
record. This allows for efficient search operations and
enables the database system to handle large amounts of
data.
 Decision trees: Binary trees can be used to implement
decision trees, a type of machine learning algorithm used
for classification and regression analysis.
Binary Search Tree
A binary Search Tree is a node-based binary tree
data structure that has the following properties:
The left subtree of a node contains only nodes with
keys lesser than the node’s key.

The right subtree of a node contains only nodes with


keys greater than the node’s key.

The left and right subtree each must also be a binary


search tree.
Real-time applications of Binary Trees

 File explorer.
 Used as the basic data structure in Microsoft Excel
and spreadsheets.
 Editor tool: Microsoft Excel and spreadsheets.
 Evaluate an expression
Advantages of Binary Tree

 Easy to implement: Binary trees are relatively easy


to implement and understand, making them a popular
choice for a wide range of applications.

 Ordered traversal: The structure of binary trees


enables them to be traversed in a specific order, such
as in-order, pre-order, and post-order. This allows for
operations to be performed on the nodes in a specific
order, such as printing the nodes in sorted order.
Disadvantages of Binary Tree
 Limited structure: Binary trees are limited to two child nodes
per node, which can limit their usefulness in certain applications.
For example, if a tree requires more than two child nodes per
node, a different tree structure may be more suitable.

 Space inefficiency: Binary trees can be space inefficient when


compared to other data structures. This is because each node
requires two child pointers, which can be a significant amount of
memory overhead for large trees.
AVL Tree

 AVL tree is a self-balancing binary search tree in


which each node maintains extra information called a
balance factor whose value is either -1, 0 or +1.

 Operations on an AVL Tree:


• Insertion
• Deletion
• Searching [It is similar to performing a search in BST]
 Balance Factor = (Height of Left Subtree - Height
of Right Subtree) or (Height of Right Subtree -
Height of Left Subtree)

 Theself balancing property of an avl tree is


maintained by the balance factor. The value of
balance factor should always be -1, 0 or +1.
Applications of BST

A Self-Balancing Binary Search Tree is used to


maintain sorted stream of data. For example, suppose
we are getting online orders placed and we want to
maintain the live data (in RAM) in sorted order of
prices.
 Forexample, we wish to know number of items
purchased at cost below a given cost at any moment.
Or we wish to know number of items purchased at
higher cost than given cost.
B-Tree

A B-tree is a self-balancing tree where all the leaf


nodes are at the same level which allows for
efficient searching, insertion and deletion of
records.

 Because of all the leaf nodes being on the same


level, the access time of data is fixed regardless
of the size of the data set.
Applications of B-Trees

 Itis used in large databases to access data stored


on the disk
 Searching for data in a data set can be achieved in
significantly less time using the B-Tree
 With the indexing feature, multilevel indexing can
be achieved.
 Most of the servers also use the B-tree approach.
 B-Treesare used in CAD systems to organize and
search geometric data.
 B-Treesare also used in other areas such as natural
language processing, computer networks, and
Advantages of B-Trees

• B-Trees have a guaranteed time complexity of O(log


n) for basic operations like insertion, deletion, and
searching, which makes them suitable for large data
sets and real-time applications.
• B-Trees are self-balancing.
• High-concurrency and high-throughput.
• Efficient storage utilization.
Disadvantages of B-Trees

• B-Trees are based on disk-based data structures


and can have a high disk usage.
• Not the best for all cases.
• Slow in comparison to other data structures.
 Insertion and Deletion:
B-Tree Insertion
B-Tree Deletion
BINARAY SEARCH TREE
CALCULATIONS
There are several key calculations and aspects is
performed on Binary search tree. These operations
and calculations allow us to efficiently manage and
manipulate the tree.
 1. Insertion
Insertion in a BST involves adding a node while maintaining the BST property:
• All nodes in the left subtree of a node must have values less than the node’s
value.
• All nodes in the right subtree must have values greater than the node’s value.

 2. Search
 Searching in a BST follows the same logic as insertion:
• Start at the root node.
• If the target value is less than the current node's value, move to the left child.
• If the target value is greater, move to the right child.
• Repeat this process until the value is found or a NULL pointer is reached,
indicating the value is not present.
 3. Height of a BST
he height of a BST (hhh) is the number of edges in the longest path from the root to a
leaf node.

 Maximum Number of Nodes:

• Formula: 2h+1 −1

 Minimum Number of Nodes:


Formula: Nmin​= h+1

 Depth of the Nodes


The depth of a node is the number of edges from the root to the node.

NB: In a Binary Search Tree (BST), the number of nodes can vary depending on the tree's
height and how well it is balanced.
1. Draw the BST where the data value at each node
is an integer and the values are entered in the
following order: 9 7 12
2. )
Draw the BST after the following insertions have
been done in the tree of part (1) 6 8 10 14
3. calculate
number nodes the height, maximum l
nodes minimum nodes and depth nodes
 Constructing the Binary Search Tree (BST) based on the given sequence of
insertions and then calculate the required properties.

 Insert the values: 9, 7, 12

9
/\
7 12

Insert the values: 6, 8, 10, 14

6 is less than 9 and less than 7, so it goes to the left of 7.


8 is less than 9 but greater than 7, so it goes to the right of 7.
10 is greater than 9 but less than 12, so it goes to the left of 12.
14 is greater than 9 and greater than 12, so it goes to the right of 12.
9
/ \
7 12
/\ /\
6 8 10 14
The number of nodes in the tree is the total number of
nodes inserted into the tree:

Nodes are: 9, 7, 12, 6, 8, 10, and 14.


Therefore, the number of nodes is 7.
 Summary of Calculations:
1. Number of nodes: 7
2. Height: 3
3. Maximum length: 2
4. Minimum length: 1
5. Depth of nodes:
1. Depth of 9: 0
2. Depth of 7: 1
3. Depth of 12: 1
4. Depth of 6: 2
5. Depth of 8: 2
6. Depth of 10: 2
7. Depth of 14: 2
Calculate the number of nodes,
maximum and minimum height,
and depth in the BST formed from
the values 20, 15, 18, 12, 24, 22,
26, and 28. Provide the algorithm
and C++ code.
Graph
A Graph is a non-linear data structure consisting of
vertices and edges. The vertices are sometimes also
referred to as nodes and the edges are lines or arcs
that connect any two nodes in the graph. More
formally a Graph is composed of a set of vertices( V )
and a set of edges( E ). The graph is denoted
by G(V, E).

 Graphdata structures are a powerful tool for


representing and analyzing complex relationships
between objects or entities.
 They are particularly useful in fields such as
 Social Network analysis
 Recommendation systems, and computer
networks.
 In the field of sports data science, graph data
structures can be used to analyze and understand
the dynamics of team performance and player
interactions on the field.
Components of a Graph

• Vertices: Vertices are the fundamental units of the


graph. Sometimes, vertices are also known as vertex
or nodes. Every node/vertex can be labeled or
unlabelled.

• Edges: Edges are drawn or used to connect two nodes


of the graph. It can be ordered pair of nodes in a
directed graph. Edges can connect any two nodes in
any possible way. There are no rules. Sometimes,
edges are also known as arcs. Every edge can be
labelled/unlabelled.
Types Of Graph

 1. Null Graph
A graph is known as a null graph if there are
no edges in the graph.

 2. Trivial Graph
Graph having only a single vertex, it is also the
smallest graph possible.
 3. Undirected Graph
A graph in which edges do not have any direction. That is
the nodes are unordered pairs in the definition of every
edge.

 4. Directed Graph
A graph in which edge has direction. That is the nodes
are ordered pairs in the definition of every edge.
Representation of Graphs

 There are two ways to store a graph:


• Adjacency Matrix
• Adjacency List
 Adjacency Matrix
In this method, the graph is stored in the form of the 2D
matrix where rows and columns denote vertices. Each
entry in the matrix represents the weight of the edge
between those vertices.
 Adjacency List
This graph is represented as a collection of linked lists.
There is an array of pointer which points to the edges
connected to that vertex.
Real-Time Applications of Graph:
• Social media analysis: Social media platforms
generate vast amounts of data in real-time, which can
be analyzed using graphs to identify trends, sentiment,
and key influencers. This can be useful for marketing,
customer service, and reputation management.

• Network monitoring: Graphs can be used to monitor


network traffic in real-time, allowing network
administrators to identify potential bottlenecks,
security threats, and other issues. This is critical for
ensuring the smooth operation of complex networks.
• Financial trading: Graphs can be used to analyze
real-time financial data, such as stock prices and
market trends, to identify patterns and make trading
decisions. This is particularly important for high-
frequency trading, where even small delays can have
a significant impact on profits.

• Internet of Things (IoT) management: IoT


devices generate vast amounts of data in real-time,
which can be analyzed using graphs to identify
patterns, optimize performance, and detect
anomalies. This is important for managing large-scale
Advantages of Graph
• Representing complex data: Graphs are
effective tools for representing complex data,
especially when the relationships between the
data points are not straightforward. They can help
to uncover patterns, trends, and insights that may
be difficult to see using other methods.
• Efficient data processing: Graphs can be
processed efficiently using graph algorithms,
which are specifically designed to work with graph
data structures. This makes it possible to perform
complex operations on large datasets quickly and
• Network analysis: Graphs are commonly used in
network analysis to study relationships between
individuals or organizations, as well as to identify
important nodes and edges in a network. This is
useful in a variety of fields, including social sciences,
business, and marketing.

• Pathfinding: Graphs can be used to find the shortest


path between two points, which is a common problem
in computer science, logistics, and transportation
planning.
Disadvantages of Graph
• Limited representation: Graphs can only represent
relationships between objects, and not their properties or
attributes. This means that in order to fully understand the data,
it may be necessary to supplement the graph with additional
information.
• Difficulty in interpretation: Graphs can be difficult to interpret,
especially if they are large or complex. This can make it
challenging to extract meaningful insights from the data, and may
require advanced analytical techniques or domain expertise.
• Scalability issues: As the number of nodes and edges in a graph
increases, the processing time and memory required to analyze it
also increases. This can make it difficult to work with large or
complex graphs.

You might also like