Professional Documents
Culture Documents
1
Data Structures
Data Structures: An aggregation of atomic and composite data
types into a set with defined relationships.
Space complexity of a program is the amount of memory it
needs to run to completion.
Time complexity of a program is the amount of computer time
it needs to run to completion.
Binary search:
First=1
Last=end
Loop(first<=last)
mid=(first+last)/2
if(target>list[mid])
01/26/09 05:42 AM 2
Data Structures
First=mid+1
Else if target <list[mid]
Last=mid-1
Else
First=last+1
Locn=mid
If target = list[mid]
Found=true
Else
Found=false
Return found
01/26/09 05:42 AM 3
Data Structures
A hash search is a search in which the key through an
algorithmic function, determines the location of the data.
A linked list is an ordered collection of data in which each
element contains the location of the next element:data and
link.
The nodes in a ll are called self – referential structures.
In the circularly – linked list, the last node’s link points to the
first node of the list.
A doubly ll is a data structure in which the node has a pointer
to both its successor and its predecessor.
A stack is a linear list in which all additions and deletions are
restricted to one end called the top of the stack.{LIFO
structure}.
Eg., coins and dishes(Balancing Symbols,Postfix…)
01/26/09 05:42 AM 4
Data Structures
Common operations:
Push
Pop
Stack top
Infix a + b
Postfix a b +
Prefix + a b
01/26/09 05:42 AM 5
Data Structures
Algorithm Queens8
Createstack(stack)
Set row=1 and col =0
Loop (row<=boardsize)
Loop(col<=boardsize and row<=boardsize)
add 1 to col
if (not guarded(row,col))
place queen at board [row][col]
pushstack(stack,[row][col])
add 1 to row
set col = 0
01/26/09 05:42 AM 6
Data Structures
Loop(col>=boardsize)
Popstack(stack,[row,col])
Remove queen at board[row][col]
Printboard(stack)
Operations:
Enq
Deq
Q front,rear.
01/26/09 05:42 AM 7
Data Structures
Recursion is a repetitive process in which an algorithm calls
itself.
Towers of Hanoi:
01/26/09 05:42 AM 8
Data Structures
A tree consists of a finite set of elements called nodes and a
finite set of directed lines called branches that connect the
nodes.
The height of the tree is the level of the leaf in the longest
path from the root plus one.
01/26/09 05:42 AM 9
Data Structures
Preorder: - Root,left,right
Inorder – left,root, right
Post order – left,right,root
General algorithm:
If(root is not null)
Inorder(root->leftsubtree)
Inorder(root)
Inorder(root->rightsubtree)
01/26/09 05:42 AM 10
Data Structures
In the depth – first traversal, the processing proceeds along a
path from the root through one child to the most distant
descendent of that child before processing a second child.
01/26/09 05:42 AM 12
Data Structures
An m-way tree is a search tree in which each node can have
from zero to m subtrees , where m is defined as the order of
the tree.
01/26/09 05:42 AM 13
Data Structures
B – tree and B+ tree:
Each data entry must be represented at the leaf level.
Each leaf node has one additional pointer, which is used to
move to the next leaf node in sequence.(B+)
01/26/09 05:42 AM 14
Data Structures
A directed graph or digraph is a graph in which each line has a
direction to its successor.
An undirected graph is a graph in which there is no direction
on the lines known as edges.
01/26/09 05:42 AM 15
Data Structures
A network is a graph whose lines are weighted.
01/26/09 05:42 AM 16
Data Structures
Solution to collision:
Linear Probing
Quadratic probing(i2)
Double Hashing(R-(X Mod R), R – prime<X)
Rehashing(create a new table, twice as large as the old table
size)
Extendible hashing(00,01,10,11)
01/26/09 05:42 AM 17
Data Structures
Merge Sort(O(N log N))
Quick sort(O(N log N) – O(N2))
External sort
Polyphase Merge
External selection
Multiway merge
01/26/09 05:42 AM 18
Data Structures
A simple way to check if a problem is in NP is to phrase the
problem as a y/n problem?
01/26/09 05:42 AM 19
Data Structures
Any subset that satisfies these constraints is called a feasible
solution. We need to find a feasible solution that either
maximizes or minimizes a given objective function.
01/26/09 05:42 AM 20
Data Structures
Np – complete problems can be solved inpolynomial time.
01/26/09 05:42 AM 21