introduction to data structures

99
Advanced Data Structures 1 Introduction to Data Structures Data Structures: A data structure is an arrangement of data in a computer's memory or even disk storage. Data structures can be classified into two types Linear Data Structures Non Linear Data Structures Linear Data Structures: Linear data structures are those data structures in which data elements are accessed (read and written) in sequential fashion ( one by one) Eg: Stacks , Queues, Lists, Arrays Non Linear Data Structures: Non Linear Data Structures are those in which data elements are not accessed in sequential fashion. Eg: trees, graphs Algorithm: Step by Step process of representing solution to a problem in words is called an Algorithm. Characteristics of an Algorithm: Input : An algorithm should have zero or more inputs Output: An algorithm should have one or more outputs Finiteness: Every step in an algorithm should end in finite amount of time Unambiguous: Each step in an algorithm should clearly stated Effectiveness: Each step in an algorithm should be effective

Upload: ism33

Post on 25-Sep-2015

223 views

Category:

Documents


2 download

DESCRIPTION

Introduction to Data Structures

TRANSCRIPT

  • Advanced Data Structures

    1

    Introduction to Data Structures

    Data Structures: A data structure is an arrangement of data in a computer's memory or even disk storage.

    Data structures can be classified into two types

    Linear Data Structures

    Non Linear Data Structures

    Linear Data Structures:

    Linear data structures are those data structures in which data elements are accessed (read and

    written) in sequential fashion ( one by one)

    Eg: Stacks , Queues, Lists, Arrays

    Non Linear Data Structures:

    Non Linear Data Structures are those in which data elements are not accessed in sequential

    fashion.

    Eg: trees, graphs

    Algorithm:

    Step by Step process of representing solution to a problem in words is called an Algorithm.

    Characteristics of an Algorithm:

    Input : An algorithm should have zero or more inputs

    Output: An algorithm should have one or more outputs

    Finiteness: Every step in an algorithm should end in finite amount of time

    Unambiguous: Each step in an algorithm should clearly stated

    Effectiveness: Each step in an algorithm should be effective

  • Advanced Data Structures

    2

    Characteristics of Data Structures

    Data

    Structure Advantages Disadvantages

    Array Quick inserts

    Fast access if index known

    Slow search

    Slow deletes

    Fixed size

    Ordered

    Array

    Faster search than unsorted array Slow inserts

    Slow deletes

    Fixed size

    Stack Last-in, first-out acces 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 Tree Quick search

    Quick inserts

    Quick deletes

    (If the tree remains balanced)

    Deletion algorithm is complex

    Red-Black

    Tree

    Quick search

    Quick inserts

    Quick deletes

    (Tree always remains balanced)

    Complex to implement

    2-3-4 Tree Quick search

    Quick inserts

    Quick deletes

    (Tree always remains balanced)

    (Similar trees good for disk storage)

    Complex to implement

    Hash Table Very fast access if key is known

    Quick inserts

    Slow deletes

    Access slow if key is not known

    Inefficient memory usage

    Heap Quick inserts

    Quick deletes

    Access to largest item

    Slow access to other items

    Graph Best models real-world situations Some algorithms are slow and very

    complex

  • Advanced Data Structures

    3

    Stack :

    Stack is a Linear Data Structure which follows Last in First Out mechanism.

    It means: the first element inserted is the last one to be removed

    Stack uses a variable called top which points topmost element in the stack. top is incremented

    while pushing (inserting) an element in to the stack and decremented while poping (deleting) an

    element from the stack

    Push(A) Push(B) Push(C) Push(D) Pop()

    Valid Operations on Stack:

    Inserting an element in to the stack (Push)

    Deleting an element in to the stack (Pop)

    Displaying the elements in the queue (Display)

    Note:

    While pushing an element into the stack, stack is full condition should be checked

    While deleting an element from the stack, stack is empty condition should be checked

    Applications of Stack:

    Stacks are used in recursion programs

    Stacks are used in function calls

    Stacks are used in interrupt implementation

    A

    B

    A

    C

    B

    A

    C

    B

    A

    top

    top

    top

    top

    D

    C

    B

    A

    top

  • Advanced Data Structures

    4

    Queue:

    Queue is a Linear Data Structure which follows First in First out mechanism.

    It means: the first element inserted is the first one to be removed

    Queue uses two variables rear and front. Rear is incremented while inserting an element into the

    queue and front is incremented while deleting element from the queue

    Insert(A) Insert(B) Insert(C) Insert(D) Delete()

    Valid Operations on Queue:

    Inserting an element in to the queue

    Deleting an element in to the queue

    Displaying the elements in the queue

    Note:

    While inserting an element into the queue, queue is full condition should be checked

    While deleting an element from the queue, queue is empty condition should be checked

    Applications of Queues:

    Real life examples

    Waiting in line

    Waiting on hold for tech support

    Applications related to Computer Science

    Threads

    Job scheduling (e.g. Round-Robin algorithm for CPU allocation)

    A

    B

    A

    C

    B

    A

    D

    C

    B

    A

    D

    C

    B rear

    front

    rear

    front

    rear

    front

    rear

    front

    rear

    front

  • Advanced Data Structures

    5

    Linked List:

    To overcome the disadvantage of fixed size arrays linked list were introduced.

    A linked list consists of nodes of data which are connected with each other. Every node consist

    of two parts data and the link to other nodes. The nodes are created dynamically.

    NODE

    Data link

    Types of Linked Lists:

    Single linked list

    Double linked list

    Circular linked list

    Valid operations on linked list:

    Inserting an element at first position

    Deleting an element at first position

    Inserting an element at end

    Deleting an element at end

    Inserting an element after given element

    Inserting an element before given element

    Deleting given element

    bat

    bat cat sat vat NULL

  • Advanced Data Structures

    6

    Trees :

    A tree is a Non-Linear Data Structure which consists of set of nodes called vertices and set of edges which links vertices

    Terminology:

    Root Node: The starting node of a tree is called Root node of that tree

    Terminal Nodes: The node which has no children is said to be terminal node or leaf .

    Non-Terminal Node: The nodes which have children is said to be Non-Terminal Nodes

    Degree: The degree of a node is number of sub trees of that node

    Depth: The length of largest path from root to terminals is said to be depth or height of

    the tree

    Siblings: The children of same parent are said to be siblings

    Ancestors: The ancestors of a node are all the nodes along the path from the root to the

    node

    A

    B C

    D

    G

    E F

    I H

    Property Value Number of nodes : 9

    Height : 4

    Root Node : A

    Leaves : ED, H, I, F, C

    Interior nodes : D, E, G

    Number of levels : 5

    Ancestors of H : I

    Descendants of B : D,E, F

    Siblings of E : D, F

  • Advanced Data Structures

    7

    Binary Trees:

    Binary trees are special class of trees in which max degree for each node is 2

    Recursive definition:

    A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint

    binary trees called the left subtree and the right subtree.

    Any tree can be transformed into binary tree. By left child-right sibling representation.

    Binary Tree Traversal Techniques:

    There are three binary tree traversing techniques

    Inorder

    Preorder

    Postorder

    Inorder: In inorder traversing first left subtree is visited followed by root and right subtree

    Preorder: In preorder traversing first root is visited followed by left subtree and right subtree.

    Postorder: In post order traversing first left tree is visited followed by right subtree and root.

    A

    B

    C

    D

    E

    F G K

  • Advanced Data Structures

    8

    Binary Search Tree:

    A Binary Search Tree (BST) is a binary tree which follows the following conditons

    Every element has a unique key.

    The keys in a nonempty left subtree are smaller than the key in the root of subtree.

    The keys in a nonempty right subtree are grater than the key in the root of subtree.

    The left and right subtrees are also binary search trees.

    Valid Operations on Binary Search Tree:

    Inserting an element

    Deleting an element

    Searching for an element

    Traversing

    63

    41 89

    34 56 72 95

  • Advanced Data Structures

    9

    Avl Tree:

    If in a binary search tree, the elements are inserted in sorted order then the height will be n,

    where n is number of elements. To overcome this disadvantage balanced trees were introduced.

    Balanced binary search trees

    An AVL Tree is a binary search tree such that for every internal node v of T, the

    heights of the children of v can differ by at most 1.

    Operations of Avl tree:

    Inserting an element

    Deleting an element

    Searching for an element

    Traversing

    Height balancing

    88

    44

    17 78

    32 50

    48 62

    2

    4

    1

    1

    2

    3

    1

    1

  • Advanced Data Structures

    10

    Graphs

    A graph is a Non-Linear Data Structure which consists of set of nodes called vertices V and set

    of edges E which links vertices

    Note: A tree is a graph with out loops

    Graph Tree

    Graph Traversal:

    Problem: Search for a certain node or traverse all nodes in the graph

    Depth First Search

    Once a possible path is found, continue the search until the end of the path

    Breadth First Search

    Start several paths at a time, and advance in each one step at a time

    0

    1 2

    3

    0

    1 2

    3 4 5 6

  • Advanced Data Structures

    11

    Object Oriented Programming:

    You've heard it a lot in the past several years. Everybody is saying it.

    What is all the fuss about objects and object-oriented technology? Is it real? Or is it hype? Well,

    the truth is--it's a little bit of both. Object-oriented technology does, in fact, provide many

    benefits to software developers and their products. However, historically a lot of hype has

    surrounded this technology, causing confusion in both managers and programmers alike. Many

    companies fell victim to this hardship (or took advantage of it) and claimed that their software

    products were object-oriented when, in fact, they weren't. These false claims confused

    consumers, causing widespread misinformation and mistrust of object-oriented technology.

    Object:

    As the name object-oriented implies, objects are key to understanding object-oriented

    technology. You can look around you now and see many examples of real-world objects: your

    dog, your desk, your television set, your bicycle.

    Definition: An object is a software bundle of variables and related methods

    Class:

    In the real world, you often have many objects of the same kind. For example, your bicycle is

    just one of many bicycles in the world. Using object-oriented terminology, we say that your

    Introduction to Object Oriented Programming

  • Advanced Data Structures

    12

    bicycle object is an instance of the class of objects known as bicycles. Bicycles have some state

    (current gear, current cadence, two wheels) and behavior (change gears, brake) in common.

    However, each bicycle's state is independent of and can be different from other bicycles.

    Definition: A class is a blueprint or prototype that defines the variables and methods common to

    all objects of a certain kind.

    Inheritance:

    Acquiring the properties of one class in another class is called inheritance

    The Benefits of Inheritance

    Subclasses provide specialized behaviors from the basis of common elements provided

    by the super class. Through the use of inheritance, programmers can reuse the code in the

    superclass many times.

    Programmers can implement superclasses called abstract classes that define "generic"

    behaviors. The abstract superclass defines and may partially implement the behavior but

    much of the class is undefined and unimplemented. Other programmers fill in the details

    with specialized subclasses.

    Data Abstraction:

    The essential element of object oriented programming in abstraction. The complexity of

    programming in object oriented programming is maintained through abstraction.

    For example, the program consist of data and code which work over data. While executing a

    program we dont thing in which location that data is being stored how the input device is

    transferring the input to the memory etc. this abstraction allows us to execute the program

    without thinking deeply about the complexity of execution of program.

    Encapsulation:

    Encapsulation is the mechanism that binds together code and the data and keeps them safe from

    outside world. In the sense it is a protective wrapper that prevents the code and data from being

  • Advanced Data Structures

    13

    accessed by other code defied outside the wrapper. Access is controlled through a well defined

    interface.

    Polymorphism:

    Existing in more that one form is called polymorphism.

    Polymorphism means the ability to take more that one form. For example an operation may

    exhibit different behavior in different behavior in different instances.

    For example consider operation of addition. For two numbers the operation will generate a sum.

    If the operands are string the operation would produces a third string by concatenation.

    C++ supports polymorphism through method overloading and operator overloading

    Method overloading:

    if the same method name used for different procedures that the method is said to be overloaded.

    Dynamic Binding:

    Binding refer to the linking of a procedure call to the code to be executed in response to the call.

    Dynamic binding means that the code associated with a given procedure call is not know until

    the time of the call at runtime. It is associated with a polymorphism reference depends on the

    dynamic type of that reference.

    Message communication:

    An object oriented program consists of objects that communicate with each other. The process

    of programming in an object oriented language therefore involves the following basic steps:

    1. creating classes that define objects and their behaviors.

    2. creating objects from class definitions.

    3. establishing communication among objects.

  • Advanced Data Structures

    14

    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.

    To best describe the term Abstract Data Type, it is best to break the term down into "data type"

    and then "abstract".

    Data type:

    When we consider a primitive type we are actually referring to two things: a data item with

    certain characteristics and the permissible operations on that data. An int in Java, for example,

    can contain any whole-number value from -2,147,483,648 to +2,147,483,647. It can also be used

    with the operators +, -, *, and /. The data type's permissible operations are an inseparable part of

    its identity; understanding the type means understanding what operations can be performed on it.

    In C++, any class represents a data type, in the sense that a class is made up of data (fields) and

    permissible operations on that data (methods). By extension, when a data storage structure like a

    stack or queue is represented by a class, it too can be referred to as a data type. A stack is

    different in many ways from an int, but they are both defined as a certain arrangement of data

    and a set of operations on that data.

    abstract

    Now lets look at the "abstract" portion of the phrase. The word abstract in our context stands for

    "considered apart from the detailed specifications or implementation".

    In C++, an Abstract Data Type is a class considered without regard to its implementation. It can

    be thought of as a "description" of the data in the class and a list of operations that can be carried

    out on that data and instructions on how to use these operations. What is excluded though, is the

  • Advanced Data Structures

    15

    details of how the methods carry out their tasks. An end user (or class user), you should be told

    what methods to call, how to call them, and the results that should be expected, but not HOW

    they work.

    We can further extend the meaning of the ADT when applying it to data structures such as a

    stack and queue. In Java, as with any class, it means the data and the operations that can be

    performed on it. In this context, although, even the fundamentals of how the data is stored should

    be invisible to the user. Users not only should not know how the methods work, they should also

    not know what structures are being used to store the data.

    Consider for example the stack class. The end user knows that push() and pop() (amoung other

    similar methods) exist and how they work. The user doesn't and shouldn't have to know how

    push() and pop() work, or whether data is stored in an array, a linked list, or some other data

    structure like a tree.

  • Advanced Data Structures

    16

    Push(item)

    {

    If (stack is full) print stack over flow

    else

    Increment top ;

    Stack [top]= item;

    }

    Pop()

    {

    If( stack is empty) print stack under flow

    else

    Decrement top

    }

    Display()

    {

    If ( stack is empty) print no element to display

    else

    for i= top to 0 step -1

    Print satck[i];

    }

    Stack ADT Algorithms

  • Advanced Data Structures

    17

    #include

    #include

    #include

    class stack

    {

    int stk[5];

    int top;

    public:

    stack()

    {

    top=-1;

    }

    void push(int x)

    {

    if(top > 4)

    {

    cout

  • Advanced Data Structures

    18

    for(int i=top;i>=0;i--)

    cout

  • Advanced Data Structures

    19

    Insert ( item)

    {

    If rear = max -1 then print queue is full

    else

    {

    Increment rear

    Queue [rear]=item;

    }

    }

    Delete()

    {

    If front = rear print queue is empty

    else

    Increment front

    }

    Display()

    {

    If front=rear print queue is empty

    else

    For i =front to rear

    Print queue[i];

    }

    Queue ADT Algorithms

  • Advanced Data Structures

    20

    #include

    #include

    #include

    class queue

    {

    int queue[5];

    int rear,front;

    public:

    queue()

    {

    rear=-1;

    front=-1;

    }

    void insert(int x)

    {

    if(rear > 4)

    {

    cout

  • Advanced Data Structures

    21

    return;

    }

    for(int i=front+1;i

  • Advanced Data Structures

    22

    Push(item)

    {

    If (stack is full) print stack over flow

    else

    goto end of list and let it be temp

    temp->next=item

    item->next=NULL;

    }

    Pop()

    {

    If(head is null) print stack under flow

    else

    goto last but one node and let it be temp

    temp->next=NULL

    }

    Display()

    {

    If ( head=NULL) print no element to display

    else

    {

    Temp=head;

    While(temp!=NULL)

    {

    Print(temp->data)

    Temp=temp->next;

    }

    }

    Algorithm for Stack Using Linked List

  • Advanced Data Structures

    23

    #include

    #include

    #include

    class node

    {

    public:

    class node *next;

    int data;

    };

    class stack : public node

    {

    node *head;

    int tos;

    public:

    stack()

    {

    os=-1;

    }

    void push(int x)

    {

    if (tos < 0 )

    {

    head =new node;

    head->next=NULL;

    head->data=x;

    tos ++;

    }

    else

    {

    node *temp,*temp1;

    temp=head;

    if(tos >= 4)

    {

    cout next;

    temp1=new node;

    Stack Using Linked List

  • Advanced Data Structures

    24

    temp->next=temp1;

    temp1->next=NULL;

    temp1->data=x;

    }

    }

    void display()

    {

    node *temp;

    temp=head;

    if (tos < 0)

    {

    cout

  • Advanced Data Structures

    25

    cout ch;

    switch(ch)

    {

    case 1:

    cout ch;

    s1.push(ch);

    break;

    case 2: s1.pop();break;

    case 3: s1.display();

    break;

    case 4: exit(0);

    }

    }

    }

    OUTPUT

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:1

    enter a element23

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:1

    enter a element67

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:3

    23 67

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:3

    23

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    stack under flow

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:4

  • Advanced Data Structures

    26

    Insert ( item)

    {

    If rear = max -1 then print queue is full

    else

    {

    Increment rear

    Create a new node called item

    goto last node in the list and let it be temp

    temp-next=item;

    item-next=NULL;

    }

    }

    Delete()

    {

    If front = rear print queue is empty

    else

    {

    Increment front

    head=head-next;

    }

    }

    Display()

    {

    If front=rear print queue is empty

    else Temp=head;

    While(temp!=NULL)

    {

    Print(temp-data)

    Temp=temp-next;

    }

    }

    Algorithm Queue using Linked List

  • Advanced Data Structures

    27

    #include

    #include

    #include

    class node

    {

    public:

    class node *next;

    int data;

    };

    class queue : public node

    {

    node *head;

    int front,rare;

    public:

    queue()

    {

    front=-1;

    rare=-1;

    }

    void push(int x)

    {

    if (rare < 0 )

    {

    head =new node;

    head->next=NULL;

    head->data=x;

    rare ++;

    }

    else

    {

    node *temp,*temp1;

    temp=head;

    if(rare >= 4)

    {

    cout next;

    Queue using Linked List

  • Advanced Data Structures

    28

    temp1=new node;

    temp->next=temp1;

    temp1->next=NULL;

    temp1->data=x;

    }

    }

    void display()

    {

    node *temp;

    temp=head;

    if (rare < 0)

    {

    cout

  • Advanced Data Structures

    29

    int ch;

    clrscr();

    while(1)

    {

    cout ch;

    switch(ch)

    {

    case 1:

    cout ch;

    s1.push(ch);

    break;

    case 2: s1.pop();break;

    case 3: s1.display();

    break;

    case 4: exit(0);

    }

    }

    }

    OUTPUT

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:1

    enter a element23

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:1

    enter a element54

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:3

    23 54

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    queue under flow

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:4

  • Advanced Data Structures

    30

    Algorithm Insertfirst(item)

    {

    if dequeue is empty

    {

    Item-next=item-prev=NULL;

    tail=head=item;

    }

    else if(dequeue is full) print insertion is not possible

    else

    {

    item-next=head;

    item-prev=NULL;

    head=item;

    }

    }

    Algorithm Insertlast (item)

    {

    if dequeue is empty

    {

    Item-next=item-prev=NULL;

    tail=head=item;

    }

    else if(dequeue is full) print insertion is not possible

    else

    {

    tail-next=head;

    item-prev=tail;

    tail=item;

    }

    }

    Deletefirst()

    {

    If (dequeue is empty) print no node to delete;

    else

    {

    Head=head-next;

    Head-prev=NULL;

    }

    }

    Algorithms fo DeQueue Using Double Linked List

  • Advanced Data Structures

    31

    Deletelast()

    {

    if (dequeue is empty) print no node to delete;

    else

    {

    tail=tail-prev;

    tail-next=NULL;

    }

    }

    Displayfirst()

    {

    if( dequeue is empty) print no node to display

    else

    {

    temp=head;

    while(temp-next!=null) then do

    {

    print(temp-data);

    temp=temp-next;

    }

    }

    }

    Displaylast()

    {

    if( dequeue is empty) print no node to display

    else

    {

    temp=tail

    while(temp-prevt!=null) then do

    {

    print(temp-data);

    temp=temp-prev;

    }

    }

    }

  • Advanced Data Structures

    32

    #include

    #include

    #include

    class node

    {

    public:

    int data;

    class node *next;

    class node *prev;

    };

    class dqueue: public node

    {

    node *head,*tail;

    int top1,top2;

    public:

    dqueue()

    {

    top1=0;

    top2=0;

    head=NULL;

    tail=NULL;

    }

    void push(int x)

    {

    node *temp;

    int ch;

    if(top1+top2 >=5)

    {

    cout next=NULL;

    head->prev=NULL;

    tail=head;

    top1++;

    Implementation of DeQueue Using Double Linked List

  • Advanced Data Structures

    33

    }

    else

    {

    cout ch;

    if(ch==1)

    {

    top1++;

    temp=new node;

    temp->data=x;

    temp->next=head;

    temp->prev=NULL;

    head->prev=temp;

    head=temp;

    }

    else

    {

    top2++;

    temp=new node;

    temp->data=x;

    temp->next=NULL;

    temp->prev=tail;

    tail->next=temp;

    tail=temp;

    }

    }

    }

    void pop()

    {

    int ch;

    cout ch;

    if(top1 + top2

  • Advanced Data Structures

    34

    else

    {

    top2--;

    tail=tail->prev;

    tail->next=NULL;

    }

    }

    void display()

    {

    int ch;

    node *temp;

    cout ch;

    if(top1+top2

  • Advanced Data Structures

    35

    while (1)

    {

    cout ch;

    switch(ch)

    {

    case 1: cout ch;

    d1.push(ch); break;

    case 2: d1.pop(); break;

    case 3: d1.display(); break;

    case 4: exit(1);

    }

    }

    }

    OUTPUT

    1.INSERT 2.DELETE 3.DISPLAU 4.EXIT

    Enter ur choice:1

    enter element4

    1.INSERT 2.DELETE 3.DISPLAU 4.EXIT

    Enter ur choice:1

    enter element5

    Add element 1.FIRST 2.LAST

    enter ur choice:1

    1.INSERT 2.DELETE 3.DISPLAU 4.EXIT

    Enter ur choice:1

    enter element6

    Add element 1.FIRST 2.LAST

    enter ur choice:2

    1.INSERT 2.DELETE 3.DISPLAU 4.EXIT

    Enter ur choice:3

    display from 1.Staring 2.Ending

    Enter ur choice1

    5 4 6 1.INSERT 2.DELETE 3.DISPLAU 4.EXIT

    Enter ur choice:2

    Delete 1.First Node 2.Last Node

    Enter ur choice:1

    1.INSERT 2.DELETE 3.DISPLAU 4.EXIT

    Enter ur choice:3

    display from 1.Staring 2.Ending

    Enter ur choice1

    4 6 1.INSERT 2.DELETE 3.DISPLAU 4.EXIT

    Enter ur choice:4

  • Advanced Data Structures

    36

    Algorithm Insertfirst(item)

    {

    if cqueue is empty then head=item;

    else if(cqueue is full) print insertion is not possible

    else

    {

    Rear=(rear +1) mod max

    } cqueue[rear]=x;

    }

    Algorithm Deletet()

    {

    If (dequeue is empty) print no node to delete;

    else

    {

    Front=(front+1) mod max

    }

    }

    Algorithm display()

    {

    If (front >rear) display elements for front to max and 0 to rear

    Else display elements from front to rear

    }

    Algorithm for Circular Queue

  • Advanced Data Structures

    37

    #include

    #include

    #include

    class cqueue

    {

    int q[5],front,rare;

    public:

    cqueue()

    {

    front=-1;

    rare=-1;

    }

    void push(int x)

    {

    if(front ==-1 && rare == -1)

    {

    q[++rare]=x;

    front=rare;

    return;

    }

    else if(front == (rare+1)%5 )

    {

    cout

  • Advanced Data Structures

    38

    }

    front= (front+1)%5;

    }

    void display()

    {

    int i;

    if( front

  • Advanced Data Structures

    39

    OUTPUT

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter ur choice1

    enter element4

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter ur choice1

    enter element5

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter ur choice1

    enter element3

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter ur choice3

    4 5 3

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter ur choice2

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter ur choice3

    5 3

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter ur choice4

  • Advanced Data Structures

    40

    #include

    #include

    #include

    # define max 10

    typedef struct list

    {

    int data;

    struct list *next;

    }node_type;

    node_type *ptr[max],*root[max],*temp[max];

    class Dictionary

    {

    public:

    int index;

    Dictionary();

    void insert(int);

    void search(int);

    void delete_ele(int);

    };

    Dictionary::Dictionary()

    {

    index=-1;

    for(int i=0;idata=key;

    if(root[index]==NULL)

    {

    Algorithm for Dictionary Program to Implement Functions of a Dictionary

  • Advanced Data Structures

    41

    root[index]=ptr[index];

    root[index]->next=NULL;

    temp[index]=ptr[index];

    }

    else

    {

    temp[index]=root[index];

    while(temp[index]->next!=NULL)

    temp[index]=temp[index]->next;

    temp[index]->next=ptr[index];

    }

    }

    void Dictionary::search(int key)

    {

    int flag=0;

    index=int(key%max);

    temp[index]=root[index];

    while(temp[index]!=NULL)

    {

    if(temp[index]->data==key)

    {

    coutnext=temp[index]->next;

    cout

  • Advanced Data Structures

    42

    temp[index]=NULL;

    free(temp[index]);

    }

    void main()

    {

    int val,ch,n,num;

    char c;

    Dictionary d;

    clrscr();

    do

    {

    cout

  • Advanced Data Structures

    43

    OUTPUT

    MENU:

    1.Create

    2.Search for a value

    3.Delete an value

    Enter your choice:1

    Enter the number of elements to be inserted:8

    Enter the elements to be inserted:10 4 5 8 7 12 6 1

    Enter y to continue......y

    MENU:

    1.Create

    2.Search for a value

    3.Delete an value

    Enter your choice:2

    Enter the element to be searched:12

    Search key is found!!

    Enter the element to be deleted:1

    1 has been deleted.

    Enter y to continue......y

  • Advanced Data Structures

    44

    Algorithm insertion(int x) {

    If(tree is empty) then root is empty

    Otherwise

    {

    temp=search(item); // temp is the node where search for the

    item halts

    if( item > temp) then temp-right=item;

    otherwise temp-left =item

    Reconstruction procedure: rotating tree

    left rotation and right rotation

    Suppose that the rotation occurs at node x

    Left rotation: certain nodes from the right subtree of x move to its left subtree; the root of the right subtree of

    x becomes the new root of the reconstructed subtree

    Right rotation at x: certain nodes from the left subtree of x move to its right subtree; the root of the left subtree

    of x becomes the new root of the reconstructed subtree

    }

    Algorithm Search(int x)

    Algorithm delete()

    {

    Case 1: the node to be deleted is a leaf

    Case 2: the node to be deleted has no right child, that is, its right subtree is empty

    Case 3: the node to be deleted has no left child, that is, its left subtree is empty

    Case 4: the node to be deleted has a left child and a right child

    }

    Algorithm Search(x, root)

    {

    if(tree is empty ) then print tree is empty

    otherwise

    If(x grater than root) search(root-right);

    Otherwise if(x less than root ) search(root-left)

    Otherwise return true

    }

    }

    AVL TREE

  • Advanced Data Structures

    45

    #include

    #include

    #include

    #include

    void insert(int,int );

    void delte(int);

    void display(int);

    int search(int);

    int search1(int,int);

    int avltree[40],t=1,s,x,i;

    void main()

    {

    int ch,y;

    for(i=1;i> ch;

    switch(ch)

    {

    case 1:

    cout ch;

    insert(1,ch);

    break;

    case 2:

    cout x;

    y=search(1);

    if(y!=-1) delte(y);

    else cout

  • Advanced Data Structures

    46

    cout

  • Advanced Data Structures

    47

    }

    t++;

    }

    void delte(int x)

    {

    if( avltree[2*x]==-1 && avltree[2*x+1]==-1)

    avltree[x]=-1;

    else if(avltree[2*x]==-1)

    { avltree[x]=avltree[2*x+1];

    avltree[2*x+1]=-1;

    }

    else if(avltree[2*x+1]==-1)

    { avltree[x]=avltree[2*x];

    avltree[2*x]=-1;

    }

    else

    {

    avltree[x]=avltree[2*x];

    delte(2*x);

    }

    t--;

    }

    int search(int s)

    {

    if(t==1)

    {

    cout

  • Advanced Data Structures

    48

    return;

    }

    for(int i=1;i

  • Advanced Data Structures

    49

    OUTPUT

    1.insert 2.display 3.delete 4.search 5.exit

    Enter u r choice to perform on AVL tree1

    Enter an element to insert into tree4

    do u want to continuey

    1.insert 2.display 3.delete 4.search 5.exit

    Enter u r choice to perform on AVL tree1

    Enter an element to insert into tree5

    do u want to continuey

    1.insert 2.display 3.delete 4.search 5.exit

    Enter u r choice to perform on AVL tree3

    Enter an item to deletion5

    itemfound

    do u want to continuey

    1.insert 2.display 3.delete 4.search 5.exit

    Enter u r choice to perform on AVL tree2

    4

    do u want to continue4

  • Advanced Data Structures

    50

    Algorithm BFS(s): Input: A vertex s in a graph

    Output: A labeling of the edges as discovery edges and cross

    edges

    initialize container L0 to contain vertex s

    i 0

    while Li is not empty do

    create container Li+1 to initially be empty

    for each vertex v in Li do

    if edge e incident on v do

    let w be the other endpoint of e

    if vertex w is unexplored then

    label e as a discovery edge

    insert w into Li+1

    else label e as a cross edge

    i i + 1

    Breath First Search Algorithm

  • Advanced Data Structures

    51

    #include

    #include

    #include

    int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];

    void main()

    {

    clrscr();

    int m;

    cout n;

    cout m;

    cout >j;

    cost[i][j]=1;

    }

    cout v;

    cout

  • Advanced Data Structures

    52

    OUTPUT

    enterno of vertices9

    ente no of edges9

    EDGES

    1 2

    2 3

    1 5

    1 4

    4 7

    7 8

    8 9

    2 6

    5 7

    enter initial vertex1

    Visited vertices

    12 4 5 3 6 7 8 9

  • Advanced Data Structures

    53

    Algorithm DFS(v); Input: A vertex v in a graph

    Output: A labeling of the edges as discovery edges and

    backedges

    for each edge e incident on v do

    if edge e is unexplored then let w be the other

    endpoint of e

    if vertex w is unexplored then label e as a discovery

    edge recursively call DFS(w)

    else label e as a backedge

    Depth First Search Algorithm

  • Advanced Data Structures

    54

    #include

    #include

    #include

    int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];

    void main()

    {

    int m;

    clrscr();

    cout n;

    cout m;

    cout >j;

    cost[i][j]=1;

    }

    cout v;

    cout

  • Advanced Data Structures

    55

    OUTPUT

    enterno of vertices9

    ente no of edges9

    EDGES

    1 2

    2 3

    2 6

    1 5

    1 4

    4 7

    5 7

    7 8

    8 9

    enter initial vertex1

    ORDER OF VISITED VERTICES1 2 3 6 4 7 8 9 5

  • Advanced Data Structures

    56

    Algorithm Prim(E,Cost,n,t)

    {

    Let (k, l) be an edge of minimum cost in E;

    Mincost= cost[k,l];

    t[1,1]=k;

    t[1,2]=l;

    for i=1 to n do

    {

    If (cost[i, l]cost[k,j])

    then near[j]=k

    }

    }

    Prims Algorithm

  • Advanced Data Structures

    57

    #include

    #include

    #include

    int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u;

    void main()

    {

    int m,c;

    clrscr();

    cout n;

    cout m;

    cout >j>>c;

    cost[i][j]=c;

    }

    for(i=1;i

  • Advanced Data Structures

    58

    if(cost[v][j]

  • Advanced Data Structures

    59

    Algorithm Krushkal(E, cost,n,t)

    {

    for i=1 to n do parent[i]=-1;

    i=0;

    mincost=0;

    while( I < n-1)

    {

    Delete a minimum coast edge (u,v) form the heap and

    reheapfy using adjust

    J=find(u);

    K=find(v);

    If(j!=k) then

    {

    i=i+1;

    t[I,1]=u; t[I,2]=v;

    mincost=mincost+ cost[u,v];

    union(j,k)

    }

    If( i != n-1) the write ( no spanning tree);

    else

    Return mincost

    }

    }

    Kruskals Algorithm

  • Advanced Data Structures

    60

    #include

    #include

    #include

    int cost[10][10],i,j,k,n,m,c,visit,visited[10],l,v,count,count1,vst,p;

    main()

    {

    int dup1,dup2;

    cout n;

    cout m;

    cout >j >>c;

    cost[i][j]=c;

    cost[j][i]=c;

    }

    for(i=1;i

  • Advanced Data Structures

    61

    for(p=1;p

  • Advanced Data Structures

    62

    #include

    #include

    #include

    int shortest(int ,int);

    int cost[10][10],dist[20],i,j,n,k,m,S[20],v,totcost,path[20],p;

    void main()

    {

    int c;

    clrscr();

    cout n;

    cout m;

    cout > j >>c;

    cost[i][j]=c;

    }

    for(i=1;i

  • Advanced Data Structures

    63

    S[v]=1;

    dist[v]=0;

    for(i=2;i

  • Advanced Data Structures

    64

    4 1 10

    4 5 15

    5 2 20

    5 3 35

    6 5 3

    enter initial vertex1

    1

    14

    145

    1452

    13

  • Advanced Data Structures

    65

    Algorithm preorder( root)

    {

    1. current = root; //start the traversal at the root node

    2. while(current is not NULL or stack is nonempty)

    if(current is not NULL)

    {

    visit current;

    push current onto stack;

    current = current->llink;

    }

    else

    {

    pop stack into current;

    current = current->rlink; //prepare to visit

    //the right subtree

    }

    Non recursive Pre order Traversing Algorithm

  • Advanced Data Structures

    66

    #include

    #include

    #include

    class node

    {

    public:

    class node *left;

    class node *right;

    int data;

    };

    class tree: public node

    {

    public:

    int stk[50],top;

    node *root;

    tree()

    {

    root=NULL;

    top=0;

    }

    void insert(int ch)

    {

    node *temp,*temp1;

    if(root== NULL)

    {

    root=new node;

    root->data=ch;

    root->left=NULL;

    root->right=NULL;

    return;

    }

    temp1=new node;

    temp1->data=ch;

    temp1->right=temp1->left=NULL;

    temp=search(root,ch);

    if(temp->data>ch)

    temp->left=temp1;

    else

    temp->right=temp1;

    Non recursive Pre order Traversing

  • Advanced Data Structures

    67

    }

    node *search(node *temp,int ch)

    {

    if(root== NULL)

    {

    cout right== NULL)

    return temp;

    if(temp->data>ch)

    { if(temp->left==NULL) return temp;

    search(temp->left,ch);}

    else

    { if(temp->right==NULL) return temp;

    search(temp->right,ch);

    } }

    void display(node *temp)

    {

    if(temp==NULL)

    return ;

    display(temp->left);

    coutleft;

    if(p==NULL && top>0)

  • Advanced Data Structures

    68

    {

    p=pop(root);

    }

    }

    }

    node * pop(node *p)

    {

    int ch;

    ch=stk[top-1];

    if(p->data==ch)

    {

    top--;

    return p;

    }

    if(p->data>ch)

    pop(p->left);

    else

    pop(p->right);

    }

    };

    void main()

    {

    tree t1;

    int ch,n,i;

    while(1)

    {

    cout > ch;

    switch(ch)

    {

    case 1: cout

  • Advanced Data Structures

    69

    OUTPUT

    1.INSERT

    2.DISPLAY 3.PREORDER TRAVERSE

    4.EXIT

    Enter your choice:1

    enter no of elements to insert

    enter the elements7

    5 24 36 11 44 2 21

    1.INSERT

    2.DISPLAY 3.PREORDER TRAVERSE

    4.EXIT

    Enter your choice:2

    2 5 11 21 24 36 44

    1.INSERT

    2.DISPLAY 3.PREORDER TRAVERSE

    4.EXIT

    Enter your choice:3

    5 2 24 11 21 36 44

    1.INSERT

    2.DISPLAY 3.PREORDER TRAVERSE

    4.EXIT

    Enter your choice:4

  • Advanced Data Structures

    70

    Algorithm inorder( root)

    {

    1. current = root; //start traversing the binary tree at

    // the root node

    2. while(current is not NULL or stack is nonempty)

    if(current is not NULL)

    {

    push current onto stack;

    current = current->llink;

    }

    else

    {

    pop stack into current;

    visit current; //visit the node

    current = current->rlink; //move to the

    //right child

    }

    }

    Non recursive In order Traversing

  • Advanced Data Structures

    71

    #include

    #include

    #include

    class node

    {

    public:

    class node *left;

    class node *right;

    int data;

    };

    class tree: public node

    {

    public:

    int stk[50],top;

    node *root;

    tree()

    {

    root=NULL;

    top=0;

    }

    void insert(int ch)

    {

    node *temp,*temp1;

    if(root== NULL)

    {

    root=new node;

    root->data=ch;

    root->left=NULL;

    root->right=NULL;

    return;

    }

    temp1=new node;

    temp1->data=ch;

    temp1->right=temp1->left=NULL;

    temp=search(root,ch);

    if(temp->data>ch)

    temp->left=temp1;

    else

    temp->right=temp1;

    Non recursive In order Traversing

  • Advanced Data Structures

    72

    }

    node *search(node *temp,int ch)

    {

    if(root== NULL)

    {

    cout right== NULL)

    return temp;

    if(temp->data>ch)

    { if(temp->left==NULL) return temp;

    search(temp->left,ch);}

    else

    { if(temp->right==NULL) return temp;

    search(temp->right,ch);

    } }

    void display(node *temp)

    {

    if(temp==NULL)

    return ;

    display(temp->left);

    coutright);

    }

    void inorder( node *root)

    {

    node *p;

    p=root;

    top=0;

    do

    {

    while(p!=NULL)

    {

    stk[top]=p->data;

    top++;

    p=p->left;

    }

    if(top>0)

    {

    p=pop(root);

  • Advanced Data Structures

    73

    cout data;

    p=p->right;

    }

    }while(top!=0 || p!=NULL);

    }

    node * pop(node *p)

    {

    int ch;

    ch=stk[top-1];

    if(p->data==ch)

    {

    top--;

    return p;

    }

    if(p->data>ch)

    pop(p->left);

    else

    pop(p->right);

    }

    };

    void main()

    {

    tree t1;

    int ch,n,i;

    while(1)

    {

    cout > ch;

    switch(ch)

    {

    case 1: cout n;

    for(i=1;i> ch;

    t1.insert(ch);

    }

    break;

    case 2: t1.display(t1.root);break;

    case 3: t1.inorder(t1.root); break;

    case 4: exit(1);

    }

    }

  • Advanced Data Structures

    74

    }

    OUTPUT

    1.INSERT

    2.DISPLAY 3.INORDER TRAVERSE

    4.EXIT

    Enter your choice:1

    enter no of elements to inser

    5 24 36 11 44 2 21

    1.INSERT

    2.DISPLAY 3.INORDER TRAVERSE

    4.EXIT

    Enter your choice:3

    251121243644

    1.INSERT

    2.DISPLAY 3.INORDER TRAVERSE

    4.EXIT

    Enter your choice:3

    251121243644

    1.INSERT

    2.DISPLAY 3.INORDER TRAVERSE

    4.EXIT

    Enter your choice:4

  • Advanced Data Structures

    75

    Algorithm postorder( node root)

    {

    1. current = root; //start traversal at root node

    2. v = 0;

    3. if(current is NULL)

    the binary tree is empty

    4. if(current is not NULL)

    a. push current into stack;

    b. push 1 onto stack;

    c. current = current->llink;

    d. while(stack is not empty)

    if(current is not NULL and v is 0)

    {

    push current and 1 onto stack;

    current = current->llink;

    }

    else

    {

    pop stack into current and v;

    if(v == 1)

    {

    push current and 2 onto stack;

    current = current->rlink;

    v = 0;

    }

    else

    visit current;

    }}

    Non recursive Post order Traversing Algorithm

  • Advanced Data Structures

    76

    #include

    #include

    #include

    class node

    {

    public:

    class node *left;

    class node *right;

    int data;

    };

    class tree: public node

    {

    public:

    int stk[50],top;

    node *root;

    tree()

    {

    root=NULL;

    top=0;

    }

    void insert(int ch)

    {

    node *temp,*temp1;

    if(root== NULL)

    {

    root=new node;

    root->data=ch;

    root->left=NULL;

    root->right=NULL;

    return;

    }

    temp1=new node;

    temp1->data=ch;

    temp1->right=temp1->left=NULL;

    temp=search(root,ch);

    if(temp->data>ch)

    temp->left=temp1;

    else

    temp->right=temp1;

    Non recursive Post order Traversing

  • Advanced Data Structures

    77

    }

    node *search(node *temp,int ch)

    {

    if(root== NULL)

    {

    cout right== NULL)

    return temp;

    if(temp->data>ch)

    { if(temp->left==NULL) return temp;

    search(temp->left,ch);}

    else

    { if(temp->right==NULL) return temp;

    search(temp->right,ch);

    } }

    void display(node *temp)

    {

    if(temp==NULL)

    return ;

    display(temp->left);

    coutdata;

    top++;

    if(p->right!=NULL)

    stk[top++]=-p->right->data;

    p=p->left;

    }

  • Advanced Data Structures

    78

    while(stk[top-1] > 0 || top==0)

    {

    if(top==0) return;

    cout left);

    else

    pop(p->right);

    }

    };

    void main()

    {

    tree t1;

    int ch,n,i;

    clrscr();

    while(1)

    {

    cout > ch;

    switch(ch)

    {

    case 1: cout

  • Advanced Data Structures

    79

    }

    break;

    case 2: t1.display(t1.root);break;

    case 3: t1.postorder(t1.root); break;

    case 4: exit(1);

    }

    }

    }

    OUTPUT

    1.INSERT

    2.DISPLAY 3.POSTORDER TRAVERSE

    4.EXIT

    Enter your choice:1

    enter no of elements to insert:

    enter the elements7

    5 24 36 11 44 2 21

    1.INSERT

    2.DISPLAY 3.POSTORDER TRAVERSE

    4.EXIT

    Enter your choice:2

    2 5 11 21 24 36 44

    1.INSERT

    2.DISPLAY 3.POSTORDER TRAVERSE

    4.EXIT

    Enter your choice:3

    2 21 11 44 36 24 5

    1.INSERT

    2.DISPLAY 3.POSTORDER TRAVERSE

    4.EXIT

    Enter your choice:4

  • Advanced Data Structures

    80

    Algorithm quicksort(a[],p,q)

    {

    V=a[p];

    i=p;

    j=q;

    if(i v);

    Repeat

    J=j-1;

    Until(a[j]

  • Advanced Data Structures

    81

    #include

    #include

    int a[10],l,u,i,j;

    void quick(int *,int,int);

    void main()

    {

    clrscr();

    cout

  • Advanced Data Structures

    82

    a[l]=temp;

    cout

  • Advanced Data Structures

    83

    Algorithm Mergesort(low,high)

    {

    If(low

  • Advanced Data Structures

    84

    #include

    #include

    void mergesort(int *,int,int);

    void merge(int *,int,int,int);

    int a[20],i,n,b[20];

    void main()

    {

    clrscr();

    cout n;

    cout

  • Advanced Data Structures

    85

    while(h

  • Advanced Data Structures

    86

    Definition: A heap is a list in which each element contains a key, such that the key in the

    element at position k in the list is at least as large as the key in the element at position 2k + 1

    (if it exists), and 2k + 2 (if it exists)

    Algorithm Heapify(a[],n)

    {

    For i=n/2 to 1 step -1

    Adjustify (a,i,n);

    }

    Algorithm Adjustify(a[],i,n)

    {

    Repeat

    {

    J=leftchild(i)

    Compare left and right child of a[i] and store the index of grater number in j

    Compare a[j] and a[i]

    If (a[j]>a[i]) then

    Copy a[j] to a[i] and move to next level

    }until(j

  • Advanced Data Structures

    87

    #include

    #include

    int a[20],n;

    main()

    {

    int i,j,temp;

    clrscr();

    printf("ente n");

    scanf("%d",&n);

    printf("enter the elements");

    for(i=1;i

  • Advanced Data Structures

    88

    adjust(int a[],int i,int n)

    {

    int j,iteam;

    j=2*i;

    iteam=a[i];

    while(j

  • Advanced Data Structures

    89

    #include

    #include

    int min(int a,int b);

    int cost[10][10],a[10][10],i,j,k,c;

    void main()

    {

    int n,m;

    cout n;

    cout m;

    cout>j>>c;

    a[i][j]=cost[i][j]=c;

    }

    for(i=1;i

  • Advanced Data Structures

    90

    getch();

    }

    int min(int a,int b)

    {

    if(a

  • Advanced Data Structures

    91

    Algorithm Insert(item)

    {

    If(tree is empty) then root is empty

    Otherwise

    {

    temp=search(item); // temp is the node where search for the

    item halts

    if( item > temp) then temp-right=item;

    otherwise temp-left =item

    }

    }

    Algorithm Search(x, root)

    {

    if(tree is empty ) then print tree is empty

    otherwise

    If(x grater than root) search(root-right);

    Otherwise if(x less than root ) search(root-left)

    Otherwise return true

    }

    }

    Algorithm Delete(x)

    {

    Search for x in the tree

    If (not found) print not found

    Otherwise{

    If ( x has no child) delete x;

    If(x has left child) move the left child to x position

    If(x has right child) move the right child to x position

    If(x has both left and right children) replace x with

    greatest of left subtree of x or smallest of right

    subtree of x and delete selected node in the subtree

    }

    }

    Algorithms for Binary Search Tree

  • Advanced Data Structures

    92

    #include

    #include

    #include

    class node

    {

    public:

    class node *left;

    class node *right;

    int data;

    };

    class tree: public node

    {

    public:

    int stk[50],top;

    node *root;

    tree()

    {

    root=NULL;

    top=0;

    }

    void insert(int ch)

    {

    node *temp,*temp1;

    if(root== NULL)

    {

    root=new node;

    root->data=ch;

    root->left=NULL;

    root->right=NULL;

    return;

    }

    temp1=new node;

    temp1->data=ch;

    temp1->right=temp1->left=NULL;

    temp=search(root,ch);

    if(temp->data>ch)

    temp->left=temp1;

    else

    temp->right=temp1;

    Binary Search Tree

  • Advanced Data Structures

    93

    }

    node *search(node *temp,int ch)

    {

    if(root== NULL)

    {

    cout right== NULL)

    return temp;

    if(temp->data>ch)

    { if(temp->left==NULL) return temp;

    search(temp->left,ch);}

    else

    { if(temp->right==NULL) return temp;

    search(temp->right,ch);

    } }

    void display(node *temp)

    {

    if(temp==NULL)

    return ;

    display(temp->left);

    coutdata==ch)

    {

    top--;

    return p;

    }

    if(p->data>ch)

    pop(p->left);

    else

    pop(p->right);

    }

    };

  • Advanced Data Structures

    94

    void main()

    {

    tree t1;

    int ch,n,i;

    clrscr();

    while(1)

    {

    cout ch;

    switch(ch)

    {

    case 1: cout

  • Advanced Data Structures

    95

    #include

    #include

    #include

    #define MAX 10

    int find(int i,int j);

    void print(int,int);

    int p[MAX],q[MAX],w[10][10],c[10][10],r[10][10],i,j,k,n,m;

    char idnt[7][10];

    void main()

    {

    clrscr();

    cout >n;

    cout

  • Advanced Data Structures

    96

    c[i][j]=w[i][j]+c[i][k-1]+c[k][j];

    }

    }

    cout

  • Advanced Data Structures

    97

    1. What is the difference between an ARRAY and a LIST?

    2. What is faster : access the element in an ARRAY or in a LIST?

    3. Define a constructor - what it is and how it might be called (2 methods).

    4. Describe PRIVATE, PROTECTED and PUBLIC - the differences and give examples.

    5. What is a COPY CONSTRUCTOR and when is it called (this is a frequent question !)?

    6. Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have

    a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and

    SQUARE.

    7. What is the word you will use when defining a function in base class to allow this

    function to be a polimorphic function?

    8. You have two pairs: new() and delete() and another pair : alloc() and free(). Explain

    differences between eg. new() and malloc()

    9. Difference between C structure and C++ structure.

    10. Diffrence between a assignment operator and a copy constructor

    11. What is the difference between overloading and overridding?

    12. Explain the need for Virtual Destructor.

    13. Can we have Virtual Constructors?

    14. What are the different types of polymorphism?

    15. What are Virtual Functions? How to implement virtual functions in C

    16. What are the different types of Storage classes?

    17. What is Namespace?

    Viva Voice Questions

  • Advanced Data Structures

    98

    18. Difference between vector and array?

    19. How to write a program such that it will delete itself after exectution?

    20. Can we generate a C++ source code from the binary file?

    21. What are inline functions?

    22. What is strstream ?

    23. Explain passing by value, passing by pointer and passing by reference

    24. Have you heard of mutable keyword?

    25. Is there something that I can do in C and not in C++?

    26. What is the difference between calloc and malloc?

    27. What will happen if I allocate memory using new and free it using free or allocate

    sing calloc and free it using delete?

    28. When shall I use Multiple Inheritance?

    29. How to write Multithreaded applications using C++?

    30. Write any small program that will compile in C but not in C++

    31. What is Memory Alignment?

    32. what is the difference between a tree and a graph?

    33. How to insert an element in a binary search tree?

    34. How to delete an element from a binary search tree?

    35. How to search an element in a binary search tree?

    36. what is the disadvantage in binary search tree?

    37. what is ment by height balanced tree?

    38. Give examples for height blanced tree?

    39. What is a 2-3 tree?

  • Advanced Data Structures

    99

    40. what is a dictonary?

    41.What is a binary search tree?

    42. what is an AVL tree?

    43. how height balancing is performed in AVL tree?

    44. what is a Red Black tree?

    45. what is difference between linked list and an array?

    46. how dynamic memory allocation is performed in c++?

    47. what are tree traversing techniques?

    48. what are graph traversing techniques?

    49. what is the technique in quick sort.?

    50 what is the technique in merge sort?

    51. what is data structure.

    52. how to implement two stacks in an array?

    53. what is ment by generic programming?

    54. write the syntax for function templet?

    55. write the syntax for class templet?

    56. what is ment by stream?

    57. what is the base class for all the streams?

    58. how to create a file in c++?

    59. how do you read a file in c++?

    60. how do you write a file in c++?