0% found this document useful (0 votes)
87 views3 pages

BST

This document contains C++ code that defines methods for a binary search tree (BST) data structure, including constructors, insertion, searching, and traversal methods. It also includes two new methods: balancedBST() which takes a sorted array and builds a balanced BST containing the same elements; and findLevel() which returns the level of a given item in the tree or reports if the item is not found.

Uploaded by

api-377593734
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views3 pages

BST

This document contains C++ code that defines methods for a binary search tree (BST) data structure, including constructors, insertion, searching, and traversal methods. It also includes two new methods: balancedBST() which takes a sorted array and builds a balanced BST containing the same elements; and findLevel() which returns the level of a given item in the tree or reports if the item is not found.

Uploaded by

api-377593734
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

BST.cpp
Michael Rose
002994588
Final Exam take home portion added.
Added definitions of findLevel() and balancedBST() to the file
*/

#include <iostream>
#include <iomanip>

using namespace std;

#include "BST.h"

//--- Definition of constructor


BST::BST()
: myRoot(0)
{}

bool BST::empty() const


{
return myRoot == 0;
}

bool BST::search(const int & item) const


{
if (myRoot == 0)
return false;
else
return this->myRoot->search(item);
}

void BST::insert(const int & item)


{
BinNode * locptr = myRoot; // search pointer
BinNode * parent = 0; // pointer to parent of current node
bool found = false; // indicates if item already in BST
while (!found && locptr != 0)
{
parent = locptr;
if (item < locptr->data) // descend left
locptr = locptr->left;
else if (locptr->data < item) // descend right
locptr = locptr->right;
else // item found
found = true;
}
if (!found)
{ // construct node containing item
locptr = new BinNode(item);
if (parent == 0) // empty tree
myRoot = locptr;
else if (item < parent->data) // insert to left of parent
parent->left = locptr;
else // insert to right of parent
parent->right = locptr;
}
}

//function to create a balanced BST


//Input: a sorted array of ints ARR, the start and the end points
//Output: while the function returns the value of arr[mid], it will also create the
balanced BST
int BST::balancedBST(int arr[], int start, int end) {
//if the start > end just return NULL
if (start > end)
return NULL;

int mid = (start + end) / 2; //get the midpoint


//insert the midpoint
insert(arr[mid]);

//recursively call the function to insert on the left side and the right side
insert(balancedBST(arr, start, mid -1));
insert(balancedBST(arr, mid+1, end));

return arr[mid];
}

//function to find the level of a value


//Input: a value to be found
//Output: the level that the item is at or an error message if the item is not in
the tree
int BST::findLevel(int item) {
int value; //value to be returned
//if the BST is empty, error out
if (myRoot == 0) {
cerr << "BST is empty" << endl;
}
//if we find the data at the root, return 0
else if (myRoot->data == item) {
return 0;
}
//otherwise, search for the item using searchLevel helper function and store
the result
else
value = myRoot->searchLevel(item, 0);
//if the value is -1, we did not find the item so state that it was not found
if (value == -1) {
cerr << item << " does not exist in the BST." << endl;
}
//return the value
return value;
}
void BST::inOrder() {
if (myRoot == 0)
cout << "BST is empty" << endl;
else
myRoot->inorder();
}

void BST::preOrder() {
if (myRoot == 0)
cout << "BST is empty" << endl;
else
myRoot->preorder();
}

int BST::nodeCount() {
if (myRoot == 0)
{
cout << "BST is empty" << endl;
return 0;
}

else
return myRoot->nodeCount();
}

You might also like