What are “marker for NULL pointers” in binary tree?

廉价感情. 提交于 2019-12-11 10:05:29

问题


I was going through this and this post about binary search tree implementation.

I saw that a binary search tree is represented as (for example):

1 5 7 10 40 50

I was trying to learn about the serialization or de-serialization of the same here. The blog post is making me crazy with those -1s which they're calling markers for NULL pointers. And they're representing the tree as:

20 8 4 -1 -1 12 10 -1 -1 14 -1 -1 -1

Confusion

What are those -1s?

My final goal is to store and read a binary search tree to some kind of file using C# but this confusion is keeping me off.


回答1:


These -1 stand for places where there is no more childs.

For your example

     20
    /
   8__
  /   \
4      12
       /\
     10  14

You can imagine adding additional -1 (you can use any value that can not occur in the tree itself) to places where nodes have no children:

       20
      /   \
     8__   -1
    /   \
  4      12
 /\      /\
-1 -1   10  14
        /\   /\
      -1 -1 -1 -1

And now if you go through your tree in "root, then left subtree, then right subtree" order, you will get the following string:

20 8 4 -1 -1 12 10 -1 -1 14 -1 -1 -1

Which is exactly what you have. So this is a way to represent the tree in an array form. At the same time, it is easy to reconstruct the tree from that form. Knowing that these -1s are special in a sense that they have no more children, you can reconstruct the tree from such an array without any ambiguity.




回答2:


Not all nodes have two children. Obviously, because the tree would be infinite otherwise. At runtime/in memory, a missing node is represented by a nullptr, on disk by -1.




回答3:


The meaning of these -1's was explained in other answers, let me present the code of reading and writing a tree:

class Tree
{
public:
  int value;
  Tree* left;
  Tree* right;

  Tree(int i_value, Tree* i_left, Tree* i_right)
    : value(i_value), left(i_left), right(i_right)
  {}
  Tree(const Tree&) = delete;

  static const int NO_TREE = -1;

  template<typename Iterator>
  static Tree* Create(Iterator& i_iterator)
  {
    if (*i_iterator == NO_TREE)
    {
      i_iterator++;
      return nullptr;
    }
    int value = *(i_iterator++);
    Tree* left = Create(i_iterator);
    Tree* right = Create(i_iterator);
    return new Tree(value, left, right);
  }

  template<typename Iterator>
  static void Write(Tree* i_tree, Iterator& i_iterator)
  {
    if (i_tree == nullptr)
    {
      *(i_iterator++) = NO_TREE;
      return;
    }
    *(i_iterator++) = i_tree->value;
    Write(i_tree->left, i_iterator);
    Write(i_tree->right, i_iterator);
  }
};

Usage:

vector<int> v = { 20, 8, 4, -1, -1, 12, 10, -1, -1, 14, -1, -1, -1 };
Tree* t = Tree::Create(v.begin());
vector<int> w;
Tree::Write(t, std::back_inserter(w));


来源:https://stackoverflow.com/questions/33822277/what-are-marker-for-null-pointers-in-binary-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!