Download Basic Element of Data Structures like linked list, stack and queue

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

Linked list wikipedia , lookup

Red–black tree wikipedia , lookup

Quadtree wikipedia , lookup

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Basic Element of Data Structures like
linked list, stack and queue, trees,
Graph,recursion
Introduction to basic element of data
structure
• Basic technique for improving algorithm is to
structure the data in such a way that the resulting
operation can be efficiently carried out.
• They are
1. Linked list
2. Stack
3. Queue
4. Tree
5. Graph
Linked list
• Linked list is a collection of element in which each
element is pointing to next element.
Types of linked list are
1. Single linked list
2. Double linked list
Operation on linked list are
1. Add
2. Delete
3. Traverse
Stack
• Stack is a list in which insertion & deletion are
taking place at only one end called as top of
the stack
• Stack is a LIFO (Last in first out ) structure.
E
D
C
B
A
Top
Stack
Stack is implemented using
1. Array Representation
2. Linked list representation
Basic operation on stack are
1. Add
2. Delete
3. Stack Top
Operation on stack using array
representation
Algorithm Add(item)
{
If(top ≥ n-1) then
{ write(“Stack is full”);return false;}
Else
{top:=top+1;stack[top]:=item; return true;}
}
Algorithm Delete(item)
{
If(top<0) then
{write(“Stack is empty!”); return false;}
Else
{ item:=stack[top];top:=top-1;return true;}
}
Operation on stack using Linked
representation of stack
Node = record{
Type data; node*link;
}
Algorithm Add(item)
{
Temp:=new node;
If(temp->data):=item;(temp->link):=top;
Top:=temp; return true;
}
Algorithm delete(item)
{
If(top=0) then
{
Write(“Stack is empty!”); return false;}
Else
{item:=(top->data); temp:=top; top:=(top->link); delete temp; return true;}
}
Queue
• An efficient Queue representation can be
obtained by taking an array q[0:n-1] & treating it
as if it were circular
• Element are inserted by increasing the variable
rear to the next free position
• When rear=n-1, the next element is entered at
q[0] in case that spot is free
• Front always point one position counterclockwise
from the first element in the queue
• Time require to add & delete element in queue is
o(1).
Basic queue operation
Algorithm AddQ(item)
{
Rear:=(rear+1) mod n;
If(front=rear)then
{Write(“Queue is full!”);If(front=0) then rear:=n-1;else rear:=rear-1;return false;}
Else
{q[rear]:=item; return true;}
}
Algorithm DeleteQ(item)
{
If(front=rear) then
{ write(“Queue is empty!”); return false; }
Else
{front :=(front + 1) mod n; item:=q[front]; return true; }
}
Tree
• A tree is finite set of one or more nodes such
that there is specially designed node called
the root and remaining node are partitioned
into n≥0 disjoint set.T1, T2,.. Tn, where each of
these subset is a tree. The set set.T1, T2,.. Tn
are called the sub tree of the root.
Level & Degree
•
node (13)
Level
1
A
•
•
•
•
•
•
•
•
•
•
degree of a node
B
leaf (terminal)
nonterminal
E
F
parent
children
K
L
sibling
degree of a tree (3)
ancestor
level of a node
height of a tree (4)
C
G
2
D
H
M
I
J
3
4
Binary tree
• Binary tree is a tree node where each node
have 0, 1 or 2 children
• Example
A
B
D
C
E
F
Binary search tree
• A binary search tree is a binary tree. It may be
empty. If not empty, then it satisfies the
following property.
1. Every element has a key and no two
elements have the same key.
2. The key in the left subtree are smaller than
the key in the root
3. The key in the right subtree are larger than
the key in the root
Graph
• 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 .
• 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
Graph terminology
• Adjacent nodes: two nodes are adjacent if
they are connected by an edge
• Path: a sequence of vertices that connect
two nodes in a graph
• Complete graph: a graph in which every
vertex is directly connected to every other
vertex
Graph terminology (cont.)
• What is the number of edges in a complete
directed graph with N vertices?
N * (N-1)
2
O( N )
Graph terminology (cont.)
• What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
2
O(N )
Graph terminology (cont.)
• Weighted graph: a graph in which each edge
carries a value
Graph implementation
• Array-based implementation
– A 1D array is used to represent the vertices
– A 2D array (adjacency matrix) is used to
represent the edges
Array-based implementation
Graph implementation (cont.)
• Linked-list implementation
– A 1D array is used to represent the vertices
– A list is used for each vertex v which contains the
vertices which are adjacent from v (adjacency list)
Linked-list implementation
Recursion?
• Sometimes, the best way to solve a problem is
by solving a smaller version of the exact same
problem first
• Recursion is a technique that solves a problem
by solving a smaller problem of the same type
Example of Recursion: factorial
function
• Recursive implementation
int Factorial(int n)
{
if (n==0) // base case
return 1;
else
return n * Factorial(n-1);
}
Coding the factorial function (cont.)
• Iterative implementation
int Factorial(int n)
{
int fact = 1;
for(int count = 2; count <= n; count++)
fact = fact * count;
return fact;
}
Application
• Application of stack is decimal to binary no.
conversion, parse a tree.
• Application of Queue is Memory queue
• Application of tree is MST
• Application of graph is Network