Answer to Question #298749 in C++ for Faith

Question #298749

Construct a binary tree using the following 20,19,22,15,12,10,9,7,24,26,23,29,28,25

1
Expert's answer
2022-02-16T16:32:39-0500
#include <iostream>


using namespace std;




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


class btree {
public:
	btree();
	~btree();


	void insert(int key);
	node* search(int key);
	void destroy_tree();
	void inorder_print();
	void postorder_print();
	void preorder_print();


private:
	void destroy_tree(node* leaf);
	void insert(int key, node* leaf);
	node* search(int key, node* leaf);
	void inorder_print(node* leaf);
	void postorder_print(node* leaf);
	void preorder_print(node* leaf);


	node* root;
};




btree::btree() {
	root = NULL;
}


btree::~btree() {
	destroy_tree();
}


void btree::destroy_tree(node* leaf) {
	if (leaf != NULL) {
		destroy_tree(leaf->left);
		destroy_tree(leaf->right);
		delete leaf;
	}
}


void btree::insert(int key, node* leaf) {


	if (key < leaf->value) {
		if (leaf->left != NULL) {
			insert(key, leaf->left);
		}
		else {
			leaf->left = new node;
			leaf->left->value = key;
			leaf->left->left = NULL;
			leaf->left->right = NULL;
		}
	}
	else if (key >= leaf->value) {
		if (leaf->right != NULL) {
			insert(key, leaf->right);
		}
		else {
			leaf->right = new node;
			leaf->right->value = key;
			leaf->right->right = NULL;
			leaf->right->left = NULL;
		}
	}


}


void btree::insert(int key) {
	if (root != NULL) {
		insert(key, root);
	}
	else {
		root = new node;
		root->value = key;
		root->left = NULL;
		root->right = NULL;
	}
}


node* btree::search(int key, node* leaf) {
	if (leaf != NULL) {
		if (key == leaf->value) {
			return leaf;
		}
		if (key < leaf->value) {
			return search(key, leaf->left);
		}
		else {
			return search(key, leaf->right);
		}
	}
	else {
		return NULL;
	}
}


node* btree::search(int key) {
	return search(key, root);
}


void btree::destroy_tree() {
	destroy_tree(root);
}


void btree::inorder_print() {
	inorder_print(root);
	cout << "\n";
}


void btree::inorder_print(node* leaf) {
	if (leaf != NULL) {
		inorder_print(leaf->left);
		cout << leaf->value << ",";
		inorder_print(leaf->right);
	}
}


void btree::postorder_print() {
	postorder_print(root);
	cout << "\n";
}


void btree::postorder_print(node* leaf) {
	if (leaf != NULL) {
		inorder_print(leaf->left);
		inorder_print(leaf->right);
		cout << leaf->value << ",";
	}
}


void btree::preorder_print() {
	preorder_print(root);
	cout << "\n";
}


void btree::preorder_print(node* leaf) {
	if (leaf != NULL) {
		cout << leaf->value << ",";
		inorder_print(leaf->left);
		inorder_print(leaf->right);
	}
}


int main() {


	btree* tree = new btree();


	tree->insert(20);
	tree->insert(19);
	tree->insert(22);
	tree->insert(15);
	tree->insert(12);
	tree->insert(10);
	tree->insert(9);
	tree->insert(7);
	tree->insert(24);
	tree->insert(26);
	tree->insert(23);
	tree->insert(29);
	tree->insert(28);
	tree->insert(25);


	tree->preorder_print();
	tree->inorder_print();
	tree->postorder_print();


	delete tree;


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog