Hey guys just having some issues with getting print out for parent and siblings of each node in the tree. Here is the full block of code
/*
---------------
Binary sort tree
*/
#include <iostream>
#include <cstdlib>
#include <cstdint>
using namespace std;
class BinSearchT
{
private:
struct tr_node
{
tr_node* left;
tr_node* right;
tr_node* parent;
int data;
};
tr_node* root;
public:
BinSearchT()
{
root = NULL;
}
bool isEmpty() const { return root==NULL; }
void pInorder();
void inorder(tr_node*);
void insert(int);
void remove(int);
};
void BinSearchT::insert(int d)
{
tr_node* t = new tr_node;
//tr_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
t->parent = NULL;
// is this a new tree?
if(isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
tr_node* curr;
curr = root;
// Find the Node's parent
while(curr)
{
t->parent = curr;
if(t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if(t->data < t->parent->data)
t->parent->left = t;
else
t->parent->right = t;
}
}
void BinSearchT::pInorder()
{
inorder(root);
}
void BinSearchT::inorder(tr_node* p)
{
if(p != NULL)
{
if(p->left) inorder(p->left);
//int left=*reinterpret_cast<int *>(p->left);
//int right=*reinterpret_cast<int *>(p->right);
//int parent=*reinterpret_cast<int *>(p->parent);
cout<<"\n "<<p->data<<" Left: "<<p->left->data<<" Right: "<<p->right->data<<" Parent: "<<p->parent->data<<endl;
if(p->right) inorder(p->right);
}
else return;
}
int main()
{
BinSearchT b;
int ch,tmp,tmp1;
while(1)
{
cout<<endl<<endl;
cout<<" BinSearchTOps "<<endl;
cout<<" ----------------------------- "<<endl;
cout<<" 1. Insertion/Creation "<<endl;
cout<<" 2. In-Order Traversal "<<endl;
cout<<" 3. Exit "<<endl;
cout<<" Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<" Enter Number to be inserted(just one) : ";
cin>>tmp;
b.insert(tmp);
break;
case 2 : cout<<endl;
cout<<" In-Order Traversal "<<endl;
cout<<" -------------------"<<endl;
b.pInorder();
break;
case 3 : system("pause");
return 0;
break;
}
}
}
so the issue that i am having is that the parent and the siblings are printing out as memory locations and if the value is supposed to be null it prints out all 0's which makes me think that it is working and that i just need to find out how to get those memory locations.
Now i do some searching and found this post on stackoverflow C++ - Get value of a particular memory address
this is what i've tried
//int left=*reinterpret_cast<int *>(p->left);
//int right=*reinterpret_cast<int *>(p->right);
//int parent=*reinterpret_cast<int *>(p->parent);
i commented that part out of the program since it did not work. I would appreciate any help i can get. input: 10 20 8 4 5 15 17 2 I cant post images so i cant post the output.
EDIT: OUTPUT
2 Left: 00000000 Right:00000000 Parent:00484C58
4 Left: 00484F98 Right:00484CA8 Parent:00484C08
5 Left: 00000000 Right:00000000 Parent:00484C58
8 Left: 00484C58 Right:00000000 Parent:00484BB8
10 Left: 00484C08 Right:00484EF8 Parent:00000000
15 Left: 00000000 Right:00484F48 Parent:00484BB8
17 Left: 00000000 Right:00000000 Parent:008A4F48
20 Left: 008A4F48 Right:00000000 Parent:008A4BB8
But it should be
2 Left: Null Right:Null Parent:4
4 Left: 2 Right:5 Parent:8
5 Left: Null Right:Null Parent:4
8 Left: 4 Right:Null Parent:10
10 Left: 8 Right:20 Parent:Null
15 Left: Null Right:17 Parent:20
17 Left: Null Right:Null Parent:15
20 Left: 15 Right:Null Parent:10
As you can see where ever the numbers match the memory locations also match which supports the fact that it is working just need to get the numbers and not the memory address.