+ All Categories
Home > Documents > C&DS Unit-VII and VIII (Data Structures)

C&DS Unit-VII and VIII (Data Structures)

Date post: 09-Apr-2018
Category:
Upload: n-janaki-ram
View: 220 times
Download: 0 times
Share this document with a friend

of 21

Transcript
  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    1/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:1

    DATA STRUCTURES

    A data structure is an organized collection of data along with the allowed operations on that. In

    other words a data structure is an arrangement of data in a computer's memory or even disk

    storage. An example of several common data structures are arrays, linked lists, queues, stacks

    and binary trees.

    Characteristics of Data Structures:

    Classifications of Data Structures can be done in many ways. One way is to classifybased on their linearity.

    Linear Data Structures

    In a Linear Data Structure the data items are arranged in a linear sequence.

    Ex: Array, stack, queue, linked list

    Non Linear Data Structures

    The data items are not in a linear sequence.

    Ex: Tree, graph

    Data

    Structure

    Advantages Disa

    Array Quick insertsFast access if index known

    Slow searchSlow deletesFixed size

    Ordered Array Faster search than unsorted array Slow insertsSlow deletesFixed size

    Stack Last-in, first-out access Slow access to other items

    Queue First-in, first-out access Slow access to other items

    Linked List Quick inserts, Quick deletes Slow search

    Binary Search

    Tree

    Quick search, Quick inserts and Quick

    deletes (If the tree remains balanced)

    Deletion algorithm is complex

    Graph Best models real-world situations Some algorithms are slow

    and very complex

    NOTE: The data structures shown above (with the exception of the array) can be thought

    of as Abstract Data Types (ADTs).

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    2/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:2

    Abstract Data Types:

    An Abstract Data Type (ADT) is more a way of looking at a data structure: focusing on

    what it does and ignoring how it does its job. A stack or a queue is an example of an ADT. It is

    important to understand that both stacks and queues can be implemented using an array. It is also

    possible to implement stacks and queues using a linked list. This demonstrates the "abstract"

    nature of stacks and queues: how they can be considered separately from their implementation.

    Tree and Graph are also ADTs.

    Arrays

    An array is a group of logically related data items of the same data-type addressed by acommon name and all the items are stored in contiguous memory locations.

    The computer does not need to keep track of the address of every element of the array butneeds to keep track only of the address of the first element of the array, using that address

    the computer calculates the address of any element of array by the following formula,

    Required location=starting address of array + (data type * scale factor) Access the content of any specified location without scanning any other element of an

    array. The elements are accessed by specifying the subscript or index.

    It is very fast to access. Programming length decreases i.e., single operation changes all the data of an array. It holds the data as well as addresses of structures. Multi-dimensional arrays are very powerful for several functions. Searching is done very fast.

    Some of the basic operations that can be performed on the arrays are: Creating an array. Deleting or inserting an element in the array. Sort the elements of the array. Search an element in an array. Displaying (visiting) the elements of array. Insertion or deletion at the specified location.

    Linked Lists Memory is allocated statically for an array. An estimation of the maximum size of the list

    is required which wastes lot of space if not used. This also limits the user not to store

    more than maximum size specified. Insertion and deletion are expensive because

    insertion at first requires first pushing the entire array down one spot to make a room,

    similarly deletion requires pushed up the list.

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    3/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:3

    To avoid the disadvantages of arrays introduced a new concept called linked list, inwhich elements are not stored in a contiguously locations. Linked list is also called as

    self-referential class or one-way list.

    The linked list consists of a series of structures, which are not necessarily adjacent inmemory. Each structure contains the element and a pointer to a structure containing its

    successor. The Previous element to the tail of the list is called its predecessor. Clearly

    head does not have a predecessor and tail (last element) does not have successor. We call

    this the Next pointer.

    The last cells Next pointer points to NULL. Combination of data and pointer to nextstructure is also called as node.

    Some of the basic operations that can be performed on a list are

    Create a list

    Search for an element in a list Delete an element from a list ( at beg, at end, at given location) Add an element at a specified location of a list ( at beg, at end, at given location). Sort the list Display (Traversal) the elements the list Determine the size or no of elements in a list

    The general declaration of a node in C is as follows:

    struct node

    {Element_type data;

    struct node *next; //self referential structure

    };

    typedef struct node NODE;

    NODE *list;

    We can form a linked list using pointers the structure of the list may be as shown below.

    A List with Pointer values (Addresses are assumed)

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    4/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:4

    Double Linked List:

    Sometimes it is convenient to traverse lists backwards. The standard implementation(single linked list) does not help here.

    The cost of this is an extra link, which adds to the space requirements and also doublesthe cost of instructions and deletions because there are more pointers to fix.

    On the other hand it simplifies deletion, because you no longer have to refer to a key byusing a pointer to the previous cell.

    Declaration of the node is:

    struct node

    {

    struct node *previous;

    Element_type data;

    struct node *next;

    };typedef struct node NODE;

    NODE *list;

    A Sample Double Linked List:

    Circular Linked List:

    The linked list in which the last element (or tail) points back to the head (or first element)

    is called Circular Linked List.

    Single Circular Linked List:

    Note: Please refer Lab Exercise 13 and 14 for Singly Linked List and Doubly Linked List

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    5/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:5

    Stack

    It is a list of elements to which additions and deletions can only be made at one end-thetop.

    Consequently, the stack becomes a last-in-first-out (LIFO) data structure; the lastelement added is the first to be removed.

    When we add an item to the top of the stack, it is called a PUSHoperation and when weremove an item from the top, it is called aPOPoperation.

    The stack data structure maintains a stack pointer always pointing to its top. After pushing an item, the stack pointer moves up to point always to the last item added. Similarly, after popping an item, the stack pointer moves down to the next last item in the

    stack.

    Stack Model

    Stack can be implemented in either fixed size stack or Dynamic size stack. In fixed size stack we use arrays and for dynamic size stack we use linked list or pointers

    to implement stacks

    Array Implementation of stack

    A sample Stack

    3

    2

    1

    After you Push 4 the Stack would be

    4

    3

    2

    1

    Top

    Top

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    6/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:6

    When you Pop the Stack would be

    3

    2

    1

    Linked List Implementation of Stack or dynamic size stack:

    The implementation of a stack can also use a single linked list. We perform a push by inserting at the front of the list. We perform a pop by deleting the element at the front of the list. A top operation merely examines the element at the front of the list, returns its value.

    Eg:

    A sample Stack

    After you Push 4 the stack would be

    When you Pop, the Stack would be

    Note: Please refer to Lab Exercise 15 for implementation of stack.

    Top

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    7/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:7

    Applications of Stacks:

    Compilers will take the help of stack for checking syntax errors (Symbol Balancing) Stacks were used in evaluating postfixexpressions (Reverse polish notation). Stacks are used in conversion of infix expressions to postfix expressions. Stacks are used for matching brackets in an expression. Stacks are for memory management during the function call. When there is a function

    call, all the important information that needs to be saved, such as register values and the

    return addresses are saved onto the stack. The information saved is called either an

    activation record or stack frame.

    Queue

    Like Stacks, Queues are lists. With a queue, however, insertion is done at one end calledrear end, where as deletion is performed at the other end called front end.

    Queue can be called FIFO (First in First out) List. The basic operations on a queue are

    o Insertion (Enqueue), which inserts an element at the end of the list called the rearo Deletion (Dequeue), which deletes the elements at the start of the list called front.

    Queues can also be implemented using arrays (static size) or linked lists (dynamic size).Array Implementation of Queue

    Consider a queue, which can add a maximum of five elements

    After Create

    0 1 2 3 4

    Front = 0, Rear = 0

    After adding 5, 7, and 9 in same sequence the queue will be

    5 7 9

    0 1 2 3 4

    Front Rear

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    8/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:8

    After a Dequeue operation

    7 9

    0 1 2 3 4

    Front Rear

    Applications of queues:

    Uses of Queues in operating systems: Operating system maintains several queues likeready queue, device queue etc. for handling multiple processes.

    Use of Queue in Network Ex: for Queuing jobs in file Server. A Whole branch of mathematics, known as queuing theory, deals with computing,

    probabilistically, how long users expect to wait on a line, how long the line gets, and

    other such questions.

    Note: Please refer to Lab Exercise 16 for implementation of Queue.

    Circular Queue

    There is one potential problem with the linear queue. Once the queue is full, even after some

    deletion operations the Queue appears to be full. (In the above illustration, suppose rear = 4 and

    front = 2 for 5 cell queue the queue appears to be full but not). Then you cannot insert until the

    queue is completely empty.

    The Simple solution is that whenever front and rear gets to the end of the array, it is

    wrapped around to the beginning. This is known as circular Queue.

    Consider a queue which is full

    5 7 9 3 10

    0 1 2 3 4

    Front Rear

    Font=0 and Rear = 4

    After a deletion operation:7 9 3 10

    0 1 2 3 4

    Front Rear

    Font=1 and Rear = 4

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    9/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:9

    Now insertion can be done at rear because in circular queue once the rear or front reaches the

    maximum they wrap around. Therefore rear will wrap around to 0 (Front should not be 0).

    After an insertion operation (inserting 30):

    30 7 9 3 10

    0 1 2 3 4

    Rear Front

    Font=1 and Rear = 0

    Program for implementing Circular Queue:

    #include

    #include//for exit(0) function

    #define MAX 5

    int q[MAX],f=-1,r=-1; //declare an array for queue and 2 variables for front and rear end.

    void insert(int n) //a function to insert element into the queue at the front end

    {

    if((r+1)%MAX==f) //if rear pointer reaches maximum index of variable, insertion cannot be done

    printf("\n\nQue is full insertion cannot be done at this stage");

    else

    {

    if(f==-1) //if queue is empty point front to first element

    f=0;r=(r+1)%MAX; //increment the rear pointer and add the new element to array

    q[r]=n;

    printf("\n\nElement %d is inserted successfully\n",n);

    }

    }

    void del() //function to delete a element from front end

    {

    if(f==-1) //queue is empty when f=-1

    printf("\n\nThere are no elements in Que to delete.The que is empty\n");

    else

    {

    printf("\n\n The element deleted is %d.",q[f]); //display the deleted front element and

    increment front

    if(f==r) //only one element is present in queue and it is deleted

    f=r=-1;//queue is empty so initialize f and r

    else

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    10/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:10

    f=(f+1)%MAX;

    }

    }

    void disp() //function to traverse the queue and display the values of it

    {

    int i;

    if(f==-1) //check if the queue is empty

    printf("\n\nThe queue is empty.");

    else //if queue is not empty

    {

    printf("\n\nThe elements of the Queue are:\n\n");

    for(i=f;i!=r;i=(i+1)%MAX)//start from front and move till rear but not rear element

    printf("%d ",q[i]); //display each element

    printf("%d ",q[r]); //display the last element

    }

    }

    void main()

    {

    char ch;

    int n;

    while(1)

    {

    clrscr();printf("\n\n\n\n\t*****MENU*****\n\n\n\n");

    printf(" 1: Insert at rear end.\n\n 2: Delete at front end.\n\n 3: Dispaly the elements.\n\n 4:

    Terminate the program\n\n\n\nPlease enter ur choice: ");

    ch=getch();

    clrscr();

    switch(ch)

    {

    case '1': printf("\n\n\n\n\n\n\t****INSERTION*****\n\n\nEnter the element to inserted: ");

    scanf("%d",&n);

    insert(n);

    break;

    case '2': printf("\n\n\n\n\n\n\t****DELETION*****\n\n\n");

    del();

    break;

    case '3': printf("\n\n\n\n***DISPLAY***\n\n\n");

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    11/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:11

    disp();

    break;

    case '4': printf("\n\n\n\n\tARE U SURE TO EXIT?\n\n\tPress 'n' if no : ");

    ch=getch(); //take confirmation of exiting

    if(ch!='n') //if user enter n to indicate not to exit, skip exiting and continue to menu

    exit(0);

    break;

    default : printf("\n\n\n\tINVALID OPTION entered");

    }

    printf("\n\n\n\n\nPress any key for main MENU....");

    getch();

    }

    }

    Trees

    Def: A tree is a collection of nodes. A Tree consists of a distinguished node r, called the root,

    and zero or more nonempty (sub) trees T1, T2Tk each of whose roots are connected by a

    directed edge from r. The root of each subtree is said to be child of r, and r is the parent of each

    sub root.

    A Tree of n nodes will consist of n-1 edges (An edge is connecting line or link betweentwo nodes).

    Node with no children is calledLeaf. Nodes with same parent are calledSiblings. APath from n1 to nk is defined as a sequence of nodes n1,n2,----,nk such that ni is the

    parent of ni + 1 for 1

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    12/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:12

    Trees are used in storing records into the database (e.g. B-Trees). This improves theperformance of data storage and retrieval.

    Trees are used in maintaining the directory and file system within the operating systems. Used to represent web page structures.

    Node Declaration for Trees:

    struct node

    {

    Element_Type data;

    struct node * Child_1;

    struct node * Child_2;

    struct node * Child_3;

    . . . .

    struct node * Child_N;};

    A Sample Tree:

    Binary Trees:

    A Binary tree is a tree in which no node can have more than two children. The first

    subset contains a single element called the root of the tree. The other two subsets themselves

    binary tree, called the left and right sub trees of the original tree.

    Eg:

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    13/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:13

    If every non-leaf node in a binary tree has nonempty left and right sub trees, the tree istermed a strictly binary tree. It is also called 2-tree.

    A complete binary tree is a tree with n levels, where for each level d; the number ofexisting nodes at level d except for the maximum level (n

    thlevel) is equal to 2

    d. This

    means all possible nodes exist at these levels. An additional requirement for a complete

    binary tree is that for the nth

    level, while every node does not have to exist, the nodes that

    do exist must fill from left to right.

    A binary tree is said to be balanced tree when the depth of the tree (maximum level) isequal to log2N where N is the number of nodes.

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    14/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:14

    6

    2

    3

    1 4

    8

    Binary Search Tree (BST)

    A binary search tree is a binary tree in which all elements stored in the left sub tree of any node

    X are all less than the element stored at X, and all elements stored in the right sub tree of X are

    greater than the element stored at X.

    Minimum element of the BST is the left leaf node and Maximum element of the BST is

    the right most leaf node. Searching of any element is very fast. The average depth of a binary

    search tree is O(log n).

    Eg:

    Insert Operation:

    Consider a Binary Search tree

    To insert any element first check whether the element is greater than the root or less thanthe root.

    If the element is greater than the root then it moves towards right sub-tree. If it is less than the root then it moves towards left sub-tree. This process continues recursively until a correct location is found and insertion is done. Duplication Policy: When the element to be inserted is already present in tree, then it is a

    duplicate and one of the following policies can be applied:

    o Reject the duplicate and do not insert the element.o Insert it in the appropriate location in the left sub tree.o Insert it in the appropriate location in the right sub tree.

    Inserting 5:

    5 > 4

    6

    2

    3

    1 4

    8

    5 Inserted

    5 > 2

    5 < 6

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    15/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:15

    Remove or Delete Operation

    Once we have found the node to be removed, we need to consider several possibilities If node is a leaf, it can be deleted immediately and its parent is updated with null in its

    place.

    If the node has one child, the node can be removed after its parent adjusts a pointer tobypass the node. Notice that the removed node is now unreferenced and can be deleted

    only if a pointer to it has been saved (in a temporary variable).

    The complicated case is when a node has two children the general strategy is to replacethe key of this node will the smallest key of the right sub tree and remove that smallest

    key from the right sub tree.

    Eg:

    Consider a BST

    After removing 2 which has two children

    Minimum of

    Right sub Tree

    Where 3 is the minimum of sub tree and 3 is deleted from right sub tree.

    6

    2

    3

    1 4

    8

    3.5

    6

    3

    3.5

    1 4

    8

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    16/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:16

    Traversals of Binary trees:

    Let T be a binary tree. There are different methods that differ in the order in which they visit the

    nodes. The three different traversal methods are:

    Inorder Preorder Postorder

    Inorder Traversal:

    It follows the general strategy of left-node-right (L-N-R). In this traversal, if T is not

    empty

    Traverse the left sub tree, Visit the root node of T, Then traverse the right sub tree.

    Consider a binary tree,

    The inorder traversal of the given binary tree is,

    D B E A F C

    Preorder Traversal:

    It follows the general strategy of node-left-right (N-L-R). In this traversal, if T is not

    empty.

    Visit the root node

    Traverse the left sub tree in preorder, Traverse the right sub tree in preorder.

    For the above tree the Preorder Traversal is,

    A B D E C F

    A

    B

    FD E

    C

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    17/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:17

    Post order Traversal:

    It follows the general strategy of left-right-node (L-R-N). In this traversal, if T is not empty.

    Traverse the left sub tree in Post order, Traverse the right sub tree in Post order, Visit the root.

    For the above tree the post order traversal is,

    D E B F C A

    Note: Please refer Lab Exercise 18 for implementation of BST.

    Graphs

    A Graph G consists of a set V of vertices (Nodes) and a set of edges (arcs) E. We write G = (V, E). V is a finite and non empty set of vertices and E is a set of pairs of

    vertices called edges.

    An edge e = (v, w) is a pair of vertices v and w, and is said to be incident with v and w.Nodes v and w are called as endpoints of e, and u and v are said to be adjacent nodes or

    neighbors.

    Eg:

    V (G) = { A, B, C }

    E (G) = { (A, B) (A, C) (B, C) }

    Representation of Graphs:

    There are two types in which we can represent graphs.

    1. Adjacency Matrix

    2. Adjacency Lists

    The Choice of representative depends on the application and function to be performed on the

    graph.

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    18/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:18

    Adjacency Matrix:

    The Adjacency Matrix A for a graph G = (V, E) with n vertices, is an n x n Matrix if bits,

    such that A

    1 If there is an edge Vi to Vj(un weighted graph)

    Aij = 0 If otherwise

    In case of weighted graph we replace 1 with the weight.

    Eg:

    Let us consider a graph

    The Adjacency Matrix would be

    1 2 3 4

    A = 1 0 1 0 1

    2 0 0 1 0

    3 0 0 0 0

    4 0 0 1 0

    Observe that have we needed n2

    bits to represent a graph with n nodes.

    The disadvantages of this kind of representations are

    1) It takes O (n2) spaces to represent a graph with n vertices, even for sparse graphs

    2) It takes O (n2) time to solve most of the graph problems.

    Adjacency List Representation:

    In this representation, we store a graph as a linked structure. We store all the vertices in a

    list and then for each vertex, we have a linked list of the adjacent vertices.

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    19/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:19

    Eg: Let us consider a Graph

    The representation would be

    1

    2

    3

    4

    Graph Traversals:

    Given Undirected graph G and a vertex V in V(G) we are interested in visiting all vertices in G

    that are reachable from V1 we shall look at two ways of doing this

    1. Depth first Search

    2. Breadth first Search

    Depth first Search:

    Depth first Search of an undirected graph proceeds as follows:

    The start Vertex V is visited. Next an unvisited Vertex W adjacent to V is selected and a depth first search from W

    initiated.

    When a vertex U is reached such that all its adjacent vertices have been visited. We back up the last vertex visited which has an unvisited Vertex W adjacent to it and

    initiate a depth first search from W.

    2 4

    3

    3

    NULL

    NULL

    NULL

    NULL

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    20/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Staff: Janaki Ram Dept of IT Page:20

    The search terminates when no unvisited vertex can be reached from any of the visitedones.

    Pseudo Code

    Void DFS(Vertex V)

    {

    visited[v] = True;

    for each Vertex W adjacent to V

    if(! Visited[W])

    DFS(W);

    }

    Breadth-First-Search:

    Starting at vertex V and marking it as visited. BFS differs from Depth first search in that all unvisited vertices adjacent to V are

    visited first.

    Then unvisited vertices adjacent to these vertices are visited and so on.Pseudo code

  • 8/7/2019 C&DS Unit-VII and VIII (Data Structures)

    21/21

    C & DS for I MCA Unit- 7 and 8 (Data Structures) VVIT

    Void BFS(Vertex V)

    {

    initialize Q to be Empty //Q is a Queue

    while(1)

    {

    for each vertex W adjacent to V

    if(visited[W] = false)

    {

    Enqueue(Q, W);

    Visited[W] = True;

    }

    if(Q is Empty)

    return;

    V = Dequeue(Q);}

    }


Recommended