C++ Homework

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;
}
Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more
error: Content is protected !!