You are on page 1of 5

Michael Loeser

ID: 2927976
CST-370
Final Coding Assignment – Balanced BST
2/27/2019

Description
The take-home programming final is focused on building and parsing a Balanced Binary Search
Tree. This was an extension of the Binary Search Tree functionality worked on in the prior
week.

Part 1 was the design and implementation of an algorithm to construct a balanced binary
search tree from a sorted integer array. The prior BST.cpp methods were available to use.

The algorithm I created is called balancedInsert and has a private recursive balancedInsertAux
function. It is focused on the idea that a balanced tree works in halves and we were already
working with a sorted array of known length. The root of any part of the tree should be the
median of that range of the integer array. The elements to the left, created from the first half
of the integer array (elements 0 to median-1), can then be looked at as another sub-array
where the root comes from the median of those elements. This is the same process that a
binary search executes and lends itself extremely well to recursive execution. The node is
created from each median element, with index value determined from from the same
calculation used in binary search (startIndex + endIndex)/2. The base case is the same as with
a binary search where if start > end, the function returns. Recursive function calls are made for
the right and left elements in the array range passed in as params.

Algorithm: balancedInsertAux(root, sortedArr[], startIndex, endIndex)


// Input: BinNode root element – passed by reference, the array being parsed, the starting
index, the end index
// Output: none

// base case test – return if index values are all processed


If (startIndex < endIndex)
return

// find the median index value


mid = (startIndex + endIndex)/2

// instantiate the root node element passed as a parameter – data is the median array value
root = new BinNode(sortedArr[mid])

// process the left and right sides of the array values – start to mid-1 and mid+1 to end
balancedInsertAux(root->left, sortedArr, startIndex, mid-1)
balancedInsertAux(root->right, sortedArr, mid+1, endIndex)
Part 2 is the design and implementation of a function FindLevel(item) that traverses the
balanced tree searching for an item. If the item is found the function should return the level
where the item was found. If the item is not found the function should return and display an
error.

This was implemented as a slightly adjusted version of the BST search function. The BST search
returns a Boolean value when the item is found. The FindLevel function (private recursive
version implemented as findLevelAux) returns a 0 when the item is found and adds 1 to the
value for each recursive function return. If the item is not found a -1 is returned and no values
are added. Testing for the -1 return generates the error.

Algorithm: findLevelAux(root, item)


// Input: BinNode root element to test, integer item value to find in the tree
// Output: integer – 0 or higher as the level if the item is found, -1 if not found

// test for null node


If (root == 0)
return

// set the default return value and catch function return


retVal = 0

if (item < root->data) // item less than root, descend left side
retVal = findLevelAux(root->left, item)
else if (root->data < item) // item greater than root, descend right side
retVal = findLevelAux(root->right, item)
else // item found!
return retVal // return the 0 base value

// test for item found condition – 0 or higher


if (retVal > -1)
return retVal+1

// item not found, return the -1


return retVal

Another recursive function called inOrderWithLevelAux was created to traverse the tree using
the inOrder algorithm created for week 7, calling the findLevel function for each node, and
display the output. The output displays in the format example below:
item level
0 0
Part 3 is the creation of the test program to take the sorted array of integers, constructs the
balanced BST, and then prints out the levels for all nodes in the array using the method created
for Part 2.

My test program also supports user entered tests of the findLevel function (commented out for
submission) where the user can give a value to find in the tree. The test function also displays
the tree using an implementation of the recursive graph algorithm described in the book to
help with validation of the balanced tree structure.

Testing:
Testing input arrays are provided in the program spec. The function execution takes the array
pre-coded into the test program, generates the binary search tree, then executes the
inOrderWithLevel function to traverse the tree, displaying the items and levels.

Test Input 1:
Input array - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

The array creates the balanced array shown in the graph. The item 5, which is the median
element in the even number element array, is the root element. The graph display shows the
balanced binary search tree was created.

The level display below the graph traverses the tree with InOrder parsing, which displays the
items in increasing order from left to right. The level values match the tree levels with the root
at level 0 and items 4, 7, 10 at lowest level 3.
Test Input 2:
Input array - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

The array creates the balanced array shown in the graph. The item 6, which is the median
element in the odd number element array, is the root element. The graph display shows the
balanced binary search tree was created.

The level display below the graph traverses the tree with InOrder parsing, which displays the
items in increasing order from left to right. The level values match the tree levels with the root
at level 0 and items 2, 5, 8, 12 at the lowest level 3.

Test Input 3:
Input array - 1
The third test includes only a single element, 1. The graph shows the single item tree. The level
display below the graph shows the single item with level 0.

You might also like