Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to implement a print function to print out my binary search tree in an Inorder traversal, Please be as detailed as possible and

I need to implement a print function to print out my binary search tree in an Inorder traversal, Please be as detailed as possible and please fix any error that I might have missed in the code. Also please take a look at my Search function and rewrite it if possible to include printing out if the value is found or not since it might be wrong,

My code is:

#include #include #include using namespace std; // class node for each node of tree class Node { public: // value of node double data; // left and right child Node *left, *right; // depth of this node in the tree int node_depth; // constructor of node Node(double val, int depth = 0) { this->data = val; this->left = NULL; this->right = NULL; this->node_depth = depth; } }; // create a binary search tree class BST { public: // total nodes in bst int nodes; // root of bst Node *root; // depth of complete bst int tree_depth; // constructor of bst BST() { this->root = NULL; this->nodes = 0; this->tree_depth = 0; } // O(1) int size() { return this->nodes; } // O(1) int depth() { return this->tree_depth; } // O(logn) bool insert(double val) { if (!this->root) { this->root = new Node(val); this->nodes++; return true; } Node *temp = this->root; // find the location and insert into the tree. while (temp) { if (temp->data > val) { // if left exits move ahead else insert to the left of tree if (temp->left) { temp = temp->left; } else { temp->left = new Node(val, temp->node_depth + 1); this->tree_depth = max(this->tree_depth, temp->left->node_depth); this->nodes++; return true; } // if already present then return false } else if (temp->data == val) { return false; // else insert in the right node } else { // same as left child. // if right not exists then insert else insert if (temp->right) { temp = temp->right; } else { temp->right = new Node(val, temp->node_depth + 1); // change depth if it has changed this->tree_depth = max(this->tree_depth, temp->right->node_depth); this->nodes++; return true; } } } return false; } // O(logn) Node *search(double val) { // if tree is not valid if (!this->root) return NULL; Node *temp = this->root; // find if present while (temp) { if (temp->data == val) { return temp; } else if (temp->data > val) { temp = temp->left; } else { temp = temp->right; } } // not found return NULL; } // Function to perform inorder traversal on the BST Node* minValueNode(Node* node) { Node* current = node; /* loop down to find the leftmost leaf */ while (current && current->left != NULL) current = current->left; return current; } Node* deleteNode(Node* root, int data) { // base case if (root == NULL) return root; // If the key to be deleted is // smaller than the root's // key, then it lies in left subtree if (data data) root->left = deleteNode(root->left, data); // If the key to be deleted is // greater than the root's // key, then it lies in right subtree else if (data > root->data) root->right = deleteNode(root->right, data); // if key is same as root's key, then This is the node // to be deleted else { // node has no child val if (root->left==NULL and root->right==NULL) return NULL; // node with only one child or no child 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 with two children: Get the inorder successor // (smallest in the right subtree) Node* temp = minValueNode(root->right); // Copy the inorder successor's content to this node root->data = temp->data; // Delete the inorder successor root->right = deleteNode(root->right, temp->data); } return root; } };

int main() { BST tree, *root = NULL; srand(time(NULL)); int n = 5; for(int i=0; i { int y = rand() % n; tree.insert(y); } tree.insert(n+1); cout tree.inorder(root); return 0; }

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

fork tannenbaum

Answered: 1 week ago