Вы находитесь на странице: 1из 4

Title: Final Report

Author: Joseph Cortez

ID: 002885414

Date: 2/21/2018

This shows the implementation of Binary Search tree functions working correctly for the final project.
The sample array is {3, 33, 7, 21, 9, 15, 55, 32, 24}. The inorder() function works correctly as it shows the
correct traversal. The leafHeights() function works correctly as it correctly displays each leaf and their
heights. The NewSearch() function works correctly as it correctly shows if the item is in the BST or if
there is a next greater item, or if it is greater than all the items in the BST.

Part 1:

Pseudocode:

Create array

Sort array via Selection Sort()

Create temp[] array to store new balanced array for insertion

Int start = 0, end = arraySize -1, count = -1;


// Resort sorted array to mimic binary search tree to utilize insert function

Void ArrayBalance(int A[], int start, int end, int & count, int temp[])

Increment counter for each pass of outer function

If (start > end)

Return; //Get out of current recursive loop

Mid = start + (end – start) / 2; // Find midpoint of array

Temp[count] = A[mid]; // fill each value of new array

ArrayBalance(A, start, mid -1, count, temp); // traverse left side of array

Count --; // reset counter

ArrayBalance(A, mid + 1, end, count, temp); // traverse right side of array

// Traverse new array and insert each item into balanced binary search tree

For(int I = 0; I < arraySize; i++)

IntBST.insert(temp[i]);

Running Time of SelectionSort is O(n2) where n is array size

Running Time of ArrayBalance is O(n) where n is array size

Part 3:

Pseudocode:

Void leafHeights(ostream & out)

leafHeightsAux(out, myRoot, -1)

// Auxiliary function is necessary because we will be using recursion

leafHeightsAux(ostream & out, BinNodePointer subtreeRoot, int height)

Increment height

If node is null, return

If node is leaf node, print data

If left child exists, check for leaf recursively


If right child exists, check for leaf recursively

Running time for leafHeightsAux is O(n), where n is number of nodes

Part 4:

Pseudocode:

Int find; // number to be found

Cin >> find;

Int NewSearch(int & item)

Return NewSearchAux(item, myRoot);

// Auxiliary function is necessary because we will be using recursion

Int NewSearchAux(int & item, BinNodePointer subtreeRoot)

Static int foundValue = 0;

If subTreeRoot is NULL return foundValue;

If data in subtreeRoot > item

FoundValue = =data in subtreeRoot;

NewSearchAux(item, left subtreeRoot); //Check left subtree

Else if subtreeRoot data < item

NewSearchAux(item, right subtreeRoot); // check right subtree

Else if subtreeRoot data == item

Return item; // item found

Else return foundValue; // return next highest value in tree

Int found = intBST.NewSearch(find);

If found = =0, print the number is greater than all the numbers in the binary search tree

Else if found = =find, print found

Else print found, which is the next largest number in the binary search tree
Running time for NewSearchAux is 0(n) where n is the number of nodes

Вам также может понравиться