C++ Homework

0 comments

here are all the info you will need:

Using the binary search tree implementation posted on Blackboard, please complete the
following methods:
1. Write a recursive method that checks whether a given binary tree is a BST or not.
2. Write a method that searches the given value and returns the address of the node if it is
found, otherwise returns NULL.
3. Write a method that finds k
th smallest element in a BST.
4. Write a method that finds the minimum absolute difference in a BST.
5. Write a method that prints nodes in top view of a BST.

#include <iostream>
using namespace std;

struct node{
	int data;
	struct node* left;
	struct node* right;
};

struct node* newNode(int data){
	struct node* anode = new struct node;
	anode->data = data;
	anode->left = NULL;
	anode->right = NULL;

	return anode;
}

void insert(struct node** rootRef, int data){
	if(*rootRef == NULL){ //there is no right/left children
		*rootRef = newNode(data);
	}else{
		if(data < (*rootRef)->data ){  //if data is less than root
			insert(&(*rootRef)->left, data);
		}else{
			insert(&(*rootRef)->right, data); //if data is greater than root
		}
	}
}

void printInOrder(struct node* root){

	if(root == NULL) return;
	printInOrder(root->left);
	cout << root->data << " ";
	printInOrder(root->right);
}

void printPostOrder(struct node* root){

	if(root == NULL) return;
	printInOrder(root->left);
	printInOrder(root->right);
	cout << root->data << " ";
}
void printPreOrder(struct node* root){

	if(root == NULL) return;
	cout << root->data << " ";
	printInOrder(root->left);
	printInOrder(root->right);
}

int size(struct node* root){
	if(root == NULL) return 0;
	return (size(root->left)+size(root->right)+1);

}

int height(struct node* root){
	if(root == NULL) return 0;
	return (1+max(height(root->left), height(root->right)));

}

//Find the smallest value in BST (With a loop)
int findMinValueLoop(struct node* root){
	if(root == NULL){
		return 0;
	}else{
		struct node* current = root;
		while(current->left != NULL){
			current = current->left;
		}
		return current->data;
	}
}

//Find the smallest value in BST (Recursively)
int findMinValueRec(struct node* root){

	if(root == NULL){
		return 0;
	}else{
		if(root->left == NULL){
			return root->data;
		}
		return findMinValueRec(root->left);
	}

}

//Find the maximum value in BST (Recursively)
int findMaxValueRec(struct node* root){

	if(root == NULL){
		return 0;
	}else{
		if(root->right == NULL){
			return root->data;
		}
		return findMaxValueRec(root->right);
	}

}

struct node **getSuccessor(struct node** root){
	struct node **temp = root;
	while( (*temp) -> right != NULL){
		temp = &(*temp)->right;
	}
	return temp;

}

void deleteNode(struct node** root, int target){

	if(*root != NULL){
		if(target == (*root)->data){
			if((*root)->right == NULL){
				*root = (*root)->left;
			}else if((*root)->left == NULL){
				*root = (*root)->right;
			}else{
				struct node **successor = getSuccessor(&(*root)->left);
				(*root)->data = (*successor)->data;
				deleteNode(successor,(*successor)->data);
			}
		}else if(target < (*root)->data){
			deleteNode(&(*root)->left, target);
		}else{ //if (target > (*root)->data)
			deleteNode(&(*root)->right, target);
		}

	}

}
int main() {

	struct node* root = NULL;
	insert(&root, 30);
	insert(&root, 50);
	insert(&root, 70);
	insert(&root, 10);
	insert(&root, 90);
	insert(&root, 40);
	insert(&root, 80);
	cout << "In-order: ";
	printInOrder(root);
	cout << endl;
	cout << "Pre-order: " ;
	printPreOrder(root);
	cout << endl;
	cout << "Post-order: ";
	printPostOrder(root);
	cout << endl;

	cout << "The size of the BST: " << size(root) << endl;
	cout << "The height of the BST: " << height(root) << endl;
	cout << "The smallest value of BST: " << findMinValueRec(root) << endl;
	deleteNode(&root, 90);
	cout << "Delete 90 " << endl;

	cout << "Maximum: " << findMaxValueRec(root) << endl;
	cout << "Minimum: " << findMinValueRec(root) << endl;
	return 0;
}

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}