【问题标题】:Binary tree insertion method C++二叉树插入方法C++
【发布时间】:2020-08-22 03:30:51
【问题描述】:
struct node {
    person * data;
    node * left, * right;

    node(person * p) {
        data = p; left = NULL; right = NULL;
    }
};

class tree {
    protected:
        node * root;
    public:
        tree() {
            root = NULL;
        }

        tree(person * p) {
            root = new node(p);
        }

        void insert(person * p) {
            if(root == NULL) {
                root = new node(p);
                return;
            }
            node * curr = root;
            while(true) {
                if(p->last < curr->data->last) {
                    if(curr->left == NULL) {
                        curr->left = new node(p);
                        return;
                    }
                    curr = curr->left;
                } else {
                    if(curr->right == NULL) {
                        curr->right = new node(p);
                        return;
                    }
                    curr = curr->right;
                }
            }
        }

        void insert(node * & n, person * p) {
            if(n == NULL) {
                n = new node(p);
                return;
            }
            if(p->last < n->data->last)
                insert(n->left, p);
            else
                insert(n->right, p);            
        }

        void insert(person * p) {
            insert(root, p);
        }
        void print(node * n) {
            if(n == NULL)
                return;
            print(n->left);
            cout << n->data->ss << " " << n->data->bday << " "<< n->data->first << ", " << n->data->last << " " << n->data->zip << "\n";
            print(n->right);
        }

        void print() {
            print(root);
        }
};

我查看了 BST 插入的其他实现,我的逻辑遵循我见过的其他线程。在构造函数中将 Root 设置为 NULL 当我使用打印函数时,它会打印出随机值,而不是与 person 类型一起存储的信息。在我的代码中,我尝试了两种插入方法。我还包括了我的打印功能,以防出现问题。我从打印功能返回的打印输出几乎看起来像是显示内容的内存地址,但缺少一些信息。我不知道从这里去哪里。

【问题讨论】:

  • 调试的结果是什么?我强烈建议您在使用调试器遍历树时绘制树。
  • 这个问题中显示的代码不符合stackoverflow.com对minimal reproducible example的要求,因此这里的任何人都不太可能最终确定问题,而最多只能猜测.这个问题必须是edited 来展示一个最小的例子,不超过一两页代码(“最小”部分),其他人都可以剪切/粘贴、编译、运行和重现所描述的问题( “可重现”部分)完全如图所示(这包括任何辅助信息,例如程序的输入)。请参阅How to Ask 了解更多信息。
  • @ThomasMatthews 我已经修改了我的代码,以便我输入我希望它存储的信息,然后它会立即将其打印出来。有趣的是,我的调试器什么也没告诉我,无论我是插入一条信息还是要读入的文件。

标签: c++ printing tree binary insertion


【解决方案1】:

我发现了问题所在。在我的 person 结构中,我将构造函数的参数命名为与结构中的变量相同。显然 c++ 不喜欢这样做。将构造函数参数的名称更改为其他名称可以解决问题。

旧:

person(int ssn, int bday, string first, string last, int zip) {
        ssn = ssn; bday = bday; first = first; last = llast; zip = zip; 
    }

新:

person(int s, int b, string f, string l, int z) {
        ssn = s; bday = b; first = f; last = l; zip = z; 
    }

【讨论】:

  • 如果你使用了initializers,那么构造函数参数和类成员变量的名称相同不会有什么坏处:person(int ssn, int bday, string first, string last, int zip): ssn(ssn), bday(bday), first(first), last(last), zip(zip) { }。请注意,初始化器替换了成员变量的默认构造(否则编译器会隐式添加),并且应该优先于构造函数主体中的赋值。
猜你喜欢
  • 1970-01-01
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多