You are on page 1of 7

Final Project Report

For the four test arrays I used an empty array, an array with a single element, an array with an odd amount of numbers, and an array
with an even amount of numbers. The program successfully turned all four unsorted arrays into balanced binary search trees, and
printed out an inorder traversal of each tree without problem. The list of heights for each leaf node of all four arrays was also
successfully printed out, further proving that the trees are balanced as the difference in the heights was never more than one. For the
last NewSearch function, the program was able to find/handle the smallest element greater than or equal to an input number in the
instance of an empty array, a single element array, a number contained within the array, and a number greater than the greatest
element in the array.

Algorithms
Part 1:
Algorithm QuickSort(int unsorted[], int left, int right)
// Input: An unsorted integer array
// Output: A sorted integer array

i ← left, j ← right;

piv ← numbers[(left + right) / 2]; // Set pivot as middle of array

while (i <= j) do
while (numbers[i] < piv) do // Increase i until pivot is reached
i++;
while (numbers[j] > piv) do // Increase j until pivot is reached
j--;
if (i <= j) // Swap numbers[i] and numbers[j] if i <= j

tmp ← numbers[i];

numbers[i] ← numbers[j];

numbers[j] ← tmp;
i++;
j--;
if (left < j) // Recursive call on left side of array
quickSort(numbers, left, j);
if (i < right) // Recursive call on right side of array
quickSort(numbers, i, right);
return;

Algorithm arrayMiddles(BST intBST, int numbers[], int size)


// Input: A sorted array and an empty binary search tree
// Output: A balanced binary search tree
if (size % 2 == 0) // Handling for whether the size of array is odd or even

mid ← (size-1) / 2;

lSize ← mid;

rSize ← mid + 1;

else

mid ← size / 2;

lSize ← mid;

rSize ← mid;
for (int i ← 0; i < lSize; i++) // Populate left array with values left of mid

leftArray[i] ← numbers[i];

for (int i ← mid+1; i < size; i++) // Populate right array with values right of mid

rightArray[i-mid-1] ← numbers[i];

if (size > 2) // Insert mid and recursively call function for left and right arrays
intBST.insert(numbers[mid]);
arrayMiddles(intBST, leftArray, lSize);
arrayMiddles(intBST, rightArray, rSize);
else if (size == 2) // Insert numbers into BST if size of array is 2 or less
intBST.insert(numbers[0]);
intBST.insert(numbers[1]);
else if (size == 1)
intBST.insert(numbers[0]);
return intBST;

Algorithm ArrayTree(BST intBST, int numbers[], int size)


// Input: An empty binary search tree and an unsorted integer array
// Output: A balanced binary search tree
quickSort(numbers, 0, size-1); // Runs quick sort on unsorted array
intBST ← arrayMiddles(intBST, numbers, size); // Inserts sorted array values into binary search tree

return intBST;

Part 3:
Algorithm LeafHeights(Node * locptr, int height)
// Input: Root node of binary tree
// Output: Heights of leaves are output to console, returns current height for recursive calls
if (locptr == 0)
return 0;
else
// If the current node is a leaf node, print its value and height
if (locptr->left == 0 && locptr->right == 0)
print "Height of leaf \"" + locptr->data + "\": " + height + “\n”;
return height;
// Increase current height by one
height++;
// Recursively check left and right nodes
LeafHeights(locptr->left, height);
LeafHeights(locptr->right, height);
return height;
Part 4:
Algorithm NewSearch(int item)
// Input: An integer
// Output: Prints the smallest integer, contained in the binary search tree, greater than or equal to the input integer
Node * locptr = root;
if (locptr == 0)
print "This tree is empty." + “\n”;
return;
// Loop through rightmost nodes to make sure an element greater than the input number exists.
while (locptr->data < item) do

locptr ← locptr->right;

if (locptr == 0)
print "There is no element greater than or equal to " + item + "." + “\n”;
return;
while (locptr != 0) do
// Descend right if the current node's value is less than the input number
if (locptr->data < item)

locptr ← locptr->right;

// Save current value and descend left if current node's value is greater than or equal to the input number
else

smallest ← locptr->data;

// If the input number is found, break out of loop as no further descending is required.
if (smallest == item)
break;

locptr ← locptr->left;

// Print the input number and the found value that is greater than or equal to it.
print "The smallest element that is greater than or equal to " + item + " is " + smallest + "." + “\n”;

Time Complexities:
Part 1: O(2n), as each function call calls itself twice.
Part 2: O(n), where n is the number of nodes, as the function passes through each node only once.
Part 3: O(n), where n is the height of the binary tree, as the function only needs to descend through each node

You might also like