二叉树实现C ++ [英] Binary Tree implementation C++

查看:226
本文介绍了二叉树实现C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:::::二叉树插入::::

:::::Binary Tree insertion::::

#include "stdafx.h"
#include <iostream>

using namespace std;

struct TreeNode {
  int value;
  TreeNode* left;
  TreeNode* right;
};

struct TreeType {
  TreeNode* root;

  void insert(TreeNode* tree, int item);

  void insertItem(int value) {
    insert(root, value);
  }
};

void TreeType::insert(TreeNode* tree, int number) {
  if (tree == NULL) {
    tree = new TreeNode;
    tree->left = NULL;
    tree->right = NULL;
    tree->value = number;
    cout << "DONE";
  } else if (number < tree->value) {
    insert(tree->left, number);
  } else {
    insert(tree->right, number);
  }
}

int main() {
  TreeType* MyTree = new TreeType;
  MyTree->insertItem(8);

  return 0;
}

我目前正在学习C数据结构++,这是code,使插入二进制树。

I am currently learning Data structures in C++ and this is the code that make insertion in binary trees.

这是编译后,一切看起来正常,但是当我尝试执行这个程序,它坠毁。

After it is compiled, everything looks fine, however when i try to execute this program, it crashed.

谁能告诉我在哪里,我错了?

Can anybody tell me where I am wrong?

推荐答案

在你的树的构造函数,你需要初始化根指针为NULL。它不能保证被初始化为NULL。

In your tree constructor, you need to initialize the root pointer to NULL. It's not guaranteed to be initialized as NULL.

当您在Linux编译,你可以使用gdb来显示了当段错误的根源是来自。

When you compile in linux, you can use gdb to show where the source of the segfault is coming from.

其他的一些注意事项:


  1. 您应该值分配回您已经分配的新节点之后。你不这样做是因为你缺少C的基本原则之一++。即,它是基于C。而关于C的事情是,是一个严格的按值功能/方法调用范式。因此,在一个函数调用的参数都值。当您在存储器地址根擦肩而过,你实际上是复制指针的值。然后,你只更新本地的价值。您需要分配回根。如果您想了解这一概念从前到后,我强烈建议看杰里该隐的编程在斯坦福聚合(paradigm)课程

  2. 在主函数中,你应该尽量保持为LOWER_CASE而不是驼峰符号名。这有助于从类型区分变量(类型应该留驼峰)。

  3. 在TreeType :: insert方法,你应该叫树的变量,而不是tree_node。这样做有助于反映正确的类型,避免混淆。

  4. 只要有可能,尽量使用这个 - &GT;根这个 - &GT;插入符号。它不仅将正确解析,如果你不小心创建一个本地范围的变量,但它也更清楚其中的数据或方法定义的阅读器。伟大的编码是关于沟通。它可能只需要100-500ms少为读者了解这里符号指向;然而,微小的积蓄,你可以在避免歧义积累加起来成一个更加清晰的软件。你的未来的自己(和您的同事)会感谢你。见<一href=\"http://msmvps.com/blogs/jon_skeet/archive/2013/09/21/career-and-skills-advice.aspx\">http://msmvps.com/blogs/jon_skeet/archive/2013/09/21/career-and-skills-advice.aspx

  1. You should assign the value back to root after you've allocated the new node. You're not doing that because you're missing one of the fundamentals of c++. That is, it's based on c. And the thing about c is that is strictly a "by-value" function/method calling paradigm. So all parameters in a function call are by value. When you pass in the memory address for root, you're actually copying the value of the pointer. Then, you're only updating the local value. You need to assign it back to root. If you'd like to learn that concept front to back, I highly recommend watching Jerry Cain's Programming Paradigms course at Stanford.
  2. In your main function, you should try to keep the symbol names as lower_case instead of CamelCase. This helps differentiate variables from types (types should stay CamelCase).
  3. In your TreeType::insert method, you should call the variable tree_node instead of tree. Doing so helps reflect the correct type and avoids confusion.
  4. Whenever possible, try you use the this->root and this->insert notation. Not only will it correctly resolve if you accidentally create a locally scoped root variable, but it's also clearer to the reader where the data or method is defined. Great coding is about communication. It may only take 100-500ms less for the reader to understand where the symbol points to; however, the tiny savings that you can accumulate in avoiding ambiguities add up into a much clearer piece of software. Your future self (and your colleagues) will thank you. See http://msmvps.com/blogs/jon_skeet/archive/2013/09/21/career-and-skills-advice.aspx

最后,我可以夸大够从源头上重要的学习是如何。如果你正在学习C或C ++的第一次,读<一个href=\"http://rads.stackoverflow.com/amzn/click/0321563840\">http://www.amazon.com/The-Programming-Language-4th-Edition/dp/0321563840和<一个href=\"http://rads.stackoverflow.com/amzn/click/0131103628\">http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628.这将节省您的时间,在小时,在小时。从源头由于学会了之后,节目也有很多更愉快,因为你理解的概念,相当一部分。而且,事实是,当你在他们有能力过得去事情更有趣。

Lastly, I can overstate enough how important learning from the source is. If you're learning c or c++ for the first time, read http://www.amazon.com/The-Programming-Language-4th-Edition/dp/0321563840 and http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628. It will save you hours, upon hours, upon hours. After having learned from the source, programming is also A LOT more enjoyable because you understand a good portion of the concepts. And, the truth is, things are more fun when you have a decent level of competence in them.

这篇关于二叉树实现C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