Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the code below, add a binary tree as shown in example console window. Code should look like this I am missing the tree as

Using the code below, add a binary tree as shown in example console window.

Code should look like this

image text in transcribed

I am missing the tree as shown above, I need it to show a tree based on the numbers given.

**main.cpp**

#include "BINARYTREE.h" #include using namespace std;

int main() { BinaryTree binaryTree = BinaryTree();

cout

binaryTree.insertNode(20); binaryTree.insertNode(5); binaryTree.insertNode(8); binaryTree.insertNode(3); binaryTree.insertNode(12); binaryTree.insertNode(9); binaryTree.insertNode(2); binaryTree.insertNode(16);

cout

cout

cout

cout

cout

cout

cout

cout

return 0; }

** header **

#ifndef BINARYTREE_H #define BINARYTREE_H #include #include

using namespace std;

typedef struct Node { int data; struct Node * left; struct Node * right; } Node;

class BinaryTree { public:

BinaryTree() { root = NULL; }

virtual ~BinaryTree() {

}

void insertNode(int data) { Node * node = (Node * )malloc(sizeof(Node)); node -> data = data; node -> left = NULL; node -> right = NULL; putNodeInTree(node); numberOfNodes++; }

void deleteNode(int data) { if(root != NULL) deleteNode(root, data); numberOfNodes--; }

int size() { return numberOfNodes; }

void preOrder() { _preOrder(root); }

void postOrder() { _postOrder(root); }

void inOrder() { _inOrder(root); }

void printTree() { _printTree(root); }

private:

Node * root; int numberOfNodes = 0; void putNodeInTree(Node * node) { if(root == NULL) root = node; else { Node * current = root; while(true) {

if(current -> data > node -> data) {

if(current -> left == NULL) { current -> left = node; break; } else current = current->left; }

else {

if(current -> right == NULL) { current -> right = node; break; }

else current = current -> right;

} } } }

void _preOrder(Node * node) { if(node != NULL) { cout data left); _preOrder(node -> right); } }

void _inOrder(Node * node) { if(node != NULL) { _inOrder(node->left); cout data right); } }

void _postOrder(Node * node) { if(node != NULL) { _postOrder(node -> left); _postOrder(node -> right); cout data

void _printTreeNode(Node* node, int indent) { if(node != NULL) { if(node -> left) _printTreeNode(node -> left, indent+4); if(node -> right) _printTreeNode(node -> right, indent+4); if (indent > 0) { cout data

void _printTree(Node * node) { return; int padding = _getPadding(node, 0); cout

void _printNSpaces(int n) { for(int i = 0; i

int _getPadding(Node * node, int padding) { if(node == NULL) return padding; int left = _getPadding(node->left, padding + 1); int right = _getPadding(node->right, padding - 1); return left > right ? left : right; }

Node * minValueNode(Node* node) { Node* current = node; while (current->left != NULL) current = current->left; return current; }

Node * deleteNode(Node* root, int data) { if (root == NULL) return root;

if (data data) root -> left = deleteNode(root -> left, data); else if (data > root -> data) root->right = deleteNode(root->right, data); else { if (root->left == NULL) { Node *temp = root->right; free(root); return temp; }

else if (root -> right == NULL) { Node *temp = root->left; free(root); return temp; }

Node* temp = minValueNode(root -> right); root -> data = temp -> data; root -> right = deleteNode(root -> right, temp -> data); }

return root; }

};

#endif // BINARYTREE_H

Inserting nodes with 20, 5, 8, 3, 12, 9, 2, and 16 The number of nodes in the tree is now 8 Here are the values in the tree in order 2 3 5 8 9 12 16 20 Now deleting 8 from the tree... Now deleting 12 from the tree. . The number of nodes in the tree is now 6 Here are the values in the tree in order: 2 3 5 9 16 20 Here are the values in the tree in POST order 2 3 9 16 5 20 Tree Process returned 0 (0x0 excution time0.016 s Press any key to continue Inserting nodes with 20, 5, 8, 3, 12, 9, 2, and 16 The number of nodes in the tree is now 8 Here are the values in the tree in order 2 3 5 8 9 12 16 20 Now deleting 8 from the tree... Now deleting 12 from the tree. . The number of nodes in the tree is now 6 Here are the values in the tree in order: 2 3 5 9 16 20 Here are the values in the tree in POST order 2 3 9 16 5 20 Tree Process returned 0 (0x0 excution time0.016 s Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions