Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts

Monday, 11 December 2017

Chapter 6: Data Structure -Graphs

Chapter 6: Graph Data Structure

What is graph ? Explain.

A graph is a pictorial representation of a set of objects called vertices (nodes or points), these vertices are connected by links called edges (arcs or lines).

A graph can also be defined as a pair of sets (V, E), where V is the set of vertices and E is the set of edges, connecting the pairs of vertices(V).

Fig 6.1 Graph

In the above graph, Vertices V = {A, B, C, D, E} and Edges E = {AB, BD, CD, DE, AE}

Terminologies Used in Graph:


Adjacency

Two node or vertices are adjacent if they are connected to each other through an edge.

Example : Vertices A and B are adjacent in the above picture.

Edge

Edge represents a path(or line) between two vertices.

Example: In the above picture the lines from A to B, B to C, and so on represents edges.

Path

Path represents a sequence of edges between the two vertices.

Example: In the above picture, ABD represents a path from A to D.

Vertex

Each node of the graph is represented as a vertex.

Example: In the above picture A, B, C, D, E are vertices.

Thursday, 7 December 2017

Chapter 5: Data Structure -Trees

Chapter 5: Tree Data Structure

What is Tree? Explain.

Tree is a non-linear data structure. Tree represents the nodes connected by edges.

Terminologies Used in Tree Data Structure

Root

The node at the top of the tree is called root. There is always only one root per tree.

Path

Sequence of nodes along the edges of a tree is called path.

Parent

The node above the given node and connected by its edge upward is called parent node.

Child

The node below the given node and connected by its edge downward is called child node.

Leaf

The node which does not have any child node is called Leaf

Key

Key represents value of a node. Key is used in search operations to find out a particular node.

Siblings

A group of nodes with the same parent.

Descendant

A node reachable by repeated proceeding from parent to child.

Ancestor

A node reachable by repeated proceeding from child to parent.

Edge

The connection between one node and another.

Depth

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

Fig 5.1 - Tree

Chapter 4: Data Structure - Linked List

Chapter 4: Linked List Data Structure

What is Linked List ? Explain.

A linked list is a linear data structure where each element is a separate object. Each element (Node) of a list comprises of two items - the data and a reference to the next node. The last node has a reference to null.

The entry point into a linked list is called the head of the list. It should be noted that head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference.

Fig 4.1 - Linked List

Advantages of Linked List

  • Insertion and deletion operations can be easily implemented.
  • A linked list is a dynamic data structure. The number of nodes in a list is not fixed and can grow and shrink on demand. Any application which has to deal with an unknown number of objects will need to use a linked list.

Disadvantages of Linked List

  • It does not allow direct access to the individual elements. If you want to access a particular item then you have to start at the head and follow the references until you get to that item.
  • Linked list uses more memory compare with an array - we extra 4 bytes (on 32-bit CPU) to store a reference to the next node.

Types of Linked Lists

  • Singly linked list
  • Doubly linked list
  • Circular linked list

Singly Linked List

Singly linked list contains node with two parts data part and reference part. Data part contains data and reference part points to the next node in the sequence.

Fig 4.2 - Singly Linked List

Doubly Linked List

Double linked list contains node with three parts one data part and two reference parts. Data part contains data, first reference part points to previous node in the sequence and another reference part points to the next node in the sequence.

Fig 4.3 - Doubly Linked List

Circular Linked List

Circular linked list contains node with two parts data part and reference part. Data part contains data and reference part points to the next node in the sequence. Also last node points back to first node (head) of the list.

Fig 4.4 - Circular Linked List

Linked List Operations

Following are the important operations performed on linked lists

  • Insert a node at the begging of the list
  • Insert a node at the end of the list
  • Delete a node from the list

We can also perform common operations like search and display

Chapter 2: Data Structure - Stack

Chapter 2: Stack Data Structure

What is stack? Explain.

Stack is a LIFO (Last In First Out) data structure. Push() function is used to add new elements into the Stack Pop() function is used to delete an element from the stack. Adding and deleting of element is allowed at the only one end of the stack called TOP.

Fig DS 1.1 - Stack Data Structure

Stack Overflow:

Stack is said to be in Overflow state when it is completely full.

Stack Underflow:

Stack is said to be in Underflow state if it is completely empty.

Stack Data Structure uses:

  • Reversing of the word.
  • Conversion of expression(Infix to Postfix, Postfix to Prefix) Etc..

Implementation of stack using Array in Java Programming.

To implement the array we need to write the functions for push() and pop() operations.

Steps for PUSH operation

  1. Check if the stack is full or not.
  2. If the stack is full, then print error of overflow and exit the program.
  3. If the stack is not full, then increment the top and add the element.

Steps for POP operation

  1. Check if the stack is empty or not.
  2. If the stack is empty, then print error of underflow and exit the program.
  3. If the stack is not empty, then print the element at the top and decrement the top.

Display Elements in Stack

Steps to Display

  1. Check if the stack is empty or not.
  2. If the stack is empty, then print message stack is empty.
  3. If the stack is not empty, display the elements by looping through the stack.

Java Program to Implement Stack using array


import java.util.Arrays;

public class StackMethods {
    private int top;
    int size;
    int[] stack ;

    public StackMethods(int arraySize){
        size=arraySize;
        stack= new int[size];
        top=-1;
    }

    public void push(int value){
        if(top==size-1){
            System.out.println("Stack is full, can't push a value");
        }
        else{

            top=top+1;
            stack[top]=value;
        }
    }

    public void pop(){
        if(!isEmpty())
            top=top-1;
        else{
            System.out.println("Can't pop...stack is empty");
        }
    }

    public boolean isEmpty(){
        return top==-1;
    }

    public void display(){

        for(int i=0;i<=top;i++){
            System.out.print(stack[i]+ " ");
        }
        System.out.println();
    }
}

Data Structures Study Topics

Data Structures

Chapter 1: Introduction to Data Structures
Chapter 2: Stacks
Chapter 3: Queues
Chapter 4: Linked Lists
Chapter 5: Trees
Chapter 6: Graphs

Monday, 4 December 2017

Chapter 3: Data Structures - Queue

Chapter 3: Queue Data Structure

What is Queue ? Explain.

Queue is a First In First Out (FIFO) Data structure, which means that element inserted first will also be removed first.

Element is inserted from one end called REAR (also called as tail). Element is delete from the other end called as FRONT (also called as head).

The process of adding an element into queue is called Enqueue and the process of removal of an element from queue is called Dequeue.

Fig 3.1 Queue

Queue Data structure uses:

  • Resource allocation(Memory, CPU, Printer) in Operating system
  • Incoming call handling in Call center

Implementation of Queue

Queue data structure is implemented using Enqueue and Dequeue operations.

Steps for ENQUEUE operation

  • Check if the queue is full or not.
  • If the queue is full, then print overflow error and exit the program.
  • If the queue is not full, then increment the tail and add the element.

Steps for DEQUEUE operation

  • Check if the queue is empty or not.
  • If the queue is empty, then print underflow error and exit the program.
  • If the queue is not empty, then print the element at the head and increment the head.

Display elements in Queue

Steps to display elements in queue

  • Check if the queue is empty or not.
  • If the queue is empty, then print message “Queue is empty, cannot display”.
  • If the queue is not empty, then print the element at the head and increment the head, continue till the end of the queue.

Java program to implement queue using array


public class  QDemo {
int capacity = 3;
 int arr[] = new int[capacity];
 int size = 0;
 int top = -1;
 int rear = 0;

 public void push(int pushedElement) {
  if (top < capacity - 1) {
   top++;
   arr[top] = pushedElement;
   System.out.println("Element " + pushedElement + " is pushed to Queue !");
   display();
  } else {
   System.out.println("Queue Overflown, cannot add element ");
  }
 }

 public void pop() {
  if (top >= rear) {
   rear++;
   System.out.println("Pop operation done !");
   display();
  } else {
   System.out.println("Queue Underflow, no elements in queue ");
  }
 }

 public void display() {
  if (top >= rear) {
   System.out.println("Elements in Queue : ");
   for (int i = rear; i <= top; i++) {
    System.out.println(arr[i]);
   }
  }
 }

 public static void main(String[] args) {
  QDemo QDemo = new QDemo();
  QDemo.pop();
  QDemo.push(10);
  QDemo.push(20);
  QDemo.pop();
  QDemo.pop();
}

}

Tuesday, 31 October 2017

Chapter 1: Introduction to Data Structures

Chapter 1: Introduction to Data Structures

What is Data Structure ?

Data structure is a way of collection and organizing the data in such a way that operations on these data can be performed in efficient way. Data structure also helps in efficient storage and retrieval of data. It represents how data is organized in computer memory.

Example:

We have student records name “Ram”, age 20 and name “Shyam”, age 22. We can store these records in database or file as a data structure.

Types of Data Structures

  1. Primitive Data Structures
  2. It includes Boolean, Byte, Char, Double, Float, Int, Long, Short

  3. Non-Primitive or User Defined Data Structures
  4. It includes Array, structure, union, files, linked list (linear lists: stacks, queue; Non-linear lists: Trees, Graphs)

We will discuss the following Data Structures one by one in the coming chapters.

  • Stacks
  • Queues
  • Linked Lists
  • Trees and Graphs