You are on page 1of 13

Code / Pseudocode

CE, KMITL 1 Data Structures and Algorithms


Binary Trees
CE, KMITL 2 Data Structures and Algorithms
Preorder Traversal :
UNIX Directory Tree UNIX Directory Tree
CE, KMITL Data Structures and Algorithms 3
Postorder Traversal :
directory directory
CE, KMITL Data Structures and Algorithms 4
Preorder Traversal Pseudocode Preorder Traversal Pseudocode
preOrder( root ) {
if (root is not null) ( )
process(root)
preOrder(root->leftSubtree) p ( )
preOrder(root->rightSubtree)
end if
return
}}
CE, KMITL Data Structures and Algorithms 5
Inorder Traversal Pseudocode Inorder Traversal Pseudocode
inOrder( root ) {
if (root is not null) if (root is not null)
inOrder(root->leftSubtree)
process(root) process(root)
inOrder(root->rightSubtree)
end if end if
return
}
CE, KMITL Data Structures and Algorithms 6
Postorder Traversal Pseudocode Postorder Traversal Pseudocode
postOrder( root ) { postOrder( root ) {
if (root is not null)
postOrder(root->leftSubtree)
postOrder(root->rightSubtree)
process(root)
end if
return
}}
CE, KMITL Data Structures and Algorithms 7
BSTs
CE, KMITL 8 Data Structures and Algorithms
BST Class Template BST Class Template
CE, KMITL Data Structures and Algorithms 9
BST Class Template (contd ) BST Class Template (contd.)
Pointer passed by reference p y
(why?)
Internal functions
used in recursive
calls
CE, KMITL Data Structures and Algorithms 10
calls
BST: Public members calling
private recursive functions private recursive functions
CE, KMITL Data Structures and Algorithms 11
BST: Searching for an element BST: Searching for an element
CE, KMITL Data Structures and Algorithms 12
BST: Search using function objects
CE, KMITL Data Structures and Algorithms 13
BST: Find the smallest element BST: Find the smallest element
Tail recursion
CE, KMITL Data Structures and Algorithms 14
BST: Find the biggest element BST: Find the biggest element
Non-recursive
CE, KMITL Data Structures and Algorithms 15
BST: Insertion (contd ) BST: Insertion (contd.)
Strategy:
Traverse the tree
as in searching for t
with contains()
Insert if you
cannot find the
l t t element t.
CE, KMITL Data Structures and Algorithms 16
BST: Deletion (contd ) BST: Deletion (contd.)
CE, KMITL Data Structures and Algorithms 17
BST: Destructor BST: Destructor
CE, KMITL Data Structures and Algorithms 18
BST: Assignment Operator BST: Assignment Operator
CE, KMITL Data Structures and Algorithms 19
Heaps
CE, KMITL 20 Data Structures and Algorithms
Reheap Up Pseudocode Reheap Up Pseudocode
newNode parent
voi d r eheapUp( heap, newNode) {
i f ( newNode not zer o)
t ( N d 1) / 2
newNode parent
index array
heap
par ent = ( newNode- 1) / 2
i f ( heap[ newNode] . key > heap[ par ent ] . key)
swap( heap, newNode, par ent ) p( p, , p )
r eheapUp( heap, par ent )
end i f
end i f
r et ur n
}

