UNIT 5 Linked List, Trees and Graphs
UNIT 5 Linked List, Trees and Graphs
UNIT 5 Linked List, Trees and Graphs
[3rd Semester]
By,
Vinni Sharma
Associate Professor
BIT Durg
1
Unit V
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
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.
• 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.
• 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 *
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
• 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
7 17 30
In order listing:
25791015172030
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
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)
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