+ 1

Where is the problem in implementing BST in this code

#include <iostream> using namespace std; struct node{ int data; node *left, *right; }; class bst{ public: node *root; bst(){ root = NULL; } void insert(node *, int ); void traversal(node *); }; void bst::traversal(node* cur){ if(cur->left != NULL) traversal(cur->left); cout<<cur->data; if(cur->right != NULL) traversal(cur->right); // if(cur->left != NULL) traversal(cur->left); } void bst::insert(node *cur, int data){ if(cur == NULL){ node *temp = new node; temp->data = data; temp->left = NULL ; temp->right = NULL; cur = temp; } else{ if(data < cur->data) insert(cur->left, data); else insert(cur->right, data); } } int main() { bst a; a.insert(a.root,3); a.insert(a.root,1); a.traversal(a.root); return 0; }

7th Apr 2019, 5:59 AM
Aman Shaw
12 Antworten
0
Aman Shaw Its different as the linked list is accessing the nodes as private members, where as the bst is accessing the nodes passed as pointers into the function. If the insert changes a pointer address, it changes the copy and not the original. This is why passing the pointer back fixed the issue.
7th Apr 2019, 10:47 AM
Jared Bird
Jared Bird - avatar
+ 1
Ok fixed the insert function. Let me know if you understand what I did and why I did it. https://code.sololearn.com/cNaiMhfUQ8F6/?ref=app
7th Apr 2019, 10:10 AM
Jared Bird
Jared Bird - avatar
0
Would be easier to look at if you post in the code playground. Looks ok, are you getting a specific error? From an OOP programing perspective it would make sense for root to be private. Then there is freeing the memory of the nodes.
7th Apr 2019, 7:20 AM
Jared Bird
Jared Bird - avatar
0
There is no output.. I will make the root private and arrange everything.. First i make sure it is working well..
7th Apr 2019, 8:24 AM
Aman Shaw
0
Ok your issue is in the insert method. Might have time to find a fix later. See if you can see it.
7th Apr 2019, 8:49 AM
Jared Bird
Jared Bird - avatar
0
I understand what you did.. But dont understand why insert function need to return node.. It is just adding nodes to bst.. Like i did in linked list..
7th Apr 2019, 10:36 AM
Aman Shaw
0
Here is linked list implementation https://code.sololearn.com/cnBlYPQ78YP9/?ref=app
7th Apr 2019, 10:37 AM
Aman Shaw
0
Aman Shaw Its different as the linked list is accessing the nodes as private members, where as the bst is accessing the nodes passed as pointers into the function. If the insert changes a pointer address, it changes the copy and not the original. This is why passing the pointer back fixed the issue.
7th Apr 2019, 10:47 AM
Jared Bird
Jared Bird - avatar
0
Now i got this all.. Thanks for your help and support. Pointers are very complex. But can do magics.
7th Apr 2019, 11:02 AM
Aman Shaw
0
I get into problem again in remove function.. https://code.sololearn.com/cSG8otwH7yJ9/?ref=app
8th Apr 2019, 10:01 AM
Aman Shaw
0
Might be better to ask as a new question. If you remove a node, how do you decide how to rejoin the tree?
8th Apr 2019, 10:11 AM
Jared Bird
Jared Bird - avatar