parent index
}
parent index
newNode
CE, KMITL Data Structures and Algorithms 21
Reheap Down Pseudocode Reheap Down Pseudocode
voi d r eheapDown( heap par ent ) {
last index
element
voi d r eheapDown( heap, par ent ) {
/ / Cal cul at e l ocat i ons of chi l dr en
l ef t Chi l dI ndex = 2*par ent + 1;
r i ght Chi l dI ndex = l ef t Chi l dI ndex + 1;
heap
parent indext
r i ght Chi l dI ndex = l ef t Chi l dI ndex + 1;
/ / Det er mi ne whi ch chi l d has t he l ar ger key
i f ( l ef t Chi l dI ndex <= l ast ) / / At l east one chi l d
p
node
i f ( l ef t Chi l dI ndex < l ast ) / / At l east one chi l d
l ef t Key = heap[ l ef t Chi l dI ndex] . key
i f ( r i ght Chi l dI ndex <= l ast ) / / Ri ght chi l d exi st s ( g ) / / g
r i ght Key = heap[ r i ght Chi l dI ndex] . key
el se
r i ght Key = l ef t Key - 1; / / No r i ght chi l d make
sur e l ef t Key bi ggest
end i f
/ / i
CE, KMITL Data Structures and Algorithms 22
/ / cont i nued
Reheap Down Pseudocode Reheap Down Pseudocode
i f ( l ef t Key > r i ght Key) i f ( l ef t Key > r i ght Key)
l ar geChi l dKey = l ef t Key
l ar geChi l dI ndex = l ef t Chi l dI ndex;
el se el se
l ar geChi l dKey = r i ght Key
l ar geChi l dI ndex = r i ght Chi l dI ndex
d i f end i f
/ / Need t o swap par ent wi t h l ar gest chi l d?
i f ( heap[ par ent ] . key < l ar geChi l dKey)
swap( heap, par ent , l ar geChi l dI ndex )
r eheapDown( heap, l ar geChi l dI ndex)
end i f
end i f
r et ur n
}
CE, KMITL Data Structures and Algorithms 23
}
Build Heap Pseudocode Build Heap Pseudocode
wal ker = 1
l ( l k l )
Complexity ?
l oop ( wal ker <= l ast )
r eheapUp( heap, wal ker )
wal ker = wal ker + 1
end l oop end l oop
r et ur n
CE, KMITL Data Structures and Algorithms 24
Insert Heap Pseudocode Insert Heap Pseudocode
i f ( heap f ul l )
f l r et ur n f al se
end i f
Complexity ?
l ast = l ast + 1
heap[ l ast ] dat a heap[ l ast ] = dat a
r eheapUp( heap, l ast )
r et ur n t r ue
CE, KMITL Data Structures and Algorithms 25
Delete Heap Pseudocode Delete Heap Pseudocode
i f ( heap empt y ) i f ( heap empt y )
r et ur n f al se
end i f
dat aOut = heap[ 0]
Complexity ?
dat aOut = heap[ 0]
heap[ 0] = heap[ l ast ]
l ast = l ast 1
r eheapDown( heap, 0) r eheapDown( heap, 0)
r et ur n t r ue
CE, KMITL Data Structures and Algorithms 26
Hashing
CE, KMITL 27 Data Structures and Algorithms
Separate Chaining ( ) Separate Chaining ()
Type yp
Hash Table
Separate Chaining
CE, KMITL Data Structures and Algorithms 28
Separate Chaining ( ) Separate Chaining ()
CE, KMITL Data Structures and Algorithms 29
Separate Chaining ( ) Separate Chaining ()
CE, KMITL Data Structures and Algorithms 30
Separate Chaining ( ) Separate Chaining ()
CE, KMITL Data Structures and Algorithms 31
Separate Chaining ( ) Separate Chaining ()
CE, KMITL Data Structures and Algorithms 32
Rehashing Implementation Rehashing Implementation
CE, KMITL Data Structures and Algorithms 33
Sorting
CE, KMITL 34 Data Structures and Algorithms
Pseudocode Insertion Sort Pseudocode Insertion Sort
t 1 //N t fi t i d f i 0 current = 1 //Note: first index of array is 0
loop (current <= last)
hold = list[current] hold list[current]
walker = current 1;
loop (walker >= 0 AND hold < list[walker])
list[walker+1] = list[walker]
walker = walker 1
end loop end loop
list[walker+1] = hold
current = current + 1
end loop
return
CE, KMITL Data Structures and Algorithms 35
Pseudocode Selection Sort Pseudocode Selection Sort
current = 0
loop (current < last)
smallest = current
lk t 1 walker = current + 1
loop (walker <= last)
if( list[walker] < list[smallest] ) if( list[walker] < list[smallest] )
smallest = walker
end if
walker = walker + 1
end loop
(l ll ) swap(list,current,smallest)
current = current + 1
end loop
CE, KMITL Data Structures and Algorithms 36
end loop
Pseudocode Bubble Sort Pseudocode Bubble Sort
current = 0
d f l sorted = false
loop (current < last AND sorted false)
walker = last
sorted = true
loop (walker > current )
if( list[walker] < list[walker-1] ) if( list[walker] < list[walker 1] )
sorted = false
swap(list, walker, walker-1)
end if end if
walker = walker 1
end loop
current = current + 1
end loop
CE, KMITL Data Structures and Algorithms 37
Pseudocode Quick Sort Pseudocode Quick Sort
quicksort(list leftMostIndex rightMostIndex) { quicksort(list, leftMostIndex, rightMostIndex) {
if (leftMostIndex >= rightMostIndex)
return
d if end if
pivot = list[rightMostIndex]
left = leftMostIndex
right = rightMostIndex 1
loop (left <= right)
// Find key on left that belongs on right
l (li t[l ft] < i t) loop (list[left] < pivot)
left = left + 1
end loop
CE, KMITL Data Structures and Algorithms 38
Pseudocode Quick Sort (cont) Pseudocode Quick Sort (cont)
// Find key on right that belongs on left
loop (right >= left AND list[right] > pivot)
right = right 1 right = right 1
end loop
// Swap out-of-order elements
if (left <= right) // Why swap if left = right?
swap(list, left, right) p( , , g )
left = left + 1
right = right 1
end if end if
end loop
CE, KMITL Data Structures and Algorithms 39
Pseudocode Quick Sort (cont) Pseudocode Quick Sort (cont)
// Move the pivot element to its correct location
swap(list, left, rightMostIndex) swap(list, left, rightMostIndex)
// Continue splitting list and sorting
quickSort(list, leftMostIndex, right)
quickSort(list, left+1, rightMostIndex)
}}
CE, KMITL Data Structures and Algorithms 40
Runtime Quick Sort Runtime Quick Sort
Worst case: pivot
T(N) = T(N-1) + cN
T(N-1) = T(N-2) + c(N-1)
T(N-2) = T(N-3) + c(N-2)

