You are on page 1of 47

Data Structures using C++

[3rd Semester]

By,
Vinni Sharma
Associate Professor
BIT Durg

1
Unit V

Linked List, Trees and


Graphs
LINKED LIST
• Linked list is a one way list, which is a linear collection of data elements, called nodes,
where the linear order is given by means of pointers.
• Each node is divided is into two parts : The first part contains the data, and the second part
, called the link field, contains the address of the next node in the list.
• A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations.
• The elements in a linked list are linked using pointers as shown in the below image:
• In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.
• It can be implemented using classes or structures
LINKED LIST
• The null pointer denoted in the diagram, signals the
end of the list.
• The linked list also contains a list pointer variable –
called START or HEAD which contains the address of
the first node in the list.
• Hence there is an arrow drawn from the START or
HEAD to the first node.
• When START contains null then there is no elements in
the list and the list is empty
LINKED LIST Example
S.No INFO LINK

2
START = 9
3 O 6 INFO[9]=N LINK[9]=3
INFO[3]=O LINK[3]=6
4 T 0
INFO[6]=Y LINK[6]=7
5 INFO[7]=X LINK[7]=4
INFO[4]=T LINK[4]=0
6 Y 7

7 X 4
INFO[PTR]contains the information part of the LIST.
8
LINK[PTR] contains the nextpointer field of the LIST.
9 N 3
TRAVERSING A LINKED LIST
• Let LIST be a LINKED LIST in the memory stored in
the linear arrays INFO and LINK and START is
pointing to the first element in the LIST, NULL is
indicating the end of the LIST.
• PTR is the pointer which points to the current node.
• LINK[PTR] points to the next node to be processed.
PTR =LINK[PTR]
Above statement moves the pointer to the next
node in the LIST.
TRAVERSING A LINKED LIST Steps
1. Initialize PTR or START.
2. Process INFO[PTR]
(Information at the first Node)
3. PTR = LINK[PTR] (Update PTR so that PTR points to
the second node).
4. again process INFO[PTR](The information at second
node).
5. Again update PTR by PTR := LINK[PTR] and process
INFO[PTR] and so on…. Until PTR = NULL, which
indicates the end of the LIST.
Single linked lists

Double Linked Lists

Circular Lists
Insertion at the top
head 48 17 142 //
Steps:
1. Create a Node
2. Set the node data Values
3. Connect the pointers

head 93
Insertion at the end
head 48 17 142 //
Steps:
1. Create a Node
2. Set the node data Values
3. Connect the pointers
Insertion in the middle of the list

Steps:
1. Create a Node
2. Set the node data Values
3. Break pointer connection
4. Re-connect the pointers
Deletion from the top
head

6 4 17 42

head

6 4 17 42
head

4 17 42
Deletion from the end
head

6 4 17 42
head

6 4 17 42

head

6 4 17
Deletion from the middle
head

4 17 42

head

4 17 42
head

4 42
SEARCHING an element in A LINKED LIST
Trees
• Linked lists, stacks and queues are linear data structures.
• A tree is a nonlinear, two-dimensional data structure in
which each element is attached to one or more elements
directly beneath it.
• Tree nodes may contain two or more links.eg
Examples of Trees with levels
Basic Terminology in Trees
• Computer scientists normally draw trees from the root node down—the opposite of
how trees grow in nature.

• The connections between elements are called branches.

• A tree has a single root, called root node, which is shown at the top of the tree i.e.
root is always at the highest level 0.

• Each node has exactly one node above it, called parent. Eg: A is the parent of B,C
and D.

• The nodes just below a node are called its children. ie. child nodes are one level
lower than the parent node.

• A node which does not have any child called leaf or terminal node.

• Nodes with at least one child are called non terminal or internal nodes.
Basic Terminology in Trees
• The child nodes of same parent are said to be siblings.

• A path in a tree is a list of distinct nodes in which successive nodes are


connected by branches in the tree.

• The length of a particular path is the number of branches in that path.

• The degree of a node of a tree is the number of children of that node. The total
number of sub-trees attached to the node is called the degree of the node.Eg:
For node A degree is 3. For node K degree is 0

• The maximum number of children a node can have is often referred to as the
order of a tree.

• The height or depth of a tree is the length of the longest path from root to any
leaf.
Basic Terminologies with diagram
Applications of Trees

+
Example Arithmetic Expression:

A + (B * (C / D) ) A *

Tree for the above expression:


B /
• Used in most compilers
• No parenthesis needed – use tree structure
• Can speed up calculations e.g. replace
/ node with C/D if C and D are known C D
• Calculate by traversing tree

21
Binary Tree
Binary tree is a tree in which each node has at most two children,
a left child and a right child. Thus the order of binary tree is 2.
A binary tree is either empty or consists of
a) a node called the root
b) left and right sub trees are themselves binary trees.
A binary tree is a finite set of nodes which is either empty or
consists of a root and two disjoint trees called left sub-tree and
right sub-tree.
In binary tree each node will have one data field and two pointer
fields for representing the sub branches.
The degree of each node in the binary tree will be at the most
two.
Binary Tree

