【问题标题】:Binary sort tree - Getting value from memory location二叉排序树 - 从内存位置获取值
【发布时间】: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-&gt;datareinterpret_cast&lt;int*&gt;(p-&gt;left) 应该做什么?
  • 据我所知,这里写的 -> stackoverflow.com/questions/12298208/… 应该尊重内存地址。
  • 注释掉的代码后的第一行打印出什么?

标签: c++ pointers data-structures binary-search-tree


【解决方案1】:

在文件顶部添加 #include &lt;iomanip&gt; 以格式化输出,就像我在此答案中所做的那样:

您只是不检查指针是否有效。如果指向子节点的指针为 NULL,则无法访问那里的 data 属性,因为尚未为其分配内存。

另外,请记住,如果您发现自己需要访问内存中的原始地址而不是使用简单的指针,那么您很可能做错了事/做错了事。

只需检查子/父节点是否为空。

setw(4)left 将宽度设置为 4,并将输出格式化为左对齐。

#include <iomanip>

...

void BinSearchT::inorder(tr_node* p)
{
    if(p != NULL)
    {
        if(p->left) inorder(p->left);

        if (p->left)
            cout << "Left: " << setw(4) << left << p->left->data << "\t";
        else
            cout << "Left: Null\t";

        if (p->right)
            cout << "Right: " << setw(4) << left << p->right->data << "\t";
        else
            cout << "Right: Null\t";

        if (p->parent)
            cout << "Parent: " << setw(4) << left << p->parent->data << endl;
        else
            cout << "Parent: Null" << endl;

        if(p->right) inorder(p->right);
    }
    else 
        return;
}

插入之后:

 BinSearchTOps 
 ----------------------------- 
 1. Insertion/Creation 
 2. In-Order Traversal 
 3. Exit 
 Enter your choice : 2

 In-Order Traversal 
 -------------------
Left: Null  Right: Null Parent: 4   
Left: 2     Right: 5    Parent: 8   
Left: Null  Right: Null Parent: 4   
Left: 4     Right: Null Parent: 10  
Left: 8     Right: 20   Parent: Null
Left: Null  Right: 17   Parent: 20  
Left: Null  Right: Null Parent: 15  
Left: 15    Right: Null Parent: 10  

【讨论】:

  • 谢谢,能够与我的教授交谈,她指出了同样的事情,即我没有检查 Null 以及我丢失的 -> 数据。感谢您的帮助。
【解决方案2】:

这部分代码显然有问题:

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;

如果p-&gt;left 为空,您的代码将出现段错误或其他奇怪的行为。当我在您的代码中添加好的测试时,它对我有用...... 例如:

if ( p->left && p->right )
cout<<"\n "<<p->data<<" Left: "<<p->left->data<<" Right: "<<p->right->data<<" Parent:" <<p->parent->data<<endl;

    if(p->left) {
    std::cout << " Left: "<<p->left->data<< std::endl ;
    inorder(p->left);
}

    if(p->right) {
    std::cout << " Right: "<<p->right->data<< std::endl ;
    inorder(p->right) ;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多