Code / Pseudocode
CE, KMITL 1Data Structures and Algorithms
Binary Trees
CE, KMITL 2Data Structures and Algorithms
Preorder Traversal : UNIX Directory TreeUNIX Directory Tree
CE, KMITL Data Structures and Algorithms 3
Postorder Traversal : directory directory
CE, KMITL Data Structures and Algorithms 4
Preorder Traversal PseudocodePreorder Traversal Pseudocode
preOrder( root ) {if (root is not null)( )
process(root)preOrder(root->leftSubtree)p ( )preOrder(root->rightSubtree)
end ifreturn
}}
CE, KMITL Data Structures and Algorithms 5
Inorder Traversal PseudocodeInorder 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 ifend ifreturn
}
CE, KMITL Data Structures and Algorithms 6
Postorder Traversal PseudocodePostorder Traversal Pseudocode
postOrder( root ) {postOrder( root ) {if (root is not null)
postOrder(root->leftSubtree)postOrder(root->rightSubtree)process(root)
end ifreturn
}}
CE, KMITL Data Structures and Algorithms 7
BSTs
CE, KMITL 8Data Structures and Algorithms
BST Class TemplateBST Class Template
CE, KMITL Data Structures and Algorithms 9
BST Class Template (contd )BST Class Template (contd.)
Pointer passed by referencep y(why?)
Internal functionsused in recursive calls
CE, KMITL Data Structures and Algorithms 10
calls
BST: Public members calling private recursive functionsprivate recursive functions
CE, KMITL Data Structures and Algorithms 11
BST: Searching for an elementBST: 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 elementBST: Find the smallest element
Tail recursion
CE, KMITL Data Structures and Algorithms 14
BST: Find the biggest elementBST: 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 telement t.
CE, KMITL Data Structures and Algorithms 16
BST: Deletion (contd )BST: Deletion (contd.)
CE, KMITL Data Structures and Algorithms 17
BST: DestructorBST: Destructor
CE, KMITL Data Structures and Algorithms 18
BST: Assignment OperatorBST: Assignment Operator
CE, KMITL Data Structures and Algorithms 19
Heaps
CE, KMITL 20Data Structures and Algorithms
Reheap Up PseudocodeReheap Up PseudocodenewNode parent
void reheapUp(heap, newNode) {if (newNode not zero)
t ( N d 1)/2
newNode parent index array heap
parent = (newNode-1)/2if (heap[newNode].key > heap[parent].key)
swap( heap, newNode, parent )p( p, , p )reheapUp( heap, parent )
end ifend ifreturn
}parent index} parent index newNode
CE, KMITL Data Structures and Algorithms 21
Reheap Down PseudocodeReheap Down Pseudocode
void reheapDown(heap parent) {last index element void reheapDown(heap, parent) {
// Calculate locations of childrenleftChildIndex = 2*parent + 1;rightChildIndex = leftChildIndex + 1;
heap
parent indext rightChildIndex = leftChildIndex + 1;// Determine which child has the larger keyif (leftChildIndex rightKey)
largeChildKey = leftKeylargeChildIndex = leftChildIndex;
elseelselargeChildKey = rightKeylargeChildIndex = rightChildIndexd ifend if
// Need to swap parent with largest child?if (heap[parent].key < largeChildKey)
swap( heap, parent, largeChildIndex )reheapDown( heap, largeChildIndex)
end ifend ifreturn
}
CE, KMITL Data Structures and Algorithms 23
}
Build Heap PseudocodeBuild Heap Pseudocode
walker = 1l ( lk l )
Complexity ?
loop (walker
Insert Heap PseudocodeInsert Heap Pseudocode
if (heap full)f lreturn false
end if Complexity ?last = last + 1heap[last] dataheap[last] = datareheapUp(heap,last)return true
CE, KMITL Data Structures and Algorithms 25
Delete Heap PseudocodeDelete Heap Pseudocode
if( heap empty )if( heap empty )return false
end ifdataOut = heap[0] Complexity ?dataOut = heap[0]heap[0] = heap[last]last = last 1reheapDown(heap, 0)reheapDown(heap, 0)return true
CE, KMITL Data Structures and Algorithms 26
Hashing
CE, KMITL 27Data 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 ImplementationRehashing Implementation
CE, KMITL Data Structures and Algorithms 33
Sorting
CE, KMITL 34Data Structures and Algorithms
Pseudocode Insertion SortPseudocode Insertion Sort
t 1 //N t fi t i d f i 0current = 1 //Note: first index of array is 0loop (current = 0 AND hold < list[walker])
list[walker+1] = list[walker]walker = walker 1
end loopend looplist[walker+1] = holdcurrent = current + 1
end loopreturn
CE, KMITL Data Structures and Algorithms 35
Pseudocode Selection SortPseudocode Selection Sortcurrent = 0loop (current < last)
smallest = currentlk t 1walker = current + 1
loop (walker
Pseudocode Bubble SortPseudocode Bubble Sortcurrent = 0
d f lsorted = falseloop (current < last AND sorted false)
walker = lastsorted = trueloop (walker > current )
if( list[walker] < list[walker-1] )if( list[walker] < list[walker 1] )sorted = falseswap(list, walker, walker-1)
end ifend ifwalker = walker 1
end loopcurrent = current + 1
end loop
CE, KMITL Data Structures and Algorithms 37
Pseudocode Quick SortPseudocode Quick Sort
quicksort(list leftMostIndex rightMostIndex) {quicksort(list, leftMostIndex, rightMostIndex) {if (leftMostIndex >= rightMostIndex)
returnd ifend if
pivot = list[rightMostIndex]left = leftMostIndexright = rightMostIndex 1
loop (left = left AND list[right] > pivot)
right = right 1right = right 1end loop
// Swap out-of-order elementsif (left
Runtime Quick SortRuntime Quick Sort Worst case: pivot
T(N) = T(N-1) + cNT(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) 2N
Best case: pivot
T(N) = 2 T(N/2) + cN
)(( )( )2i
T(N) = 2 T(N/2) + cNT(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 S1 are Assume each of the sizes for S1 are
equally likely. 0 |S1| N-1.1N
0icN 1)]-i-T(N T(i)[
N1 T(N)
1N
0icNT(i)
N2
21N
0icN T(i)2 T(N) N
22N
0i1)-c(N T(i)21)T(N 1)(N
CE, KMITL Data Structures and Algorithms 42
0i
Runtime Quick Sort (cont)Runtime Quick Sort (cont)2cN 1)T(N 1)(NT(N) N )()(( )
1N2c
N1)T(N
1NT(N)
N(N+1)
N2c
1-N2)T(N
N1)-T(N
( )
1N2c
2-N3)T(N
1N2)-T(N
32c
2)1T(
3T(2)
1N
3i i12c
2T(1)
1NT(N)
N)lO(NT(N)loge (N+1) 3/2
CE, KMITL Data Structures and Algorithms 43
N) log O(NT(N)
Pseudocode Merge SortPseudocode Merge Sort
mergesort(list first last) {mergesort(list, first, last) {if( first < last )
mid = (first + last)/2;( ) ;// Sort the 1st half of the listmergesort(list, first, mid);// S t th 2nd h lf f th li t// Sort the 2nd half of the listmergesort(list, mid+1, last);// Merge the 2 sorted halves// Merge the 2 sorted halvesmerge(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 subarraysyindexA = firstlastA = midindexB = mid+1lastB = 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 mergingloop( indexA
Runtime Merge SortRuntime Merge Sort Let T(N) be the running time of Let T(N) be the running time of
mergesort on N items.1T(1)
N T(N/2) 2 T(N)1 T(1)
1T(N/2)T(N)
1/4
T(N/4)/2
T(N/2)
1N/2( )
N( )
1N/8
T(N/8)N/4
T(N/4)N/4N/2
1T(1)T(2)...
N/8N/4
112
CE, KMITL 49Data Structures and Algorithms
Runtime Merge SortRuntime Merge SortNlog
1T(1)
NT(N)
1NNlogNNT(N)
Brute force:Brute force:NT(N/2)2T(N)
N/2)T(N/4)(22T(N/2)2 N T(N/4) 4
N/2)T(N/4)(22T(N/2)2
N2T(N/4)4T(N)
...N3T(N/8)8T(N)
( )( )
kN)T(N/22 T(N)...
kk Let k = log N
N log NT(1)NT(N) Let k log N
CE, KMITL 50Data Structures and Algorithms