Data Tructure 2 Marks

download Data Tructure 2 Marks

of 19

Transcript of Data Tructure 2 Marks

  • 8/13/2019 Data Tructure 2 Marks

    1/19

    CS2201- DATA STRUCTURES

    UNIT I LINEAR STRUCTURES

    1. Define Data Structures

    Data Structures is defined as the way of organizing all data items that consider notonly the elements stored but also stores the relationship between the elements.

    2. Define Linked Lists

    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 itssuccessor. We call this thee!t "ointer. #he last cell$se!t pointer points to %LL.

    3. State te different t!"es #f $inked $ists#he different types of linked list include singly linked list, doubly linked list and

    circular linked list.

    %. List te &asic #"erati#ns carried #ut in a $inked $ist

    #he basic operations carried out in a linked list include&' (reation of a list' )nsertion of a node' Deletion of a node' *odification of a node' #ra+ersal of the list

    '. List #ut te ad(anta)es #f usin) a $inked $ist

    ' )t is not necessary to specify the number of elements in a linked list during itsdeclaration' Linked list can grow and shrink in size depending upon the insertion anddeletion that occurs in the list' )nsertions and deletions at any place in a list can be handled easily andefficiently' linked list does not waste any memory space

    *. List #ut te disad(anta)es #f usin) a $inked $ist

    ' Searching a particular element in a list is difficult and time consuming' linked list will use more storage space than an array to store the same numberof elements

    +. List #ut te a""$icati#ns #f a $inked $ist

    Some of the important applications of linked lists are manipulation ofpolynomials, sparse matrices, stacks and -ueues.

  • 8/13/2019 Data Tructure 2 Marks

    2/19

    ,. State te difference &eteen arra!s and $inked $ists

    Arra!s Linked Lists

    Size of an array is fi!ed Size of a list is +ariable)t is necessary to specify thenumber of elements during

    declaration.

    )t is not necessary to specify thenumber of elements during

    declaration)nsertions and deletions aresomewhat difficult

    )nsertions and deletions are carriedout easily

    )t occupies less memory than alinked list for the same number ofelements

    )t occupies more memory

    . Define a stack

    Stack is an ordered collection of elements in which insertions and deletions arerestricted to one end. #he end from which elements are added andor remo+ed is referredto as top of the stack. Stacks are also referred as piles, push/down lists and last/in/first/out 0L)123 lists.

    10. List #ut te &asic #"erati#ns tat can &e "erf#r/ed #n a stack

    #he basic operations that can be performed on a stack are' "ush operation' "op operation' "eek operation' Empty check' 1ully occupied check

    11. State te different a!s #f re"resentin) e"ressi#ns#he different ways of representing e!pressions are' )nfi! otation' "refi! otation' "ostfi! otation

    12. State te ru$es t# &e f#$$#ed durin) infi t# "#stfi c#n(ersi#ns

    ' 1ully parenthesize the e!pression starting from left to right. Duringparenthesizing, the operators ha+ing higher precedence are first parenthesized' *o+e the operators one by one to their right, such that each operator replacestheir corresponding right parenthesis

    ' #he part of the e!pression, which has been con+erted into postfi! is to betreated as single operand

    13. State te difference &eteen stacks and $inked $ists

    #he difference between stacks and linked lists is that insertions and deletions mayoccur anywhere in a linked list, but only at the top of the stack

    1%. enti#n te ad(anta)es #f re"resentin) stacks usin) $inked $ists tan arra!s

  • 8/13/2019 Data Tructure 2 Marks

    3/19

    ' )t is not necessary to specify the number of elements to be stored in a stackduring its declaration, since memory is allocated dynamically at run time when anelement is added to the stack' )nsertions and deletions can be handled easily and efficiently' Linked list representation of stacks can grow and shrink in size without wasting

    memory space, depending upon the insertion and deletion that occurs in the list' *ultiple stacks can be represented efficiently using a chain for each stack

    1'. Define a ueue

    4ueue is an ordered collection of elements in which insertions are restricted to oneend called the rear end and deletions are restricted to other end called the front end.4ueues are also referred as 1irst/)n/1irst/2ut 01)123 Lists.

    1*. Define a "ri#rit! ueue

    "riority -ueue is a collection of elements, each containing a key referred as thepriority for that element. Elements can be inserted in any order 0i.e., of alternating

    priority3, but are arranged in order of their priority +alue in the -ueue. #he elements aredeleted from the -ueue in the order of their priority 0i.e., the elements with the highestpriority is deleted first3. #he elements with the same priority are gi+en e-ual importanceand processed accordingly.

    1+. State te difference &eteen ueues and $inked $ists

    #he difference between -ueues and linked lists is that insertions and deletions mayoccur anywhere in the linked list, but in -ueues insertions can be made only in the rearend and deletions can be made only in the front end.

    1,. Define a Deue

    De-ue 0Double/Ended 4ueue3 is another form of a -ueue in which insertions anddeletions are made at both the front and rear ends of the -ueue. #here are two +ariationsof a de-ue, namely, input restricted de-ue and output restricted de-ue. #he inputrestricted de-ue allows insertion at one end 0it can be either front or rear3 only. #heoutput restricted de-ue allows deletion at one end 0it can be either front or rear3 only.

    1. Define an A&stract Data T!"e ADT4

    n abstract data type is a set of operations. D#s are mathematical abstractions5nowhere in an D#$s definition is there any mention of how the set of operations isimplemented. 2bjects such as lists, sets and graphs, along with their operations can be+iewed as abstract data types.

    20. 5at are te ad(anta)es #f /#du$arit!6

    ' )t is much easier to debug small routines than large routines' )t is easier for se+eral people to work on a modular program simultaneously' well/written modular program places certain dependencies in only oneroutine, making changes easier

    21. 5at are te #&7ecti(es #f stud!in) data structures6

  • 8/13/2019 Data Tructure 2 Marks

    4/19

    ' #o identify and create useful mathematical entities and operations to determinewhat classes of problems can be sol+ed using these entities and operations' #o determine the representation of these abstract entities and to implement theabstract operations on these concrete representation

    22. 5at are te t!"es #f ueues6' Linear 4ueues 6 #he -ueue has two ends, the front end and the rear end. #herear end is where we insert elements and front end is where we delete elements.We can tra+erse in a linear -ueue in only one direction ie3 from front to rear.' (ircular 4ueues 6 nother form of linear -ueue in which the last position isconnected to the first position of the list. #he circular -ueue is similar to linear-ueue has two ends, the front end and the rear end. #he rear end is where weinsert elements and front end is where we delete elements. We can tra+erse in acircular -ueue in only one direction ie3 from front to rear.' Double/Ended/4ueue 6 nother form of -ueue in which insertions anddeletions are made at both the front and rear ends of the -ueue.

    23. List te a""$icati#ns #f stacks

    ' #owers of 7anoi' 8e+ersing a string' 9alanced parenthesis' 8ecursion using stack' E+aluation of arithmetic e!pressions

    2%. List te a""$icati#ns #f ueues

    ' :obs submitted to printer' 8eal life line

    ' (alls to large companies' ccess to limited resources in %ni+ersities' ccessing files from file ser+er

    2'. 5! e need curs#r i/"$e/entati#n #f $inked $ists6

    *any languages such as 9S)( and 128#8 do not support pointers. )f linkedlists are re-uired and pointers are not a+ailable, then an alternati+e implementation mustbe used known as cursor implementation.

    UNIT II TREE STRUCTURES

  • 8/13/2019 Data Tructure 2 Marks

    5/19

    1. Define a tree

    tree is a collection of nodes. #he collection can be empty5 otherwise, a treeconsists of a distinguished node r, called the root, and zero or more nonempty 0sub3 trees#;, #.

    %. Define $ea(es

    #hese are the terminal nodes of the tree. #he nodes with degree > are always thelea+es.

    7ere, 9 and ( are leaf nodes.

    '. Define interna$ n#des

    #he nodes other than the root and the lea+es are called internal nodes.

    7ere, ( is the internal node.*. Define "arent n#de

    #he node which is ha+ing further sub/branches is called the parent node of those

  • 8/13/2019 Data Tructure 2 Marks

    6/19

    sub/branches.

    7ere, node ( is the parent node of D and E

    +. Define de"t and ei)t #f a n#de

    1or any node ni, the depth of ni is the length of the uni-ue path from the root toni. #he height of ni is the length of the longest path from ni to a leaf.

    ,. Define de"t and ei)t #f a tree

    #he depth of the tree is the depth of the deepest leaf. #he height of the tree ise-ual to the height of the root. lways depth of the tree is e-ual to height of the tree.

    . 5at d# !#u /ean &! $e(e$ #f te tree6

    #he root node is always considered at le+el zero, then its adjacent children aresupposed to be at le+el ; and so on.7ere, node is at le+el >, nodes 9 and ( are at le+el ; and nodes D and Eare at le+el

  • 8/13/2019 Data Tructure 2 Marks

    7/19

    full binary tree is a tree in which all the lea+es are on the same le+el and e+erynon/leaf node has e!actly two children.

    1%. Define a c#/"$ete &inar! tree

    complete binary tree is a tree in which e+ery non/leaf node has e!actly two

    children not necessarily to be on the same le+el.1'. State te "r#"erties #f a &inar! tree

    ' #he ma!imum number of nodes on le+el n of a binary tree is

  • 8/13/2019 Data Tructure 2 Marks

    8/19

    )nsertions and deletions in a node in+ol+e no data mo+ement e!cept therearrangement of pointers, hence less processing time.

    23. State te de/erits #f $inked re"resentati#n #f &inar! trees.

    ' Bi+en a node structure, it is difficult to determine its parent node

    ' *emory spaces are wasted for storing null pointers for the nodes, which ha+eone or no sub/trees' )t re-uires dynamic memory allocation, which is not possible in someprogramming language

    2%. Define a &inar! searc tree

    binary search tree is a special binary tree, which is either empty or it shouldsatisfy the following characteristics&E+ery node has a +alue and no two nodes should ha+e the same +alue i.e3 the+alues in the binary search tree are distinct

    ' #he +alues in any left sub/tree is less than the +alue of its parent node

    ' #he +alues in any right sub/tree is greater than the +alue of its parent node' #he left and right sub/trees of each node are again binary search trees

    2'. 5at is te use #f treaded &inar! tree6

    )n threaded binary tree, the %LL pointers are replaced by some addresses. #heleft pointer of the node points to its predecessor and the right pointer of the node points toits successor.

    2*. Tra(erse te )i(en tree usin) In#rder8 9re#rder and 9#st#rder tra(ersa$s.

    )norder & D 7 9 E 1 ( ) B :"reorder& 9 D 7 E ( 1 B ) :"ostorder& 7 D E 9 1 ) : B (

    2+. In te )i(en &inar! tree8 usin) arra! !#u can st#re te n#de % at ic $#cati#n6

    t location C; < / / / / F

  • 8/13/2019 Data Tructure 2 Marks

    9/19

    where L(n means Left (hild of node n and 8(n means 8ight (hild of node n

    UNIT III :ALANCED TREES

    1. Define A;L Tree.

    n empty tree is height balanced. )f # is a non/empty binary tree with #L and#8 as its left and right subtrees, then # is height balanced if

    i3 #L and #8 are height balanced andii3 GhL / h8GH ;

    Where hL and h8 are the heights of #L and #8 respecti+ely.2. 5at d# !#u /ean &! &a$anced trees6

    9alanced trees ha+e the structure of binary trees and obey binary search treeproperties. part from these properties, they ha+e some special constraints, which differfrom one data structure to another. 7owe+er, these constraints are aimed only at reducingthe height of the tree, because this factor determines the time comple!ity.Eg& IL trees, Splay trees.

    3. 5at are te cate)#ries #f A;L r#tati#ns6

    Let be the nearest ancestor of the newly inserted nod which has the balancing

    factor J or A; or /;.#he height of an emptytree is /;.

    '. Define s"$a! tree.

    splay tree is a binary search tree in which restructuring is done using a schemecalled splay. #he splay is a heuristic method which mo+es a gi+en +erte! + to the root ofthe splay tree using a se-uence of rotations.

    *. 5at is te idea &eind s"$a!in)6

  • 8/13/2019 Data Tructure 2 Marks

    10/19

    Splaying reduces the total accessing time if the most fre-uently accessed node ismo+ed towards the root. )t does not re-uire to maintain any information regarding theheight or balance factor and hence sa+es space and simplifies the code to some e!tent.

    +. List te t!"es #f r#tati#ns a(ai$a&$e in S"$a! tree.

    Let us assume that the splay is performed at +erte! +, whose parent andgrandparent are p and g respecti+ely. #hen, the three rotations are named as&

  • 8/13/2019 Data Tructure 2 Marks

    11/19

    13. 5at are te a""$icati#ns #f :-tree6

    ' Database implementation' )nde!ing on non primary key fields

    1%. 5at is te need f#r 9ri#rit! ueue6

    )n a multiuser en+ironment, the operating system scheduler must decide which ofthe se+eral processes to run only for a fi!ed period of time. 2ne algorithm uses -ueue.:obs are initially placed at the end of the -ueue. #he scheduler will repeatedly take thefirst job on the -ueue, run it until either it finishes or its time limit is up and place it at theend of the -ueue if it does not finish. #his strategy is not appropriate, because +ery shortjobs will soon to take a long time because of the wait in+ol+ed in the run.

    Benerally, it is important that short jobs finish as fast as possible, so these jobsshould ha+e precedence o+er jobs that ha+e already been running. 1urther more, somejobs that are not short are still +ery important and should ha+e precedence. #his particularapplication seems to re-uire a special kind of -ueue, known as priority -ueue. "riority-ueue is also called as 7eap or 9inary 7eap.

    1'. 5at are te "r#"erties #f &inar! ea"6

    i3 Structure "ropertyii3 7eap 2rder "roperty

    1*. 5at d# !#u /ean &! structure "r#"ert! in a ea"6

    heap is a binary tree that is completely filled with the possible e!ception at thebottom le+el, which is filled from left to right. Such a tree is known as a complete binarytree.

    1+. 5at d# !#u /ean &! ea" #rder "r#"ert!6

    )n a heap, for e+ery node M, the key in the parent of M is smaller than 0ore-ual to3 the key in M, with the e!ception of the root 0which has no parent3.

    1,. 5at are te a""$icati#ns #f "ri#rit! ueues6

    ' #he selection problem' E+ent simulation

    1. 5at d# !#u /ean &! te ter/ >9erc#$ate u"?6

    #o insert an element, we ha+e to create a hole in the ne!t a+ailable heap location.)nserting an element in the hole would sometimes +iolate the heap order property, so weha+e to slide down the parent into the hole. #his strategy is continued until the correct

    location for the new element is found. #his general strategy is known as a percolate up5the new element is percolated up the heap until the correct location is found.

    20. 5at d# !#u /ean &! te ter/ >9erc#$ate d#n?6

    When the minimum element is remo+ed, a hole is created at the root. Since theheap now becomes one smaller, it follows that the last element M in the heap must mo+esomewhere in the heap. )f M can be placed in the hole, then we are done.. #his isunlikely, so we slide the smaller of the hole$s children into the hole, thus pushing the hole

  • 8/13/2019 Data Tructure 2 Marks

    12/19

    down one le+el. We repeat this step until M can be placed in the hole. #hus, our action isto place M in its correct spot along a path from the root containing minimum children.#his general strategy is known as percolate down.

    UNIT I; =AS=IN@ AND SET

    1. Define =asin).

    7ashing is the transformation of string of characters into a usually shorter fi!ed length+alue or key that represents the original string. 7ashing is used to inde! and retrie+eitems in a database because it is faster to find the item using the short hashed key than tofind it using the original +alue.

    2. 5at d# !#u /ean &! as ta&$e6

    #he hash table data structure is merely an array of some fi!ed size, containing thekeys. key is a string with an associated +alue. Each key is mapped into some number inthe range > to tablesize/; and placed in the appropriate cell.

    3. 5at d# !#u /ean &! as functi#n6

    hash function is a key to address transformation which acts upon a gi+en key tocompute the relati+e position of the key in an array. #he choice of hash function shouldbe simple and it must distribute the data e+enly. simple hash function is hashNkey@keymod tablesize.

    %. 5rite te i/"#rtance #f asin).

    ' *aps key with the corresponding +alue using hash function.' 7ash tables support the efficient addition of new entries and the time spent onsearching for the re-uired data is independent of the number of items stored.

    '. 5at d# !#u /ean &! c#$$isi#n in asin)6

    When an element is inserted, it hashes to the same +alue as an alreadyinserted element, and then it produces collision.

    *. 5at are te c#$$isi#n res#$uti#n /et#ds6

    ' Separate chaining or E!ternal hashing' 2pen addressing or (losed hashing

    +. 5at d# !#u /ean &! se"arate cainin)6

    Separate chaining is a collision resolution techni-ue to keep the list of all elementsthat hash to the same +alue. #his is called separate chaining because each hash table

  • 8/13/2019 Data Tructure 2 Marks

    13/19

    element is a separate chain 0linked list3. Each linked list contains all the elements whosekeys hash to the same inde!.

    ,. 5rite te ad(anta)e #f se"arate cainin).

    ' *ore number of elements can be inserted as it uses linked lists.

    . 5rite te disad(anta)es #f se"arate cainin).

    ' #he elements are e+enly distributed. Some elements may ha+e moreelements and some may not ha+e anything.' )t re-uires pointers. #his leads to slow the algorithm down a bit because ofthe time re-uired to allocate new cells, and also essentially re-uires theimplementation of a second data structure.

    10. 5at d# !#u /ean &! #"en addressin)6

    2pen addressing is a collision resol+ing strategy in which, if collision occursalternati+e cells are tried until an empty cell is found. #he cells h>0!3, h;0!3, h3@>. #he function1 is the collision resolution strategy.

    11. 5at are te t!"es #f c#$$isi#n res#$uti#n strate)ies in #"en addressin)6

    ' Linear probing' 4uadratic probing' Double hashing

    12. 5at d# !#u /ean &! 9r#&in)6

    "robing is the process of getting ne!t a+ailable hash table array cell.

    13. 5at d# !#u /ean &! $inear "r#&in)6

    Linear probing is an open addressing collision resolution strategy in which 1 is alinear function of i, 10i3@i. #his amounts to trying se-uentially in search of an empty cell.)f the table is big enough, a free cell can always be found, but the time to do so can get-uite large.

    1%. 5at d# !#u /ean &! "ri/ar! c$usterin)6

    )n linear probing collision resolution strategy, e+en if the table is relati+elyempty, blocks of occupied cells start forming. #his effect is known as primaryclustering means that any key hashes into the cluster will re-uire se+eral attemptsto resol+e the collision and then it will add to the cluster.

    1'. 5at d# !#u /ean &! uadratic "r#&in)6

    4uadratic probing is an open addressing collision resolution strategy in which 10i3@i

  • 8/13/2019 Data Tructure 2 Marks

    14/19

    1*. 5at d# !#u /ean &! sec#ndar! c$usterin)6

    lthough -uadratic probing eliminates primary clustering, elements that hash to thesame position will probe the same alternati+e cells. #his is known as secondaryclustering.

    1+. 5at d# !#u /ean &! d#u&$e asin)6

    Double hashing is an open addressing collision resolution strategy in [email protected]

  • 8/13/2019 Data Tructure 2 Marks

    15/19

    ' *aintain list of duplicate copies of web pages' (onstructing a minimum spanning tree for a graph

    2'. 5at d# !#u /ean &! dis7#int set ADT6

    collection of non/empty disjoint sets S@S;,S

  • 8/13/2019 Data Tructure 2 Marks

    16/19

    graph B consist of a nonempty set I which is a set of nodes of the graph, a set Ewhich is the set of edges of the graph, and a mapping from the set for edge E to a set ofpairs of elements of I. )t can also be represented as B@0I, E3.

    2. Define ad7acent n#des.

    ny two nodes which are connected by an edge in a graph are called adjacentnodes. 1or e!ample, if an edge ! O E is associated with a pair of nodes 0u,+3 where u, + OI, then we say that the edge ! connects the nodes u and +.

    3. 5at is a directed )ra"6

    graph in which e+ery edge is directed is called a directed graph.

    %. 5at is an undirected )ra"6

    graph in which e+ery edge is undirected is called a directed graph.

    '. 5at is a $##"6

    n edge of a graph which connects to itself is called a loop or sling.*. 5at is a si/"$e )ra"6

    simple graph is a graph, which has not more than one edge between a pair ofnodes than such a graph is called a simple graph.

    +. 5at is a ei)ted )ra"6

    graph in which weights are assigned to e+ery edge is called a weighted graph.

    ,. Define #utde)ree #f a )ra"6

    )n a directed graph, for any node +, the number of edges which ha+e + as their

    initial node is called the out degree of the node +.. Define inde)ree #f a )ra"6

    )n a directed graph, for any node +, the number of edges which ha+e + as theirterminal node is called the indegree of the node +.

    10. Define "at in a )ra"6

    #he path in a graph is the route taken to reach terminal node from a starting node.

    11. 5at is a si/"$e "at6

    path in a diagram in which the edges are distinct is called a simple path. )t is

    also called as edge simple.12. 5at is a c!c$e #r a circuit6

    path which originates and ends in the same node is called a cycle or circuit.

    13. 5at is an ac!c$ic )ra"6

    simple diagram which does not ha+e any cycles is called an acyclic graph.

  • 8/13/2019 Data Tructure 2 Marks

    17/19

    1%. 5at is /eant &! str#n)$! c#nnected in a )ra"6

    n undirected graph is connected, if there is a path from e+ery +erte! to e+eryother +erte!. directed graph with this property is called strongly connected.

    1'. 5en is a )ra" said t# &e eak$! c#nnected6

    When a directed graph is not strongly connected but the underlying graph isconnected, then the graph is said to be weakly connected.

    1*.Na/ete different a!s #f re"resentin) a )ra"6

    a.djacencymatri! b. djacency list

    1+. 5at is an undirected ac!c$ic )ra"6

    When e+ery edge in an acyclic graph is undirected, it is called an undirectedacyclic graph. )t is also called as undirected forest.

    1,. 5at are te t# tra(ersa$ strate)ies used in tra(ersin) a )ra"6 a.9readthfirstsearch b. Depth first search

    1. 5at is a /ini/u/ s"annin) tree6

    minimum spanning tree of an undirected graph B is a tree formed from graphedges that connects all the +ertices of B at the lowest total cost.

    20. Na/e t# a$)#rit/s t# find /ini/u/ s"annin) tree

    Uruskal$salgorithm "rim$s algorithm

    21. Define )ra" tra(ersa$s.

    #ra+ersing a graph is an efficient way to +isit each +erte! and edge e!actly once.

    22. List te t# i/"#rtant ke! "#ints #f de"t first searc.

    i3 )f path e!ists from one node to another node, walk across the edge 6 e!ploringthe edge.ii3 )f path does not e!ist from one specific node to any other node, return to thepre+ious node where we ha+e been before 6 backtracking.

    23. 5at d# !#u /ean &! &readt first searc :BS46

    91S performs simultaneous e!plorations starting from a common point andspreading out independently.

    2%.Differentiate:BSandDBS.

    N#. DBS :BS

    ;. 9acktracking is possible from adead end

    9acktracking is not possible

  • 8/13/2019 Data Tructure 2 Marks

    18/19

  • 8/13/2019 Data Tructure 2 Marks

    19/19

    F. E!plain how stack is applied for e+aluating an arithmetic e!pression.C. E!plain cursor implementation of list.

    %nit ))

    ;.Write an algorithm to find an element from binary search tree.