【发布时间】:2014-04-29 16:43:23
【问题描述】:
大家好,只是在打印树中每个节点的父节点和兄弟节点时遇到了一些问题。 这是完整的代码块
/*
---------------
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;
}
}
}
所以我遇到的问题是父母和兄弟姐妹正在打印出作为内存位置,如果该值应该为空,它会打印出所有 0,这让我认为它正在工作并且我只需要了解如何获取这些内存位置。
现在我做了一些搜索,在 stackoverflow 上找到了这篇文章 C++ - Get value of a particular memory address
这是我尝试过的
//int left=*reinterpret_cast<int *>(p->left);
//int right=*reinterpret_cast<int *>(p->right);
//int parent=*reinterpret_cast<int *>(p->parent);
我从程序中评论了该部分,因为它不起作用。我会很感激我能得到的任何帮助。 输入:10 20 8 4 5 15 17 2 我不能发布图片,所以我不能发布输出。
编辑: 输出
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
但应该是的
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
正如您所看到的,数字匹配的内存位置也匹配,这支持了它正在工作的事实,只需要获取数字而不是内存地址。
【问题讨论】:
-
在将 int 写入该位置之后,为什么不直接阅读
p->data?reinterpret_cast<int*>(p->left)应该做什么? -
据我所知,这里写的 -> stackoverflow.com/questions/12298208/… 应该尊重内存地址。
-
注释掉的代码后的第一行打印出什么?
标签: c++ pointers data-structures binary-search-tree