Basic Data Structures - University Of Marylandterpconnect.umd.edu/.../datastructures.pdf · Basic...
Transcript of Basic Data Structures - University Of Marylandterpconnect.umd.edu/.../datastructures.pdf · Basic...
Basic Data StructuresBasic Data Structures
Presented by:Alok K. PriyadarshiReading Group, CIM LabUniversity of MarylandSept. 29th, 2000
IntroductionIntroduction
• Algorithms + Data Structures = Programs
• Algorithm: describes how to manipulate information.
• Data Structure: provides a logical basis for organization ofthat information in the computer.
Example: Good Use of Data StructuresExample: Good Use of Data Structures
•• Computation of Fibonacci NumbersComputation of Fibonacci Numbers
F(n) = n if n < 2 F(n) = n if n < 2
F(n) = F(n-1) + F(n-2) otherwise F(n) = F(n-1) + F(n-2) otherwise
•• Algorithm 1Algorithm 1
fib1( n ) fib1( n )
if( n<2 ) then return n if( n<2 ) then return n
else return fib1( n-1 ) + fib1( n-2 ) else return fib1( n-1 ) + fib1( n-2 )
Analysis: O(1.63 n) -> EXPONENTIAL !!!
10
8
9
7 7
8
6
Example: Contd.Example: Contd.
•• Algorithm 2Algorithm 2
fib2( n ) fib2( n )
f = new f = new intint[n+1][n+1]
f[0] = 0; f[1] = 1; f[0] = 0; f[1] = 1;
for j = 2 to n do for j = 2 to n do
f[j] = f[j-1] + f[j-2] f[j] = f[j-1] + f[j-2]
return f[n] return f[n]
Analysis: O(n)Analysis: O(n)
Use arrays to save valuesUse arrays to save values
TopicsTopicsElementary Data StructuresElementary Data Structures
•• Lists: Lists: Array ImplementationArray Implementation
Linked list Implementation Linked list Implementation
Stack and queue Stack and queue
Single, double and circular linked list Single, double and circular linked list
•• Trees: Trees: Binary TreeBinary Tree
Binary Search Tree Binary Search Tree
OperationsOperationsTwo types : Query OperationsTwo types : Query Operations
Modifying Operations Modifying Operations
•• Search( S, k ): Key[x] = kSearch( S, k ): Key[x] = k
•• Insert( S, x )Insert( S, x )
•• Delete( S, x )Delete( S, x )
•• Minimum( S )Minimum( S )
•• Maximum( S )Maximum( S )
•• Successor( S, x )Successor( S, x )
•• Predecessor( S, x )Predecessor( S, x )
Linear ListLinear List•• Ordered set of elements,Ordered set of elements,
a a11, a, a22, ……………………..a, ……………………..ann
•• Size: nSize: n
•• L = {.….aL = {.….ai-1i-1, a, aii, a, ai+1i+1……}……}
a ai-1i-1: Predecessor: Predecessor
a ai+1i+1: Successor: Successor
•• Opns: insert( x, k ), delete( x )Opns: insert( x, k ), delete( x )
find( x ), find_kth( k ) find( x ), find_kth( k )
Array implementation of ListArray implementation of List
•• List size estimation is requiredList size estimation is required
•• Insert and delete are costly: O(n)Insert and delete are costly: O(n)
•• find(x): O(n)find(x): O(n)
•• find_kth(k): O(1)find_kth(k): O(1)
…...not usually used …...not usually used
Linked ListLinked List•• No contiguous memory locationsNo contiguous memory locations
•• Series of Objects linked successivelySeries of Objects linked successively
•• Use an extra field (link) to indicateUse an extra field (link) to indicateaddress of next objectaddress of next object
a1 a2 a3 a4
rootroot datadata nextnext NullNull
Programming detailsProgramming details
•• class Node{class Node{
Data element; Data element;
Node* next; Node* next;
} }
•• class List{class List{
Node* root; Node* root;
} }
Programming details( Contd. )Programming details( Contd. )•• List-search( L, K )List-search( L, K )
x = root[L] x = root[L]
while ( x != Null ) and ( key[x] != K ) while ( x != Null ) and ( key[x] != K )
do ( x = next[x] ) do ( x = next[x] )
return x; return x;
•• List-Insert( L, x )List-Insert( L, x )
next[x] = head[L] next[x] = head[L]
head[L] = x head[L] = x
•• List-Delete( L, x )List-Delete( L, x )
List-search( L, Key[x] ) List-search( L, Key[x] )
prev[x] = next[x] prev[x] = next[x]
delete( x ) delete( x )
O(n)O(n)
��������
����������
����
O(1)O(1)
O(n)O(n)
Other Linked ListsOther Linked Lists
Doubly Linked ListDoubly Linked List
•• Also point to the previous elementAlso point to the previous element
•• Extra space requirementExtra space requirement
•• Doubles the cost of insertion and deletionsDoubles the cost of insertion and deletions
•• Simplifies deletionsSimplifies deletions
a2a1 a3
prevprev datadata nextnextrootroot
ApplicationApplication
•• Polynomial representationPolynomial representation
p = 10xp = 10x10001000 + 5x + 5x1414 + 1 + 1
∑=
=n
i
ixaxf i
0
.)(
145 01100010
pp
StackStack
•• top[s]: indexes most recently inserted elementtop[s]: indexes most recently inserted element
•• Opns: Opns: STACK_EMPTY( S ) STACK_FULL( S )STACK_EMPTY( S ) STACK_FULL( S )
PUSH( S, x ) POP( S ) PUSH( S, x ) POP( S )
3
2
1
Push PopL I F OL I F O
Stacks of Plates in cafeteriaStacks of Plates in cafeteria
Stack OperationsStack Operations•• STACK_EMPTY( S )STACK_EMPTY( S )
if top[S] = 0 if top[S] = 0
then return TRUE then return TRUE
else return FALSE else return FALSE
•• STACK_FULL( S )STACK_FULL( S )
if top[S] = n if top[S] = n
then return TRUE then return TRUE
else return FALSE else return FALSE
•• PUSH( S, x )PUSH( S, x )
if STACK_FULL( S ) if STACK_FULL( S )
then error “Overflow” then error “Overflow”
else top[S] = top[S] + 1 else top[S] = top[S] + 1
S[ top[S] ] = x S[ top[S] ] = x
•• POP( S )POP( S )
if STACK_EMPTY( S ) if STACK_EMPTY( S )
then error “Underflow” then error “Underflow”
else top[S] = top[S] - 1 else top[S] = top[S] - 1
return S[ top[S]+1 ] return S[ top[S]+1 ]
Stack ApplicationsStack Applications
•• Undo-RedoUndo-Redo
•• Infix to postfix conversionInfix to postfix conversion
a+b*c+(d*e+f)*g = a b c * + d e * f + g * + a+b*c+(d*e+f)*g = a b c * + d e * f + g * +
•• Postfix expression evaluationPostfix expression evaluation
•• Function calls in RecursionFunction calls in Recursion
QueueQueue
•• head[Q] : indexes its headhead[Q] : indexes its head
•• tail[Q] : indexes the next location at which element will betail[Q] : indexes the next location at which element will beinsertedinserted
3 2 1
F I F OF I F O
EnqueEnque DequeDeque
7 35
Head[Q]Head[Q] Tail[Q]Tail[Q]
Enque: at tailEnque: at tailDeque: at headDeque: at head
Queue ( Contd. )Queue ( Contd. )
•• Elements: head[Q], head[Q]+1…….tail[Q]-1Elements: head[Q], head[Q]+1…….tail[Q]-1
•• Initially: head[Q] = tail[Q] = 1Initially: head[Q] = tail[Q] = 1
•• Empty: head[Q] = tail[Q]Empty: head[Q] = tail[Q]
•• Full: head[Q] = tail[Q]+1Full: head[Q] = tail[Q]+1
1 2 3 4 5 6
Queue OperationsQueue Operations
•• Enqueue( Q, x )Enqueue( Q, x )
Q[ tail[Q] ] = xQ[ tail[Q] ] = x
if tail[Q] = length[Q] if tail[Q] = length[Q]
then tail[Q] = 1 then tail[Q] = 1
else tail[Q] = tail[Q]+1 else tail[Q] = tail[Q]+1
•• Dequeue( Q )Dequeue( Q )
x = Q[ head[Q] ] x = Q[ head[Q] ]
if head[Q] = length[Q] if head[Q] = length[Q]
then head[Q] = 1 then head[Q] = 1
else head[Q] = head[Q]+1 else head[Q] = head[Q]+1
3 5
1 2 3 4 5 6
2 845
1 2 3 4 5 6
headhead tailtail headheadtailtail
Queue ApplicationsQueue Applications
•• Jobs sent to line printerJobs sent to line printer
•• Requests to file serverRequests to file server
•• Queuing theoryQueuing theory
TreesTrees
•• Recursive definition:Recursive definition:
Root + zero or more subtrees each of Root + zero or more subtrees each ofwhose roots are connected to the root.whose roots are connected to the root.
•• n nodes => n-1 edgesn nodes => n-1 edges
root
T1 T2 T3 Tn…….
Trees ( Contd. )Trees ( Contd. )
•• Relations: leaves, siblings,Relations: leaves, siblings,
parent, grandparent/children parent, grandparent/children
•• Properties: Degree, DepthProperties: Degree, Depth
A
EDCB
H
FE
G
Tree ImplementationTree Implementation
First child/next sibling representation First child/next sibling representation
•• Application: Unix directory structureApplication: Unix directory structure
A
EDCB
H
FE
G
Binary TreeBinary Tree
•• No node with more than 2 childrenNo node with more than 2 children
•• Avg. depth: O( )Avg. depth: O( )
•• Worst case depth: n-1Worst case depth: n-1
n
root
T1 T2
Binary Tree ImplementationBinary Tree Implementation
•• class Node{class Node{
Data element; Data element;
Node* left; Node* left;
Node* right; Node* right;
} }
•• class BinaryTree{class BinaryTree{
Node* root; Node* root;
} }
Traversal of Binary TreeTraversal of Binary Tree
•• Preorder( node, left, right ): + + a * b c * + * d e f gPreorder( node, left, right ): + + a * b c * + * d e f g
•• Inorder( left, node, right ): a + b * c + d * e + f * gInorder( left, node, right ): a + b * c + d * e + f * g
•• Postorder( left, right, node): a b c * + d e * f + g * +Postorder( left, right, node): a b c * + d e * f + g * +
+
ga
*+
+
c
*
bed
* f
(a + b * c) + ( (d * e + f) * g)(a + b * c) + ( (d * e + f) * g)
Binary Search TreeBinary Search Tree
•• left < node < rightleft < node < right
•• AvgAvg. depth: O(log n). depth: O(log n)
6
1
2
4
3
8
6
1
2
4
3
8
7
validvalid invalidinvalid
OperationsOperations•• Find( T, x )Find( T, x )
if( T == NULL ) if( T == NULL )
return NULL; return NULL;
if( key[x] < T->element ) if( key[x] < T->element )
return( find( T->left, x ) ) return( find( T->left, x ) )
else if( key[x] > T->element ) else if( key[x] > T->element )
return( find( T->right, x ) ) return( find( T->right, x ) )
else return T; else return T;
•• Find_min(T) and Find_max(T)Find_min(T) and Find_max(T)
•• Insert(T, x )Insert(T, x )
•• Delete( T, x )Delete( T, x )
Further StudyFurther Study
•• List: X-or ListList: X-or List
•• Tree: AVL Tree, Splay Tree, B-TreeTree: AVL Tree, Splay Tree, B-Tree
•• Hash TableHash Table