Page 1 of 1

i 2 3 4 5 6 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include #include using

Posted: Sun May 15, 2022 10:14 am
by answerhappygod
I 2 3 4 5 6 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Include Iostream Include Queue Using 1
I 2 3 4 5 6 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Include Iostream Include Queue Using 1 (58.78 KiB) Viewed 50 times
Need help figuring out how to display the output of my
code; this code is about binary search tree in c++.
i 2 3 4 5 6 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <iostream> #include <queue> using namespace std; class BSTNode{ int data; BSTNode *left; BSTNode *right; BSTNode *Parent; public: BSTNode(); // Constructor by default BSTNode (int); // Constructor that specifies the value of a key -BSTNode(); // Destructor (it does nothing) bool leafNode (BSTNode *node); int numberOfChildren (BSTNode *node); BSTNode* parent (BSTNode* root, BSTNode* child); bool SearchNodeRec (BSTNode* root, int data); void insertNodeRec (BSTNode** root, int data); - }; BSTNode::BSTNode(): data (0), left (NULL), right (NULL) { } EBSTNode::BSTNode (int value) { data = value; left = right = NULL; : Ebool BSTNode::leafNode (BSTNode *node) { // Function that determines if the node is a leaf or not. if(node == NULL) () return false; if(node->left == NULL && node->right == NULL) return true; else return false; ; 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 Fint BSTNode:: numberOfChildren (BSTNode *node) { // Function that returns the number of children. int cnt = 0; if(node == NULL) return 0; 0 if(node->left != NULL) { cnt++; } return cnt; } IP BSTNode * BSTNode::parent (BSTNode* root, BSTNode* child) { // a function that returns the parent of a node child. return root->Parent; } a Ipbool BSTNode:: SearchNodeRec (BSTNode* root, int data) { // a function to search if an element is present in the tree. int d = 0; BSTNode *temp = new BSTNode; temp = root; while (temp != NULL) { d++; if(temp->data == data) { cout<< "Found at: " << d << endl; not

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 bool BSTNode:: SearchNodeRec (BSTNode* root, int data) { // a function to search if an element is present in the tree. int d = 0; BSTNode *temp = new BSTNode; temp = root; while (temp != NULL) { d++; if(temp->data == data) { cout<< "Found at: " << d << endl; return 0; } else if(temp->data > d) temp = temp->left; else temp = temp->right; } cout << "NOT FOUND" << endl; return 0; 0 } Avoid BSTNode::insertNodeRec (BSTNode** root, int data) { // a function to insert an element in the tree. queue<BSTNode*> ; q.push(*root); - while (q.size()) { BSTNode *temp = q. front(); q.pop(); if(!temp->left) { if (data != NULL) temp->left = new BSTNode (data); else temp->left = new BSTNode(0); return; 1 else q.push(temp->left); } if(!temp->right) { if (data != NULL) temp->right = new BSTNode (data); = else temp->right = new BSTNode(0); return; } else { q.push(temp->right); } } 91 1 } int main() { // SHOW AN EXAMPLE OUTPUT FOR THIS CODE return 0; } 92 93 94