T(2) = T(1) + c(2)


) O(N i c T(1) T(N)
2
N
= + =

Best case: pivot
T(N) = 2 T(N/2) + cN
) ( ( ) ( )
2 i

=
T(N) = 2 T(N/2) + cN
T(N) = cN log N + N = O(N log N)
CE, KMITL Data Structures and Algorithms 41
Runtime Quick Sort (cont) Runtime Quick Sort (cont)
Assume each of the sizes for S
1
are Assume each of the sizes for S
1
are
equally likely. 0 s |S
1
| s N-1.
1 N
0 i
cN 1)] - i - T(N T(i) [
N
1
T(N) + |
.
|

\
|
+ =

=
1 N
0 i
cN T(i)
N
2
+ |
.
|

\
|
=

=
2
1 N
0 i
cN T(i) 2 T(N) N + |
.
|

\
|
=

=
2
2 N
0 i
1) - c(N T(i) 2 1) T(N 1) (N + =
. \

=
CE, KMITL Data Structures and Algorithms 42
0 i
Runtime Quick Sort (cont) Runtime Quick Sort (cont)
2cN 1) T(N 1) (N T(N) N + + = ) ( ) ( ( )
1 N
2c
N
1) T(N
1 N
T(N)
+
+

=
+

N(N+1)
N
2c
1 - N
2) T(N
N
1) - T(N
+

=
( )
1 N
2c
2 - N
3) T(N
1 N
2) - T(N

3
2c
2
) 1 T(
3
T(2)
+ =

+
=
+ =
+
1 N
3 i
i
1
2c
2
T(1)
1 N
T(N)
N) l O(N T(N)
~log
e
(N+1) 3/2
CE, KMITL Data Structures and Algorithms 43
N) log O(N T(N) =
Pseudocode Merge Sort Pseudocode Merge Sort
mergesort(list first last) { mergesort(list, first, last) {
if( first < last )
mid = (first + last)/2; ( ) ;
// Sort the 1
st
half of the list
mergesort(list, first, mid);
// S t th 2
nd
h lf f th li t // Sort the 2
nd
half of the list
mergesort(list, mid+1, last);
// Merge the 2 sorted halves // Merge the 2 sorted halves
merge(list, first, mid, last);
end if
}
CE, KMITL Data Structures and Algorithms 44
Pseudocode Merge Sort (cont) Pseudocode Merge Sort (cont)
merge(list, first, mid, last) {
// Initialize the first and last indices of our subarrays y
indexA = first
lastA = mid
indexB = mid+1
lastB = last
indexC = indexA // Index into our temp array
CE, KMITL Data Structures and Algorithms 45
Pseudocode Merge Sort (cont) Pseudocode Merge Sort (cont)
// Start the merging
loop( indexA <= lastA AND indexB <= lastB )
if( list[indexA] < list[indexB] ) ( [ ] [ ] )
tempArray[indexC] = list[indexA]
indexA = indexA + 1
else else
tempArray[indexC] = list[indexB]
indexB = indexB + 1
d f end if
indexC = indexC + 1;
end loopp
CE, KMITL Data Structures and Algorithms 46
Merge Sort Pseudocode (cont) Merge Sort Pseudocode (cont)
// At this point one of our subarrays is empty // At this point, one of our subarrays is empty
// Now go through and copy any remaining items
// from the non-empty array into our temp array
loop (indexA <= lastA)
tempArray[indexC] = list[indexA]
indexA = indexA + 1 indexA indexA + 1
indexC = indexC + 1
end loop
loop (indexB <= lastB ) loop (indexB <= lastB )
tempArray[indexC] = list[indexB]
indexB = indexB + 1
indexC = indexC + 1
end loop
CE, KMITL Data Structures and Algorithms 47
Merge Sort Pseudocode (cont) Merge Sort Pseudocode (cont)
// Finally, we copy our temp array back into
// our original array
indexC = first
loop (indexC <= last)
li t[i d C] t A [i d C] list[indexC] = tempArray[indexC]
indexC = indexC + 1
end loop end loop
}
CE, KMITL Data Structures and Algorithms 48
Runtime Merge Sort Runtime Merge Sort
Let T(N) be the running time of Let T(N) be the running time of
mergesort on N items.
1 T(1) =
N T(N/2) 2 T(N)
1 T(1)
+ =
1
T(N/2) T(N)
+
1
/4
T(N/4)
/2
T(N/2)
1
N/2
( )
N
( )
+ =
+ =
1
N/8
T(N/8)
N/4
T(N/4)
N/4 N/2
+ =
1
T(1) T(2)
...
N/8 N/4
+ = 1
1 2
+ =
CE, KMITL 49 Data Structures and Algorithms
Runtime Merge Sort Runtime Merge Sort
N log
1
T(1)
N
T(N)
+ =
1 N
N log N N T(N) + =
Bruteforce: Brute force:
N T(N/2) 2 T(N) + =
N/2) T(N/4) (2 2 T(N/2) 2 + =
N T(N/4) 4
N/2) T(N/4) (2 2 T(N/2) 2
+ =
+ =
N 2 T(N/4) 4 T(N) + =
...
N 3 T(N/8) 8 T(N)
( ) ( )
+ =
kN ) T(N/2 2 T(N)
...
k k
+ =
Let k=logN
N log N T(1) N T(N) + =
Let k log N
CE, KMITL 50 Data Structures and Algorithms

You might also like