The root node (node B) is the first node in a tree.


Each link in the root node refers to a child (nodes A and D).
The left child (node A) is the root node of the left subtree (which contains only node A),
and the right child (node D) is the root node of the right subtree (which contains nodes D
and C).
The children of a given node are called siblings (e.g., nodes A and D are siblings).
A node with no children is a leaf node (e.g., nodes A and C are leaf nodes).
Types of Binary Trees:
1.Left skewed binary tree: If the right sub-tree is missing in
every node of a tree we call it as left skewed tree.
Types of Binary Trees:
2.Right skewed binary tree: If the left sub-tree is missing in
every node of a tree we call it is right subtree.
Types of Binary Trees:
3. Complete binary tree: The tree in which degree of each
node is two is called a complete binary tree. In a complete
binary tree there is exactly one node at level 0, two nodes at
level 1 and four nodes at level 2 and so on.
TRAVERSING A BINARY TREE
Traversing a tree means that processing it so that each node is
visited exactly once.
A binary tree can be traversed a number of ways.
The most common tree traversals are In-order, Pre-order and
Post-order
Pre-order Root | Left | Right
1.Visit the root
2.Traverse the left sub tree in pre-order
3.Traverse the right sub tree in pre-order.
In-order Left | Root | Right
1.Traverse the left sub tree in in-order
2.Visit the root
3.Traverse the right sub tree in in-order.
Post-order Left | Right | Root
1.Traverse the left sub tree in post-order
2.Traverse the right sub tree in post-order.
3.Visit the root
Binary Search Trees
In the simple binary tree the nodes are arranged in any fashion. Depending on
user‘s desire the new nodes can be attached as a left or right child of any desired
node.
In such a case finding for any node is a long cut procedure, because in that case
we have to search the entire tree.
And thus the searching time complexity will get increased unnecessarily.
So to make the searching algorithm faster in a binary tree we will go for building
the binary search tree. The binary search tree is based on the binary search
algorithm.
While creating the binary search tree the data is systematically arranged.
That means values at left sub-tree < root node value < right sub-tree values.
A binary search tree (with no duplicate node values) has the characteristic that
the values in any left subtree are less than the value in its parent node, and the
values in any right subtree are greater than the value in its parent node.
Binary Search Tree
Dictionary Data Structure

• Search tree 8

property
5 11
– all keys in left
subtree smaller
2 6 10 12
than root’s key
– all keys in right
4 7 9 14
subtree larger than
root’s key
13
– result:
• easy to find any 30
In Order Listing

10 visit left subtree


visit node
5 15
visit right subtree
2 9 20

7 17 30

In order listing:
25791015172030

31
Operations on Binary Search Tree
The basic operations which can be performed on
binary search tree are.
1. Insertion of a node in binary search tree.
2. Deletion of a node from binary search tree.
3. Searching for a particular node in binary
search tree.
AVL Trees t

Balanced Tree 5
6

balance = height(left subtree) - height(right subtree)


• convention: height of a “null” subtree is -1
• zero everywhere  perfectly balanced
• small everywhere  balanced enough

33
AVL Tree
Dictionary Data Structure

8
Binary search tree properties
Balance of every node is -1 b 
5 11
1
Tree re-balances itself after
every insert or delete 2 6 10 12

4 7 9 13 14

15

34
Graph Data Structure
• A data structure that consists of a set of nodes
(vertices) and a set of edges that relate the nodes
to each other
• The set of edges describes relationships among
the vertices
Formal definition of graphs
A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
Directed vs. undirected graphs
When the edges in a graph have no direction,
the graph is called undirected
Directed vs. undirected graphs (cont.)
When the edges in a graph have a direction,
the graph is called directed (or digraph)

if the graph is directed,


the order of the vertices
in each edge is
important !!

E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)


Trees vs graphs
Trees are special cases of graphs!! All Trees are graphs but Not all graphs are
trees!!
FIGURE 12-3 Various undirected graphs

FIGURE 12-4 Various directed graphs


Example of Graph: Airlines Route Map
Graph Traversals
Processing a graph
– Requires ability to traverse the graph
Traversing a graph
– Similar to traversing a binary tree
• A bit more complicated
Two most common graph traversal algorithms
– Depth first traversal
– Breadth first traversal

42
Depth First Traversal
Similar to binary tree preorder traversal
General algorithm

43
Depth First Traversal (cont’d.)
General algorithm for depth first traversal at a
given node v [Recursive algorithm]

44
Breadth First Traversal
Similar to traversing binary tree level-by-level
– Nodes at each level
• Visited from left to right
– All nodes at any level i
• Visited before visiting nodes at level i + one
Breadth-First Traversal
BFS characteristics
– Nodes being worked on maintained in a FIFO Queue, not a stack
– Iterative style procedures often easier to design than recursive
procedures
Put root in a Queue
Repeat until Queue is empty:
Dequeue a node
Process it
Add it’s children to queue
QUEUE
a
bcde a
cdefg
defg
b c d e
efghij
fghij
ghij h i j
f g
hijk
ijk
jkl
k l
kl
l

You might also like